JOINS and the FROM Clause Contd.

SQL for SQL Server
Bijoy Bordoloi and Douglas Bock
Chapter 6: Joins
Prentice Hall © 2004
1
OBJECTIVES
• Learn the basic join operation rules.
• Write equijoin and inequality join queries by using
the WHERE clause.
• Write equijoin and inequality join queries by using
the JOIN keyword in the FROM clause.
• Write complex join queries with more than two
tables and more than two columns.
• Write outer join queries.
• Write self join (recursive) queries.
Prentice Hall © 2004
2
A TYPICAL JOIN OPERATION
• The following figure displays the employee and
department tables.
• The figure illustrates the concept of a JOIN
operation by connecting columns within each table
with a line.
• One line connects the employee table’s
emp_dpt_number column with the department
table's dpt_no column.
• A second line connects the employee table’s
emp_ssn column to the department table’s
dep_mgrssn column.
Prentice Hall © 2004
3
A TYPICAL JOIN OPERATION
Prentice Hall © 2004
4
JOINS
• We will begin our study of JOIN operations by
focusing on the relationship between the employee
and department tables represented by the common
department number values.
• Our first query lists employee names and
department numbers. This query only retrieves
data from the single employee table.
Prentice Hall © 2004
5
JOINS Contd.
SELECT CAST(emp_last_name As CHAR(12)) "Last Name",
CAST(emp_first_name As CHAR(12)) "First Name",
CAST(emp_dpt_number As CHAR(4)) "Dept"
FROM employee;
Last Name
First Name
Dept
------------ ------------ ---Bock
Douglas
7
Amin
Hyder
3
Joshi
Dinesh
7
Zhu
Waiman
7
more rows will be displayed . . .
Prentice Hall © 2004
6
JOINS Contd.
• A large organization can have dozens or even
hundreds of departments. Thus, the numbers
displayed in the department column shown above
may not be very meaningful.
• Suppose you want the department names instead
of the department numbers to be listed.
• The department names are mentioned in the
“Department” table.
• Hence we need to join the “Employee” and the
“Department” tables to get the required results.
Prentice Hall © 2004
7
JOINS Contd.
• Joins can be specified in either the FROM or
WHERE clauses.
• The join conditions combine with the search
conditions in the WHERE and HAVING clauses to
control the rows that are selected from the tables
referenced in the FROM clause.
• Early standards for SQL specified the use of a
WHERE clause to join tables. The newer
ANSI/ISO SQL-99 standard uses the FROM
clause to carry out a JOIN operation.
• The older syntax is, however, still valid, and is
expected to remain valid in the future (except for
OUTER JOIN).
Prentice Hall © 2004
8
JOINS Contd.
/* JOIN using the WHERE clause */
SELECT CAST(emp_last_name As CHAR(12)) "Last Name",
CAST(emp_first_name As CHAR(12)) "First Name",
dpt_name "Department Name"
FROM employee, department
WHERE emp_dpt_number = dpt_no;
/* JOIN using the FROM clause */
SELECT CAST(emp_last_name As CHAR(12)) "Last Name",
CAST(emp_first_name As CHAR(12)) "First Name",
dpt_name "Department Name"
FROM employee e JOIN department d ON (e.emp_dpt_number =
d.dpt_no);
• Output is identical for both the queries.
Prentice Hall © 2004
9
JOINS Contd.
Last Name
First Name
Department Name
------------ ------------ --------------Bock
Douglas
Production
Amin
Hyder
Admin and Records
Joshi
Dinesh
Production
Zhu
Waiman
Production
more rows will be displayed . . .
Prentice Hall © 2004
10
HOW JOINS ARE PROCESSED
• Conceptually, when two tables are joined, SQL
creates a Cartesian product of the tables.
• A Cartesian product consists of all possible
combinations of the rows from each of the
tables regardless of whether or not the rows are
related to one another.
• Therefore, when a table with 10 rows is joined
with a table with 20 rows, the Cartesian product
is 200 rows (10 x 20 = 200 ).
Prentice Hall © 2004
11
HOW JOINS ARE PROCESSED
Contd.
• Following are two tables simply named Table_1 and
Table_2.
• Each table has a single column named Col_1. Each
table also has three rows with simple alphabetic
values stored in the Col_1 column.
Table_1
Table_2
COL_1
a
b
c
COL_1
a
b
c
Prentice Hall © 2004
12
JOINS
SELECT *
FROM table_1, table_2;
COL_1 COL_1
-------- --------a
a
b
a
c
a
a
b
b
b
c
b
a
c
b
c
c
c
Prentice Hall © 2004
13
JOINS Contd.
• The first row of the “table_1” table was joined with every
row in the “table_2” table.
• A Cartesian product may not be useful and could be
misleading.
• Always include a WHERE clause in your JOIN statements.
SELECT *
FROM table_1, table_2;
WHERE table_1.col_1 = table_2.col_1;
col_1
-------a
b
c
col_1
-------a
b
c
Prentice Hall © 2004
14
JOIN Example
• Using the WHERE clause
SELECT CAST(emp_last_name As CHAR(12)) "Last Name",
CAST(emp_first_name As CHAR(12)) "First Name",
dpt_name "Department Name"
FROM employee e, department d
WHERE e.emp_dpt_number = d.dpt_no
AND e.emp_dpt_number = 7;
Last Name
-----------Bock
Joshi
Zhu
Prescott
First Name
-----------Douglas
Dinesh
Waiman
Sherri
Department Name
-------------------Production
Production
Production
Production
Prentice Hall © 2004
15
JOIN Example Contd.
• Using the FROM clause
SELECT CAST(emp_last_name As CHAR(12)) "Last
Name",
CAST(emp_first_name As CHAR(12)) "First Name",
dpt_name "Department Name"
FROM employee e JOIN department d ON
(e.emp_dpt_number = d.dpt_no)
AND e.emp_dpt_number = 7;
Prentice Hall © 2004
16
JOIN Example Contd.
• Using a combination of FROM and
WHERE clauses (recommended method)
SELECT CAST(emp_last_name As CHAR(12)) "Last
Name",
CAST(emp_first_name As CHAR(12)) "First Name",
dpt_name "Department Name"
FROM employee e JOIN department d ON
(e.emp_dpt_number = d.dpt_no)
WHERE e.emp_dpt_number = 7;
Prentice Hall © 2004
17
JOIN OPERATION RULES
JOINS and the SELECT Clause
• A JOIN query always begins with a SELECT
clause.
• List the columns to be displayed in the result table
after the SELECT keyword.
• The result table column order reflects the order in
which column names are listed in the SELECT
clause.
• To modify the order in which column names are
listed, simply rearrange the order of the column
listing in the SELECT clause.
Prentice Hall © 2004
18
Example
SELECT dpt_name "Department Name",
CAST(emp_last_name As CHAR(12)) "Last Name",
CAST(emp_first_name As CHAR(12)) "First Name"
FROM employee e JOIN department d ON (e.emp_dpt_number =
d.dpt_no)
WHERE e.emp_dpt_number = 7;
Department Name
-------------------Production
Production
Production
Production
Last Name
-----------Bock
Joshi
Zhu
Prescott
Prentice Hall © 2004
First Name
-----------Douglas
Dinesh
Waiman
Sherri
19
JOIN OPERATION RULES
• JOIN operations also support the specification of
all columns by the use of a simple asterisk (*) in a
SELECT clause.
• The result table for the query will contain all
columns from both the tables.
• When the asterisk (*) is used, the column order of
the result table is based on the order in which
tables are listed in the FROM clause.
Prentice Hall © 2004
20
JOINS and the FROM Clause
• Any SELECT statement that has two or more table names
listed in a FROM clause is a JOIN query.
• By definition, a JOIN operation retrieves rows from two or
more tables.
• The FROM clause is always used to list the tables from
which columns are to be retrieved by a JOIN query.
• The FROM clause listing has a limit of 256 table names.
• The order of table name listings is irrelevant to the
production of the result table with the one exception – that
is, if you use an asterisk (*) in the SELECT clause, then
the column order in the result table reflects the order in
which tables are listed in the FROM clause.
Prentice Hall © 2004
21
JOINS and the FROM Clause Contd.
• As explained earlier, you can specify the join conditions in
a FROM clause by explicitly using the JOIN clause within
the FROM clause.
• You can also specify the type of JOIN such as INNER,
LEFT OUTER, RIGHT OUTER or FULL OUTER.
• If you do not specify the type of join then, by default, the
join operation is always an INNER join.
• We did not indicate the type of join in any of the example
queries thus far. So, they are all examples of INNER join
operations.
• The various OUTER join operations are covered later in
this chapter.
Prentice Hall © 2004
22
JOINS and the WHERE Clause
• The WHERE clause specifies the relationship
between tables listed in the FROM clause.
• It also restricts the rows displayed in the result
table.
• The order of selection criteria or JOIN
operations is not important.
Prentice Hall © 2004
23
JOINS and the WHERE Clause
Contd.
SELECT CAST(emp_last_name As CHAR(12)) "Last Name",
CAST(emp_first_name As CHAR(12)) "First Name",
dpt_name "Department Name"
FROM employee e, department d
WHERE e.emp_dpt_number = 7
AND e.emp_dpt_number = d.dpt_no;
Last Name
-----------Bock
Joshi
Zhu
Prescott
First Name
-----------Douglas
Dinesh
Waiman
Sherri
Department Name
-------------------Production
Production
Production
Production
Prentice Hall © 2004
24
JOINS and the WHERE Clause
Contd.
A note of caution though! The scenario exemplified in the
previous example applies only if you are joining tables using the
WHERE clause. If you elect to both join tables and specify row
selection criteria in a FROM clause, then you MUST specify the JOIN
operation before specifying the row selection criteria. Otherwise, SQL
Server will generate an error message.
SELECT CAST(emp_last_name As CHAR(12)) "Last Name",
CAST(emp_first_name As CHAR(12)) "First Name",
dpt_name "Department Name"
FROM emp_dpt_number = 7 and
employee e JOIN department d ON (e.emp_dpt_number = d.dpt_no);
Server: Msg 170, Level 15, State 1, Line 5
Line 5: Incorrect syntax near '='.
Prentice Hall © 2004
25
QUALIFYING COLUMN NAMES
• Normally you do not need to create alias names for tables.
When column names are ambiguous (the column names
used are from more than one table) you must qualify them
SELECT *
FROM table_1, table_2
WHERE col_1 = col_1;
• This query is WRONG, SQL Server would reject this
query and generate an error message.
Server: Msg 209, Level 16, State 1, Line 2
Ambiguous column name 'Col_1'.
Prentice Hall © 2004
26
QUALIFYING COLUMN NAMES Contd.
• This error message tells you that you have included a
column name somewhere in the query that exists in more
than one table listed in the FROM clause.
• Here the error is in the WHERE clause; however, it is also
possible to make a similar error in the SELECT clause.
• The SELECT statement shown below fails to qualify the
col_1 name in the SELECT clause, and SQL Server again
produces an error message.
SELECT col_1
FROM table_1, table_2
WHERE table_1.col_1 = table_2.col_1;
Server: Msg 209, Level 16, State 1, Line 2
Ambiguous column name 'Col_1'.
Prentice Hall © 2004
27
QUALIFYING COLUMN NAMES Contd.
• An ambiguous column name is qualified by using the DOT
(.) connector to connect the table name and column name.
• Sometimes it is easier to qualify column names by using table
alias names.
• Often, a single letter is used as an identifier to reduce
keystroke requirements .
SELECT CAST(e.emp_last_name As CHAR(12)) "Last Name",
CAST(e.emp_first_name As CHAR(12)) "First Name",
dpt_name "Department Name"
FROM employee e JOIN department d ON
(e.emp_dpt_number = d.dpt_no)
WHERE e.emp_dpt_number = 7;
Prentice Hall © 2004
28
QUALIFYING COLUMN NAMES Contd.
• The use of the letters “e” and “d” is completely
arbitrary; “t1” and “t2” or any other unique aliases
could have been used.
• The important points to learn are:
– The alias must follow a table name.
– Use a space to separate a table name and its alias.
– The alias must be unique within the SELECT
statement.
• If the column names are not identical you are not
required to qualify them, although you still might
want to for documentation purposes.
Prentice Hall © 2004
29
JOIN Operations Using Inequality Operators (<, >)
• The most commonly used JOIN operator is the "equal" (=) sign.
• You may use any relational operator in a JOIN query. The next query
uses an inequality operator, the greater than (>) relational operator.
SELECT CAST(emp_last_name As CHAR(12)) "Emp Last Name",
CAST(dep_name AS CHAR(12)) "Dependent Name"
FROM employee e JOIN dependent d ON (e.emp_ssn <> d.dep_emp_ssn)
WHERE e.emp_last_name IN ('Bordoloi', 'Bock')
ORDER BY emp_last_name;
Emp Last Name Dependent Name
------------- -------------Bock
Jo Ellen
.
.
.
.
Bock
Bordoloi
Rita
Deanna
More rows are displayed..
Prentice Hall © 2004
30
Joining More Than Two Tables
• While the examples given thus far have joined
rows from two tables, you can specify up to 256
tables in a JOIN operation for SQL Server.
• The more tables that are included in a JOIN
operation, the longer the query will take to
process, especially when the tables are large with
millions of rows per table.
Prentice Hall © 2004
31
Joining More Than Two Tables Contd.
• The example shown in following figure joins three
tables to produce a result table based on two different
relationships.
Prentice Hall © 2004
32
Joining More Than Two Tables Contd.
• Example
SELECT CAST(emp_last_name As CHAR(12)) "Last Name",
CAST(emp_first_name As CHAR(12)) "First Name",
'$' + CONVERT (CHAR (12), 1.10*emp_salary, 1) "Raised Salary",
p.pro_name "Project"
FROM employee e JOIN assignment a ON (e.emp_ssn = a.work_emp_ssn)
JOIN project p ON (a.work_pro_number = p.pro_number)
WHERE p.pro_name = 'Inventory';
Last Name
-----------Amin
Zhu
Markis
First Name
-----------Hyder
Waiman
Marcia
Raised Salary
------------$27500.000000
$47300.000000
$27500.000000
Prentice Hall © 2004
Project
--------Inventory
Inventory
Inventory
33
Joining More Than Two Tables Contd.
• Note that in the previous example you are
displaying data only from two tables, employee
and project. Yet, you are joining three tables,
employee, assignment, and project!
• This is because the tables, employee and project,
are not directly joinable as they do not have any
direct relationship represented by ‘primaryforeign key’ relationships.
Prentice Hall © 2004
34
Joining More Than Two Tables Contd.
• A typical join condition specifies a foreign key
from one table and its associated primary key in
the other table. In order to display data from
tables linked through an association table, you
may need to join three or four or even more
tables.
• However, you can join only two tables at a time.
That is, you can have only two table names in
each join condition. Thus, if you are joining two
tables then you must have at least one join
condition; if you are joining n tables then you
must have at least n-1 join conditions!
Prentice Hall © 2004
35
Joining Tables by Using Two Columns
• The diagram depicts the relationship at a
university where students enroll in course
sections.
Prentice Hall © 2004
36
Joining Tables by Using Two Columns
Contd.
• The SELECT statement that accomplishes the JOIN based on two
columns is shown below.
• This situation arises when the related tables have composite primary
key columns.
SELECT s.course_title "Course Title", e.student_ssn "Student SSN"
FROM enrollment e, section s
WHERE e.course_number = s.course_number AND
e.section_number = s.section_number;
/* Join conditions in the FROM clause */
SELECT s.course_title "Course Title", e.student_ssn "Student SSN"
FROM enrollment e JOIN section s
ON (e.course_number = s.course_number) AND
(e.section_number = s.section_number);
Prentice Hall © 2004
37
OUTER JOIN Operations
• SQL Server also supports what is called an
outer-join. This means that a row will appear in
the joined table even though there is no
matching value in the table to be joined.
• There are three types of outer joins: LEFT,
RIGHT, and FULL. They all begin with an
INNER JOIN, and then they add back some of
the rows that have been dropped.
Prentice Hall © 2004
38
OUTER JOIN OPERATIONS:Types of
OUTER JOIN Contd.
• A LEFT OUTER JOIN adds back all the rows
that are dropped from the first (left) table in the
join condition, and output columns from the
second (right) table are set to NULL.
• A RIGHT OUTER JOIN adds back all the rows
that are dropped from the second (right) table in
the join condition, and output columns from the
first (left) table are set to NULL.
• The FULL OUTER JOIN adds back all the
rows that are dropped from both the tables.
Prentice Hall © 2004
39
OUTER JOIN Operations CONTD.
• Suppose you want to know the names
of the employees regardless of
whether they have dependents or not.
You can use the OUTER JOIN as
follows:
SELECT CAST(emp_last_name As CHAR(12)) "Last
Name",
CAST(emp_first_name As CHAR(12)) "First Name",
CAST(dep_name As CHAR(12)) "Dependent",
CAST(dep_relationship As CHAR(8)) "Relationship"
FROM employee LEFT OUTER JOIN dependent ON
(emp_ssn = dep_emp_ssn);
Prentice Hall © 2004
40
OUTER JOIN Operations Contd.
Last Name
First Name
------------ -----------Bock
Douglas
Bock
Douglas
Bock
Douglas
Bock
Douglas
Bock
Douglas
Amin
Hyder
Joshi
Dinesh
Zhu
Waiman
Zhu
Waiman
Zhu
Waiman
Joyner
Suzanne
Bordoloi
Bijoy
Bordoloi
Bijoy
Bordoloi
Bijoy
Bordoloi
Bijoy
Markis
Marcia
Prescott
Sherri
(17 row(s) affected)
Dependent
-----------Deanna
Jeffery
Mary Ellen
Michelle
Rachael
NULL
NULL
Andrew
Jo Ellen
Susan
Allen
Anita
Mita
Monica
Rita
NULL
NULL
Prentice Hall © 2004
Relationship
-----------DAUGHTER
SON
SPOUSE
DAUGHTER
DAUGHTER
NULL
NULL
SON
DAUGHTER
SPOUSE
SPOUSE
DAUGHTER
SPOUSE
DAUGHTER
DAUGHTER
NULL
NULL
41
OUTER JOINS and NULL values
• Management might desire a listing of employees
with no dependents to satisfy some
governmental reporting requirement.
• We can take advantage of the fact that the
dep_name column will be NULL for employees
with no dependents, and simply add a criteria to
the WHERE clause to include employees where
the dep_name column is NULL.
Prentice Hall © 2004
42
OUTER JOINS and NULL values Contd.
SELECT CAST(emp_last_name As CHAR(12)) "Last Name",
CAST(emp_first_name As CHAR(12)) "First Name",
CAST(dep_name As CHAR(12)) "Dependent",
CAST(dep_relationship As CHAR(8)) "Relationship"
FROM employee LEFT OUTER JOIN dependent ON (emp_ssn =
dep_emp_ssn)
WHERE dep_name IS NULL;
Last Name
-----------Amin
Joshi
Markis
Prescott
First Name
-----------Hyder
Dinesh
Marcia
Sherri
Dependent
-----------NULL
NULL
NULL
NULL
Prentice Hall © 2004
Relationship
-----------NULL
NULL
NULL
NULL
43
RIGHT OUTER JOIN
SELECT CAST(emp_last_name As CHAR(12)) "Last Name",
CAST(emp_first_name As CHAR(12)) "First Name",
CAST(dep_name As CHAR(12)) "Dependent",
CAST(dep_relationship As CHAR(8)) "Relationship"
FROM dependent RIGHT OUTER JOIN employee
ON (emp_ssn = dep_emp_ssn);
Last Name
First Name
------------ -----------Bock
Douglas
Bock
Douglas
Bock
Douglas
Bock
Douglas
Bock
Douglas
Amin
Hyder
Joshi
Dinesh
Zhu
Waiman
Zhu
Waiman
Zhu
Waiman
Joyner
Suzanne
Bordoloi
Bijoy
Bordoloi
Bijoy
Bordoloi
Bijoy
Bordoloi
Bijoy
Markis
Marcia
Prescott
Sherri
(17 row(s) affected)
Dependent
-----------Deanna
Jeffery
Mary Ellen
Michelle
Rachael
NULL
NULL
Andrew
Jo Ellen
Susan
Allen
Anita
Mita
Monica
Rita
NULL
NULL
Relationship
-----------DAUGHTER
SON
SPOUSE
DAUGHTER
DAUGHTER
NULL
NULL
SON
DAUGHTER
SPOUSE
SPOUSE
DAUGHTER
SPOUSE
DAUGHTER
DAUGHTER
NULL
NULL
Prentice Hall © 2004
44
SELF-JOIN Operations
• A SELF JOIN operation is used to produce a
result table when the relationship of interest
exists among rows that are stored within a
single table.
Prentice Hall © 2004
45
SELF-JOIN Operations Contd.
SELECT CAST(e1.emp_last_name + ', ' + e1.emp_first_name As
CHAR(20)) "Supervisor",
CAST(e2.emp_last_name + ', ' + e2.emp_first_name As CHAR(20))
"Employee"
FROM employee e1 JOIN employee e2 ON (e1.emp_ssn =
e2.emp_superssn)
ORDER BY e2.emp_superssn;
Supervisor
-------------------Zhu, Waiman
Zhu, Waiman
Zhu, Waiman
Joyner, Suzanne
Joyner, Suzanne
Bordoloi, Bijoy
Bordoloi, Bijoy
Employee
-------------------Bock, Douglas
Joshi, Dinesh
Prescott, Sherri
Markis, Marcia
Amin, Hyder
Zhu, Waiman
Joyner, Suzanne
Prentice Hall © 2004
46
SUMMARY
• The JOIN operation enables you to combine
data from several tables.
• You can specify your join conditions either
in the FROM clause (newer syntax) or in
the WHERE clause (older syntax).
• You typically join tables using inner join
based on equality of columns (equijoin).
• However, you also learned about some other
type of joins such as outer join and self join
(recursive).
Prentice Hall © 2004
47