Retrieving Data from Multiple Tables Eng. Mohammed Alokshiya

12 downloads 147 Views 814KB Size Report
Nov 2, 2014 - Computer Engineering Dept. Database Lab (ECOM .... Example: write a query to retrieve the names and hire d
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 SQL 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 SQL 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 SQL 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. SQL SELECT EMPLOYEE_ID, LAST_NAME, DEPARTMENT_ID, DEPARTMENT_NAME FROM EMPLOYEES JOIN DEPARTMENTS USING (DEPARTMENT_ID) WHERE HIRE_DATE < '01-JAN-04';

You can also use ON clause instead of USING clause: SQL SELECT E.EMPLOYEE_ID, E.LAST_NAME, D.DEPARTMENT_ID, D.DEPARTMENT_NAME FROM EMPLOYEES 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. SQL SELECT E.FIRST_NAME, E.LAST_NAME, E.SALARY, D.DEPARTMENT_NAME, L.CITY FROM EMPLOYEES 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. SQL SELECT E.FIRST_NAME, E.LAST_NAME, E.JOB_ID, DEPARTMENT_ID, D.DEPARTMENT_NAME, L.CITY FROM EMPLOYEES 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. SQL SELECT WORKER.FIRST_NAME, WORKER.LAST_NAME, MANAGER.FIRST_NAME || ' ' || MANAGER.LAST_NAME "Manager Full Name" FROM EMPLOYEES 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. SQL SELECT E.LAST_NAME, DEPARTMENT_ID, C.LAST_NAME "Colleague" FROM EMPLOYEES 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. SQL SELECT WORKER.LAST_NAME, WORKER.HIRE_DATE, MANAGER.LAST_NAME, MANAGER.HIRE_DATE FROM EMPLOYEES 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: SQL SELECT LAST_NAME, DEPARTMENT_NAME FROM EMPLOYEES E LEFT OUTER JOIN DEPARTMENTS D USING (DEPARTMENT_ID);

7

Example: Right OUTER JOIN: SQL SELECT E.LAST_NAME, E.DEPARTMENT_ID, D.DEPARTMENT_ID, D.DEPARTMENT_NAME FROM EMPLOYEES E RIGHT OUTER JOIN DEPARTMENTS D ON E.DEPARTMENT_ID = D.DEPARTMENT_ID;

Example: FULL OUTER JOIN: SQL SELECT E.LAST_NAME, E.DEPARTMENT_ID, D.DEPARTMENT_ID, D.DEPARTMENT_NAME FROM EMPLOYEES 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. SQL 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) FROM EMPLOYEES WHERE DEPARTMENT_ID = 80;

Then you will get the average: Use it in a new Query: Second Query SELECT FIRST_NAME, LAST_NAME, SALARY FROM EMPLOYEES 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 FROM EMPLOYEES WHERE SALARY > ( SELECT AVG(SALARY) FROM EMPLOYEES 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 (>, , ,

Suggest Documents