Professional Documents
Culture Documents
Lesson 12 MULTIPLE-JOIN
Lesson 12 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 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
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.