The Islamic University of Gaza Faculty of Engineering Department of Computer Engineering ECOM 4113: Database Lab Lab # 5 Retreiving Data from Multiple Tables Eng. Alaa O Shama November, 2015
Objectives: Write SELECT statements to access data from more than one table using joins. Join a table to itself by using a self-join. View data that generally does not meet a join condition by using OUTER joins. Join Types: A join is used to view information from multiple tables. Therefore, you can join tables together to view information from more than one tables, based on a common field between them. The following table lists the types the different SQL JOINs you can use: CROSS JOIN: produces the cross-product of two tables. INNER JOIN: Specifies a join between two tables with an explicit join clause.
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 and data type in both tables. 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. 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]; In the syntax: - table1.column denotes the table and the column from which data is retrieved. - NATURAL JOIN joins two tables based on the same column name - JOIN table2 USING column_name performs a join based on the column name. - JOIN table2 ON table1.column_name = table2.column_name performs a join based on the condition in the ON clause. - LEFT/RIGHT/FULL OUTER is used to perform OUTER joins. - CROSS JOIN returns a Cartesian product from the two tables.
Qualifying Ambiguous Column Names When joining two or more tables, you need to qualify the names of the columns with the table name to avoid ambiguity. So we use table prefix to execute a query. If there are no common column names between the two tables, there is no need to qualify the columns. However, using the table prefix improves performance, because you tell the Oracle server exactly where to find the columns. Instead of full table name prefixes, use table aliases. Keeps SQL code smaller, uses less memory. In the following query, we have REGION_ID col in REGION and COUNTRY tables, so we need to use table prefix to identify to which table col belongs. 1. CROSS JOIN CROSS JOIN Syntax Table1 CROSS JOIN Table2 OR Table1, Table2 It produces the cross-product of two tables. The joined table will contain a row for every possible combination of rows from Table1 to Table2 consisting of all columns in Table1 followed by all columns intable2.
If the tables have N and M rows respectively, the joined table will have N* M rows. 2. Natural JOIN A NATURAL JOIN is a JOIN operation that creates an implicit join 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 defferent data types, an error is returned. Natural JOIN Example :
3. INNER JOIN An INNER JOIN is a JOIN operation that allows you to specify an explicit join clause. INNER JOIN Syntax TABLE1 [INNER] JOIN TABLE2 USING (COLUMN_NAME) ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME); INNER JOIN Syntax 1. USING ( COLUMN NAME ) Use the USING clause to match only one column when more than one column matches. Note: Don't qualify a column that is used in the USING clause. 2. ON (boolean_expression) Use the ON clause to specify a join condition. With this, you can specify join conditions separate from any search or filter conditions in the WHERE clause.
You can apply additional conditions to a join using AND clause or on WHERE clause. Ex: select employee ID, employee last name, department ID and department name of all employees who are hired before 2004. - USING
- ON or 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. Ex: retrieve first name, last name, job, department id, and department name for all employees who work in Toronto City.
Ex: Retrieve country name for each department. Self-Join: Joining a Table to Itself Sometimes you need to join a table to itself. To find the name of each employee's manager, you need to join the EMPLOYEE table to itself. In this process, you look in the table twice. The first time you look in the table to find LAST_NAME column and MANAGER_ID. The second time you look in the EMPLOYEE_ID column to find value of the MANAGER_ID you found then the LAST_NAME column to find manager's name.
Note: you must alias the table in self-join. Ex: retrieve employee's last names, department numbers, and all the employees who work in the same department as a given employee. Ex: 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.
4. 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 OUTER JOIN Syntax TABLE1 {LEFT RIGHT FULL} [OUTER] JOIN TABLE2 USING (COLUMN_NAME) ON (TABLE1.COLUMN_NAME = TABLE2.COLUMN_NAME); LEFT OUTER JOIN Example: To return the employees that do not have an assigned department.
RIGHT OUTER JOIN Example: To return the department that do not have any employees. FULL OUTER JOIN Example: This query retrieves all rows in the EMPLOYEES table, even if there is no match in the DEPARTMENT table. It also retrieves all rows in the DEPARTMENT table, even if there is no match in the EMPLOYEES table.
NOTE: for easy solutions For easy solution especially with more than two tables joins, you can use a cross join instead of other join types and put the join condition in WHERE clause and you will have the same result. Example: Write a query to retrieve first name, last name, department name and city, for all employees whose salary is greater than 9000. And Will have the same result
Exercise: ( Must be in your report ) 1. Display department name, manager name, and salary of the manager for all managers whose experience is more than 5 years. 2. Display job title and average salary of employees. 3. Display employee name if the employee joined before his manager. GOOD LUCK