LAB 6 - Joins
LAB 6 - Joins
HR
After completing this lesson, you should be able to do the following:
Write SELECT statements to access data from more than one table using equijoins
and nonequijoins
View data that generally does not meet a join condition by using outer joins
HR
Up to now, your experience using SQL has been limited to querying and returning
information from one database table at a time.
This would not be a problem if all data in the database were stored in only one
table.
But you know from data modeling that separating data into individual tables and
being able to associate the tables with one another is the heart of relational
database design.
Fortunately, SQL provides join conditions that enable information to be queried
from separate tables and combined in one report.
HR
Joins that are compliant with the SQL:1999 standard include the following:
Natural joins:
NATURAL JOIN clause
USING clause
ON clause
Outer joins:
LEFT OUTER JOIN
RIGHT OUTER JOIN
FULL OUTER JOIN
Cross joins
HR
Use table prefixes to qualify column names that are in multiple tables.
QUALIFYING AMBIGUOUS COLUMN NAMES
Use column aliases to distinguish columns that have identical names, but reside in
different tables.
HR
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 an equijoin based on the column name
JOIN table2 ON table1.column_name = table2.column_name performs an equijoin
based on the condition in the ON clause
LEFT/RIGHT/FULL OUTER is used to perform outer joins
EMPLOYEES DEPARTMENTS
HR
You need to understand that, for
the table to be joined, there must
be a relationship between them.
When there is a relationship, there
must be a FK.
This PK-FK is what we called as
common attribute.
For example, EMPLOYEES and
DEPARTMENTS can be joined
directly by using common
attribute named
DEPARTMENT_ID.
HR
Display employee_id, department_id and department name of all employees
department_id
from employees
SELECT employee_id, e.department_id, department_name table
FROM employees e, departments d
WHERE e.department_id = d.department_id; Table alias
Qualify common
attribute from both
tables
However in Oracle, we can use keyword JOIN to tackle the same problem.
HR
EMPLOYEES DEPARTMENTS
HR
Table alias is important as if you do not have them, the table full name must be
presented in any clause that wants to use it.
HR
A SQL join clause combines fields from 2 (or more) tables in a relational database.
A natural join is based on all columns in two tables that have the same name and
selects rows from the two tables that have equal values in all matched columns.
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.
HR
SELECT employee_id, department_id, department_name
FROM employees
NATURAL JOIN departments;
HR
If several columns have the same names but the data types do not match, natural
join can be applied using the USING clause to specify the columns that should be
used for an equijoin.
Use the USING clause to match only one column when more than one column
matches.
HR
SELECT employee_id, department_id, department_name
FROM employees
JOIN departments Enclosed the common attribute in a parentheses.
USING (department_id); Attribute in a parentheses cannot be qualified!
HR
SELECT employee_id, department_id, department_name
FROM employees
JOIN departments
USING (department_id)
WHERE department_name LIKE ‘A%’;
HR
SELECT employee_id, e.department_id, department_name
FROM employees e
JOIN departments d
USING (department_id);
HR
The join condition for the natural join is basically an equijoin of all columns with
the same name.
Table alias is compulsory for the common attribute in the SELECT statement
HR
SELECT employee_id, e.department_id “DEPT EMP”,
d.department_id “DEPT DEPT”, department_name
FROM employees e
JOIN departments d
ON (e.department_id = d.department_id);
HR
SELECT employee_id, e.department_id “DEPT EMP”,
d.department_id “DEPT DEPT”, department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
WHERE employee_id < 105;
HR
SELECT employee_id, department_name, city
FROM employees
NATURAL JOIN DEPARTMENTS
NATURAL JOIN LOCATIONS;
HR
SELECT employee_id, department_name, city
FROM employees
JOIN departments
USING (department_id)
JOIN locations
USING (location_id);
HR
SELECT employee_id, e.department_id “DEPT EMP”,
d.department_id “DEPT DEPT”, department_name
FROM employees e
JOIN departments d
ON e.department_id = d.department_id
JOIN locations l
ON d.location_id = l.location_id;
HR
In data modeling, it was sometimes necessary to show an entity with a relationship
to itself.
For example, an employee can also be a manager.
HR
HR
Find employees’ name together with their manager’s name
HR
Sometimes you may need to retrieve data from a table that has no corresponding
column in another table.
Suppose we want to know the grade_level for each employees salary.
The job_grades table does not have a common column with the employees table.
Using an ON clause allows us to join the two tables
HR
SELECT last_name, salary, grade_level, lowest_sal,
highest_sal
FROM employees
JOIN job_grades
ON(salary BETWEEN lowest_sal AND highest_sal);
HR
In ANSI-99 SQL, a join of two or more tables that returns only the matched rows is
called an inner join.
When a join returns the unmatched rows as well as the matched rows, it is called an
outer join.
Outer join syntax uses the terms "left, full, and right".
These names are associated with the order of the table names in the FROM clause
of the SELECT statement.
HR
This query retrieves all rows in the EMPLOYEES table, which is the left table, even if
there is no match in the DEPARTMENTS table.
HR
This query retrieves all rows in the DEPARTMENTS table, which is the right table,
even if there is no match in the EMPLOYEES table.
HR
This query retrieves all rows in the EMPLOYEES table, even if there is no match in
the DEPARTMENTS table. It also retrieves all rows in the DEPARTMENTS table, even
if there is no match in the EMPLOYEES table.
HR
A Cartesian product is formed when:
A join condition is omitted
A join condition is invalid
All rows in the first table are joined to all rows in the second table
HR
107 employees 27 departments
HR
In this lesson, you should have learned how to use joins to display data from
multiple tables by using:
Equijoins
Nonequijoins
Outer joins
Self-joins
Cross joins
Natural joins
Full (or two-sided) outer joins
HR
Oracle Academy, Database Programming with SQL
HR