0% found this document useful (0 votes)
10 views

Lab 05 RDBMS

Uploaded by

Muhammad Areesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Lab 05 RDBMS

Uploaded by

Muhammad Areesh
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab # 4: Sorting Data, Set Operators SWE-209

LAB TASKS:

Employees(employee_id, first_name, last_name, email, phone_number, hire_date, job_id,


salary, commission_pct, manager_id, department_id)

1. From the following schema, write a SQL query to find those employees whose first
name contains the letters R, A, or N. Sort the result-set in ascending order by salary.
Return all fields.

2. From the following schema, write a SQL query to find those employees who earn
above 21000 or the fifth character in their phone number is 8.. Sort the result-set in
ascending order by last name. Return full name (first name and last name), hire date,
commission percentage, email, and telephone separated by '-', and salary.

Orders(ord_no, purch_amt, ord_date, customer_id, salesman_id)


Salesman (salesman_id, name, city, commission)
Customer (customer_id, cust_name, city, grade, salesman_id)

3. From the following table, write a SQL query to find those salespersons generated the
largest and smallest orders on each date. Return salesperson ID, name, order no.,
highest on/ lowest on, order date.
4. From the following tables, write a SQL query to find those salespersons who have
same cities where customer lives as well as do not have customers in their cities and
indicate it by ‘NO MATCH’. Sort the result set on 2nd column (i.e. name) in
descending order. Return salesperson ID, name, customer name, commission.

32
Lab # 5: Displaying Data from Multiple Tables SWE-209

LAB # 5

DISPLAYING DATA FROM MULTIPLE TABLES

OBJECTIVE

 Write SELECT statements to access data from more than one table using
equijoins and nonequijoins
 Join a table to itself by using a self-join
 View data that generally does not meet a join condition by using outer joins
 Generate a Cartesian product of all rows from two or more tables

THEORY

OBTAINING DATA FROM MULTIPLE TABLES

Types of Joins

Joins that are compliant with the SQL:1999 standard include the following:

• Cross joins
• Natural joins
• USING clause
• Full (or two-sided) outer joins
• Arbitrary join conditions for outer joins

31
Lab # 5: Displaying Data from Multiple Tables SWE-209

Joining Tables Using SQL:1999 Syntax


Use a join to query data from more than one table

Creating Natural Joins


• The NATURAL JOIN clause is based on all columns in the two tables that
have the same name.
• It selects rows from the two tables that have equal values in all matched
columns.
• If the columns having the same names have different data types, an error is
returned.

Retrieving Records with Natural Joins

Creating Joins with the USING Clause

• If several columns have the same names but the data types do not match, the
NATURAL JOIN clause can be modified with the USING clause to specify
the columns that should be used for an equijoin.

32
Lab # 5: Displaying Data from Multiple Tables SWE-209

• Use the USING clause to match only one column when more than one column
matches.
• Do not use a table name or alias in the referenced columns.
• The NATURAL JOIN and USING clauses are mutually exclusive.

Joining Column Names

Retrieving Records with the USING Clause

Qualifying Ambiguous Column Names

• Use table prefixes to qualify column names that are in multiple tables.
• Use table prefixes to improve performance.
• Use column aliases to distinguish columns that have identical names but reside
in different tables.
• Do not use aliases on columns that are identified in the USING clause and
listed elsewhere in the SQL statement.

Using Table Aliases


• Use table aliases to simplify queries.
• Use table aliases to improve performance

33
Lab # 5: Displaying Data from Multiple Tables SWE-209

Creating Joins with the ON Clause


• The join condition for the natural join is basically an equijoin of all columns
with the same name.
• Use the ON clause to specify arbitrary conditions or specify columns to join.
• The join condition is separated from other search conditions.
• The ON clause makes code easy to understand.

Retrieving Records with the ON Clause

Self-Joins Using the ON Clause

Self-Joins Using the ON Clause

Applying Additional Conditions to a Join

34
Lab # 5: Displaying Data from Multiple Tables SWE-209

Creating Three-Way Joins with the ON Clause

Nonequijoins

Retrieving Records with Nonequijoins

Outer Joins

35
Lab # 5: Displaying Data from Multiple Tables SWE-209

INNER versus OUTER Joins


• In SQL: 1999, the join of two tables returning only matched rows is called an
inner join.
• A join between two tables that returns the results of the inner join as well as
the unmatched rows from the left (or right) tables is called a left (or right)
outer join.
• A join between two tables that returns the results of an inner join as well as the
results of a left and right join is a full outer join.

LEFT OUTER JOIN

RIGHT OUTER JOIN

FULL OUTER JOIN

Cartesian Products

• A Cartesian product is formed when:


1. A join condition is omitted

2. A join condition is invalid


3. All rows in the first table are joined to all rows in the second table

• To avoid a Cartesian product, always include a valid join condition.

Creating Cross Joins


• The CROSS JOIN clause produces the cross-product of two tables.
• This is also called a Cartesian product between the two tables.

36
Lab # 5: Displaying Data from Multiple Tables SWE-209

LAB TASKS:
Departments (department_id, department_name, manager_id, location_id)
Employees (employee_id, first_name, last_name, email, phone_number,
hire_date, job_id, salary, commission_pct, manager_id, department_id)
Jobs (job_id, job_title, min_salary, max_salary)

1. From the above schema, write a SQL query to find all those employees
who work in department ID 50 or 70. Return first name, last name,
department number and department name.
2. From the above schema, write a SQL query to find all departments
including those without any employee. Return first name, last name,
department ID, department name.
3. From the following table, write a SQL query to find the employees and
their managers. These managers do not work under any manager.
Return the first name of the employee and manager.
4. From the following table, write a SQL query to compute the average
salary of employees for each job title.

Customer (customer_id, cust_name, city, grade, salesman_id)


Order (ord_no, purch_amt, ord_date, customer_id, salesman_id)

Write a SQL statement to make a report with customer name, city,


order no., order date, purchase amount for those customers from the
existing list who placed one or more orders or which order(s) have
been placed by the customer who is not on the list.

37

You might also like