You are on page 1of 42

MULTIPLE JOIN

IN SQL SERVER
RECAP OF THE
JOIN TYPES
INNER JOIN
LEFT JOIN
RIGHT JOIN
FULL JOIN
CROSS JOIN
MULTIPLE JOIN IN SQL
■ Each query may comprise zero, one, or more joins.
■ A multiple join is a use of more than one join in a single query.
The joins used may be all of the same type, or their types can
differ.

■ The query invokes two INNER JOINs in order to join three


tables: vehicle, person and color. Only those records that have a
match in each table will be returned. First, take a look at the sets
of data that were joined.
MULTIPLE JOIN IN SQL
■ There are three sets of data that correspond to three
tables in the database: vehicle, color and person,
represented below.
OBSERVATION ON THE VALUES OF A
TABLE
■ You can see that each vehicle in the
vehicle table has a color, except for
motorcycle.
■ Each vehicle has an owner assigned,
except for bicycle, which has no owner.
■ One of the colors (blue) in the color
table is not assigned to any vehicle.
■ Further, motorcycle does not have a
color available in the database.
■ On the other hand, bicycle has an
assigned color, but no owner.
■ Lastly, the person named Miller is
assigned no vehicle.
QUERY RESULT
■ We used multiple-join to retrieve only those vehicles assigned
both a color and owner. The vehicle table has the color_id
column which identifies the color in the color table, as well as
the person_id column which identifies the person in the person
table.
■ It turns out that only two records match the criteria defined
by the two inner joins.
PICTORIAL REPRESENTATION
REMEMBER
■ Note that all JOIN operations are performed from left to right.
■ In step one, the tables in the first JOIN are matched (tables
vehicle and color). As a result, an intermediate table is created.
■ In step two, this intermediate table (treated as the left table) is
joined with another table (table person) using the second
JOIN.
■ Remember that a single JOIN of any type produces a single
intermediate table (commonly called a derived table) during a
multi-join query.
Mixed Left and Right Join With Inner Join
■ Suppose we want to query our database for all people who own a
colored vehicle or don’t own a vehicle at all.
■ We would start with the person table and join it with the vehicle table
using a LEFT JOIN. In that case, the LEFT JOIN would match each
record from the person table with a record from the vehicle table, and
for any person for whom a matching record was not found, it would
fill missing values with NULLs.
■ This join will produce a list of all people in the database with any
associated vehicle data, even if they do not own one.
■ But we’re interested in seeing only vehicles with colors assigned. This
means we must use an INNER JOIN on tables vehicle and color.
MIXED JOINS
Query our database for all people who own a colored vehicle or don’t own a vehicle
at all.

The INNER JOIN skipped those results which did not match in both tables,
i.e. in the derived table (created by joining tables person and vehicle) and the
color table. How can we solve this problem?
SOLUTION
■ The following query
presents one of a few
possible solutions. Here
the derived table returns
vehicles with colors
only, and is then
RIGHT JOINed with
the person table in order
to obtain all of the
people.
SOLUTION EXPLAINED
■ Now we have a list of all the people: those with colored
vehicles and those without vehicles.
■ We started with an INNER JOIN of tables vehicle and color.
Each vehicle included in the derived table must have a color
assigned, which is why this join type is appropriate.
■ Having selected the colored vehicles, we could now use a
RIGHT JOIN on the derived table with the person table,
which is how we obtained people who were not vehicle
owners alongside those (from the derived table) who owned
a colored vehicle.
ANOTHER METHOD
■ Another method to solve this problem is to use a LEFT JOIN
on the person table and a subquery in which we used an
INNER JOIN on tables vehicle and color.
■ Take a look at the query below.
Mixed JOINs with Full JOIN

■ The query above matches the records from three tables: person, vehicle and color
in such a way that even records without a match in the other two tables will
appear in the result table.
■ Empty columns will be filled with NULL values. That is why the query returns all
people regardless of whether they have a vehicle, all vehicles regardless of
whether they have a color assigned, and all colors regardless of whether they are
assigned to any vehicle.
RESULT
PICTORIAL REPRESENTATION
■ We used full joins to join all
records, even those that do not
match. Remember that full joins
return all records, while inner
joins return only those that
match.
■ The picture below explains the
sequence in which tables were
joined.
FULL JOIN WITH ANOTHER JOIN
■ FULL JOIN can also appear in a query
with another join type, thus creating a
multiple-join with mixed types. The
query below makes use of a FULL JOIN
with an INNER JOIN.
■ This query enables us to retrieve a list of
all people, whether or not they are
vehicle owners, and all vehicles that have
a color assigned.
■ Here’s how the two joins work:
TABLE FORMAT OUTPUT
■ First, tables vehicle and color are combined using an INNER
JOIN. Next the derived table is combined with the person
table using a FULL JOIN. Here’s the result:
UNION VS UNION ALL
■ In SQL Server you have the ability to combine multiple
datasets into one comprehensive dataset by using the UNION
vs. UNION ALL operators.
■ There is a big difference in how these work as well as the
final result set that is returned, but basically these commands
join multiple datasets that have similar structures into one
combined dataset.
UNION VS UNION ALL
■ UNION - this operation will allow you to join multiple
datasets into one dataset and will remove any duplicates
that exist. Basically it is performing a DISTINCT operation
across all columns in the result set.
 
■ UNION ALL - this operation again allows you to join
multiple datasets into one dataset, but it does not remove any
duplicate rows. Because this does not remove duplicate rows
this process is faster, but if you don't want duplicate records
you will need to use the UNION operator instead.
RULES TO UNION DATA
■ Each query must have the same number of columns
■ Each column must have compatible data types
■ Column Order must also match in both the Select statement
■ We can define Group By and Having clause with each
Select statement. It is not possible to use them with the result
set
■ We cannot use Order By clause with individual Select
statement. We can use it with result set generated from the
Union of both Select statements
Syntax

■ The syntax for the Union vs Union All operators in SQL is as follows:
■ SELECT Column1, Column2, … ColumnN
FROM <table>
[WHERE conditions]
[GROUP BY Column(s]]
[HAVING condition(s)]
UNION
SELECT Column1, Column2, … ColumnN
FROM table
[WHERE condition(s)];
ORDER BY Column1,Column2…
SQL UNION
SQL UNION
UNION ALL
SUMMARY
Union

■ For example, the table ‘A’ has 1,2, and 3 and the table ‘B’
has 3,4,5.
Union All

■ The Union operator combines the results of two or more queries into a single
result set that includes all the rows that belong to all queries in the Union. In
simple terms, it combines the two or more row sets and keeps duplicates.
INTERSECT OPERATOR

■ The interest operator keeps the rows that are common to all
the queries

■ For the same dataset from the aforementioned example, the


intersect operator output is given below
EXCEPT OPERATOR
■ The EXCEPT operator lists the rows in the first that are
NOT in the second.

■ For the same dataset from the aforementioned example, the


Except operator output is given below
RULES TO APPLY TO ALL SET OF
OPERATOR
■ Expressions in each row or the number of columns that are
defined in each query must have the same order
■ Subsequent SQL statement row sets must match the data type of
the first query
■ Parentheses are allowed to construct other set operators in the
same statement
■ It possible to have an ORDER BY clause, but that should be the
last statement of the SQL
■ GROUP BY and HAVING clauses can be applied to the
individual query
Note:
■ All of these Set operators remove duplicates, except for the Union All
operator
■ The output column names are referred from the first query i.e. when we
run the SELECT statements with any of the Set operators and result set of
each of the queries may have different column names, so the result of the
SELECT statement refers the column names from the first query in the
operation.
■ SQL JOIN is more often used combine columns from multiple related
tables whereas SET Operators combines rows from multiple tables.
■ When the expression types are the same but differ in precision, scale, or
length, the result is determined based on the same rules for combining
expressions
SEATWORK
■ APPLYING ALL THE OPERATORS YOU HAVE
LEARNED, CONSIDER THE TABLE BELOW:

SUPPLIERS TABLE ORDERS TABLE


WHAT IS THE OUTPUT OF THE CODE
BELOW?
A.

SUPPLIERS TABLE

ORDERS TABLE
WHAT IS THE OUTPUT OF THE CODE
BELOW?
B.

SUPPLIERS TABLE

ORDERS TABLE
WHAT IS THE OUTPUT OF THE CODE
BELOW?
C. D.

EXCEPT

ORDERS TABLE
SUPPLIERS TABLE
WHAT IS THE OUTPUT OF THE CODE
BELOW?
C. D.

You might also like