Islamic University of Gaza Faculty of Engineering Computer Engineering Dept. Database Lab (ECOM 4113) Lab 5 Retrieving Data from Multiple Tables Eng. Mohammed Alokshiya November 2, 2014
An JOIN clause is used to combine rows from two or more tables, based on a common field between them. The following table lists the types the different JOINs you can use: INNER JOIN: Specifies a join between two tables with an explicit join clause. LEFT OUTER JOIN: Specifies a join between two tables with an explicit join clause, preserving unmatched rows from the first table. RIGHT OUTER JOIN: Specifies a join between two tables with an explicit join clause, preserving unmatched rows from the second table. FULL OUTER JOIN: Return all rows when there is a match in ONE of the tables. NATURAL JOIN: creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name in both tables. CROSS JOIN: produces the cross-product of two tables. In general, the syntax of an JOIN clause is: JOIN Syntax SELECT TABLE1.COLUMN, TABLE2.COLUMN FROM TABLE1 [NATURAL JOIN TABLE2] [JOIN TABLE2 USING (COLUMN_NAME)] [JOIN TABLE2 ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME)] [LEFT RIGHT FULL OUTER JOIN TABLE2 ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME)] [CROSS JOIN TABLE2]; INNER JOIN An INNER JOIN is a JOIN operation that allows you to specify an explicit join clause. Syntax: INNER JOIN Syntax TABLE1 [INNER] JOIN TABLE2 USING (COLUMN_NAME) ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME); 2
Example: Join the EMPLOYEES and DEPARTMENTS tables, select employee ID (EMPLOYEE_ID), employee last name, (LAST_NAME), department ID and department name (DEPARTMENT_NAME) of all employees who are hired before 2004. SELECT EMPLOYEE_ID, LAST_NAME, DEPARTMENT_ID, DEPARTMENT_NAME JOIN DEPARTMENTS USING (DEPARTMENT_ID) WHERE HIRE_DATE < '01-JAN-04'; You can also use ON clause instead of USING clause: SELECT E.EMPLOYEE_ID, E.LAST_NAME, D.DEPARTMENT_ID, D.DEPARTMENT_NAME E JOIN DEPARTMENTS D ON E.DEPARTMENT_ID = D.DEPARTMENT_ID WHERE HIRE_DATE < '01-JAN-04'; 3
Creating Three-Way Joins with the ON Clause A three-way join is a join of three tables. Joins are performed from left to right. Example: Write a query to retrieve first name, last name, department name and city, for all employees whose salary is greater than 9000. SELECT E.FIRST_NAME, E.LAST_NAME, E.SALARY, D.DEPARTMENT_NAME, L.CITY E JOIN DEPARTMENTS D ON E.DEPARTMENT_ID = D.DEPARTMENT_ID JOIN LOCATIONS L ON D.LOCATION_ID = L.LOCATION_ID WHERE SALARY > 9000; Example: write a query to retrieve first name, last name, job, department id, and department name for all employees who work in Toronto City. SELECT E.FIRST_NAME, E.LAST_NAME, E.JOB_ID, DEPARTMENT_ID, D.DEPARTMENT_NAME, L.CITY E JOIN DEPARTMENTS D USING (DEPARTMENT_ID) JOIN LOCATIONS L ON (D.LOCATION_ID = L.LOCATION_ID AND CITY = 'Toronto'); 4
Self-Join: Joining a Table to Itself Example: write a query to retrieve the first name, last name and manager full name of all employees. SELECT WORKER.FIRST_NAME, WORKER.LAST_NAME, MANAGER.FIRST_NAME ' ' MANAGER.LAST_NAME "Manager Full Name" WORKER JOIN EMPLOYEES MANAGER ON WORKER.MANAGER_ID = MANAGER.EMPLOYEE_ID; Example: write a query to retrieve employee last names, department numbers, and all the employees who work in the same department as a given employee. SELECT E.LAST_NAME, DEPARTMENT_ID, C.LAST_NAME "Colleague" E JOIN EMPLOYEES C USING (DEPARTMENT_ID) WHERE E.EMPLOYEE_ID <> C.EMPLOYEE_ID ORDER BY 2, 1; 5
Example: write a query to retrieve the names and hire dates of all the employees who are hired before their managers, along with their managers names and hire dates. SELECT WORKER.LAST_NAME, WORKER.HIRE_DATE, MANAGER.LAST_NAME, MANAGER.HIRE_DATE WORKER JOIN EMPLOYEES MANAGER ON WORKER.MANAGER_ID = MANAGER.EMPLOYEE_ID WHERE WORKER.HIRE_DATE < MANAGER.HIRE_DATE; 6
OUTER JOIN By default, joining tables with the NATURAL JOIN, USING, or ON clauses results in an inner join. Any unmatched rows are not displayed in the output. To return the unmatched rows, you can use an outer join. An outer join returns all rows that satisfy the join condition and also returns some or all of those rows from one table for which no rows from the other table satisfy the join condition. There are three types of outer joins: LEFT OUTER RIGHT OUTER FULL OUTER Syntax: OUTER JOIN Syntax TABLE1 {LEFT RIGHT FULL} [OUTER] JOIN TABLE2 USING (COLUMN_NAME) ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME); Example: LEFT OUTER JOIN: SELECT LAST_NAME, DEPARTMENT_NAME E LEFT OUTER JOIN DEPARTMENTS D USING (DEPARTMENT_ID); 7
Example: Right OUTER JOIN: SELECT E.LAST_NAME, E.DEPARTMENT_ID, D.DEPARTMENT_ID, D.DEPARTMENT_NAME E RIGHT OUTER JOIN DEPARTMENTS D ON E.DEPARTMENT_ID = D.DEPARTMENT_ID; Example: FULL OUTER JOIN: SELECT E.LAST_NAME, E.DEPARTMENT_ID, D.DEPARTMENT_ID, D.DEPARTMENT_NAME E FULL OUTER JOIN DEPARTMENTS D ON E.DEPARTMENT_ID = D.DEPARTMENT_ID; 8
NATURAL JOIN operation A NATURAL JOIN is a JOIN operation that creates an implicit join clause for you based on the common columns in the two tables being joined. Common columns are columns that have the same name in both tables. A NATURAL JOIN can be an INNER join, a LEFT OUTER join, or a RIGHT OUTER join. The default is INNER join. If the SELECT statement in which the NATURAL JOIN operation appears has an asterisk (*) in the select list, the asterisk will be expanded to the following list of columns (in this order): All the common columns Every column in the first (left) table that is not a common column Every column in the second (right) table that is not a common column Example: retrieve department id, department name, location id and city for all departments. SELECT DEPARTMENT_ID, DEPARTMENT_NAME, LOCATION_ID, CITY FROM DEPARTMENTS NATURAL JOIN LOCATIONS; 9
Summary: 10
Using Subqueries to Solve Queries Example: retrieving all employees who has a salary greater than the average salaries in department 80. First solution: retrieve targeted employees with two steps; firstly, retrieve average salaries of all employees in dept. 80, then, use the result in another query. The first query: First Query SELECT AVG(SALARY) WHERE DEPARTMENT_ID = 80; Then you will get the average: Use it in a new Query: Second Query SELECT FIRST_NAME, LAST_NAME, SALARY WHERE SALARY > 8955.882; And you will get the targeted employees: A better solution is to embedded the first query in the second query in two parentheses, which is called: inner query: Solution using Inner Query SELECT FIRST_NAME, LAST_NAME, SALARY WHERE SALARY > ( SELECT AVG(SALARY) WHERE DEPARTMENT_ID = 80 ); 11
Types of Subqueries Single-row subqueries: Queries that return only one row from the inner SELECT statement. Multiple-row subqueries: Queries that return more than one row from the inner SELECT statement. A single-row subquery uses a single-row operator (>, <, =, <>, etc...). however, multiple-row subqueries use multiple-row operator (IN, ALL, ANY), instead of a single-row operator. The multiple-row comparison operators are: Operator IN ANY ALL Meaning Equal to any member in the list Must be preceded by =, <>, >, <, <=, >=. Compares a value to each value in a list or returned by a query. Evaluates to FALSE if the query returns no rows. Must be preceded by =, <>, >, <, <=, >=. Compares a value to every value in a list or returned by a query. Evaluates to TRUE if the query returns no rows. Example: retrieving all employees are not IT programmers and whose salary is less than that of any IT programmer. SELECT EMPLOYEE_ID, LAST_NAME, JOB_ID, SALARY WHERE SALARY < ANY (SELECT SALARY WHERE JOB_ID = 'IT_PROG' ) AND JOB_ID <> 'IT_PROG'; 12
Example: retrieving all employees whose salary is less than the salary of all employees with a job ID of IT_PROG and whose job is not IT_PROG. SELECT EMPLOYEE_ID, LAST_NAME, JOB_ID, SALARY WHERE SALARY < ALL (SELECT SALARY WHERE JOB_ID = 'IT_PROG' ) AND JOB_ID <> 'IT_PROG'; Example: write a query to retrieve the employee number and last name of all employees who work in a department with any employee whose last name contains the letter u. SELECT EMPLOYEE_ID, LAST_NAME WHERE DEPARTMENT_ID IN ( SELECT DISTINCT DEPARTMENT_ID WHERE LAST_NAME LIKE '%u%' ); 13
Example: write a query to retrieve the last name, department number, and job ID of all employees whose department location ID is 1700. SELECT LAST_NAME, DEPARTMENT_ID, JOB_ID WHERE DEPARTMENT_ID IN ( SELECT DISTINCT DEPARTMENT_ID FROM DEPARTMENTS WHERE LOCATION_ID = 1700 ); 14
Set Operators: Set operators are used to combine the results of two or more component queries into one result. Queries containing set operators are called compound queries. In oracle, there are 4 Set Operators: UNION: return all rows from multiple tables and eliminate any duplicate rows. A B UNION ALL: return all rows from multiple tables including all duplicate rows. INTERSECT: return all rows from multiple tables and eliminate any duplicate rows. A B MINUS: return all rows from multiple tables and eliminate any duplicate rows. A B 15
Example: write a query to retrieve department IDs for departments that do not contain the job ID ST_CLERK. SELECT DEPARTMENT_ID FROM DEPARTMENTS MINUS SELECT DEPARTMENT_ID WHERE JOB_ID = 'ST_CLERK'; Example: write a query to retrieve name and id for all countries that have no departments located in them. SELECT COUNTRY_ID, COUNTRY_NAME FROM COUNTRIES MINUS SELECT COUNTRY_ID, COUNTRY_NAME FROM DEPARTMENTS JOIN LOCATIONS USING (LOCATION_ID) JOIN COUNTRIES USING (COUNTRY_ID); 16
Exercises: 1. Write a query (without using set operators) to retrieve name and id for all countries that have no departments located in them. 2. Write a query to retrieve all employees whose salary is more than the salary of any employee from department 60. 3. Write a query to retrieve first name, last name, and department name, for employees whose salary is more than all their collagenous salaries in the same department. 4. Write a query to retrieve employees whose job has minimum salary 4200 and maximum salary 9000. 5. Write a query to retrieve departments names and cities for all departments that has more than 5 employees. 17