Lab 05 RDBMS
Lab 05 RDBMS
LAB TASKS:
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.
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
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
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
• 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.
• 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.
33
Lab # 5: Displaying Data from Multiple Tables SWE-209
34
Lab # 5: Displaying Data from Multiple Tables SWE-209
Nonequijoins
Outer Joins
35
Lab # 5: Displaying Data from Multiple Tables SWE-209
Cartesian Products
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.
37