0% found this document useful (0 votes)
9 views37 pages

LAB 6 - Joins

Uploaded by

2023899776
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views37 pages

LAB 6 - Joins

Uploaded by

2023899776
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 37

MUHAMMAD HAMIZ MOHD RADZI

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

 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

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 table prefixes to improve performance.

 Instead of full table name prefixes, use table aliases.

 Table alias gives a table a shorter name:

 Keeps SQL code smaller, uses less memory

 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

HR  CROSS JOIN returns a Cartesian product from the two tables


 Display employee_id, department_id and department name of all employees

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.

SELECT employee_id, employees.department_id, department_name


FROM employees, departments
WHERE employees.department_id = departments.department_id;

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.

 Rules – PK-FK must have


 Same common attribute name
 Same datatype

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.

 The NATURAL JOIN and USING clauses are mutually exclusive.

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.

 Use the ON clause to specify arbitrary conditions or specify columns to join.

 The join condition is separated from other search conditions.

 The ON clause makes code easy to understand

 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.

 We showed this using the recursive or "pig's ear" relationship.

HR
HR
 Find employees’ name together with their manager’s name

SELECT worker.last_name “EMP”, manager.last_name “MGR"


FROM employees worker
JOIN employees manager
ON worker.manager_id = manager.employee_id;

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.

SELECT e.last_name, e.department_id, d.department_name


FROM employees e LEFT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;

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.

SELECT e.last_name, e.department_id, d.department_name


FROM employees e RIGHT OUTER JOIN departments d
ON (e.department_id = d.department_id) ;

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.

SELECT e.last_name, d.department_id, d.department_name


FROM employees e FULL OUTER JOIN departments d
ON (e.department_id = d.department_id) ;

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

 To avoid a Cartesian product, always include a valid join condition.

HR
107 employees 27 departments

107 x 27 = 2889 rows of result


HR
SELECT last_name, department_name
FROM employees
CROSS JOIN 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

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy