D427 Focus Clinic
Joins
A join is a SELECT statement that combines data from two tables, known as
the left table and right table, into a single result.
Let’s visualize
how joins
work and
what the
results should
look like.
Inner Join
An INNER JOIN returns only the rows where there is a match between the two tables based
on the join condition.
Scenario:
We want to list the creator names and their corresponding content titles, but only for
creators with a valid content in the Content Table.
Query:
SELECT Creator.name, Content.title
FROM Creator
INNER JOIN Content ON Creator.creator_id = Content.creator_id;
Inner Join
Left Join
A LEFT JOIN returns all rows from the left table (Creator Table), along with the matched
rows from the right table (Content Table). If no match exists, NULL values are returned for
columns from the right table.
Scenario:
We want to list all creators and their content titles, even if they haven’t created any content.
If a creator has no content, the content column will show NULL.
Query:
SELECT Creator.name, Content.title
FROM Creator
LEFT JOIN Content ON Creator.creator_id = Content.creator_id;
Left Join
Right Join
A RIGHT JOIN returns all rows from the right table (Content Table), along with the matched rows
from the left table (Creator Table). If no match exists, NULL values are returned for columns
from the left table.
Scenario:
We want to list all content titles and their corresponding creator names, even if the content
doesn’t have an associated creator in the Creator Table.
Query:
SELECT Creator.name, Content.title
FROM Creator
RIGHT JOIN Content ON Creator.creator_id = Content.creator_id;
Right Join
Full Join
A FULL JOIN (or FULL OUTER JOIN) returns all rows when there is a match in one of the
tables. If there is no match, the result will contain NULL values for non-matching rows from
both tables.
Note: MySQL doesn’t support the FULL OUTER JOIN directly. However, you can achieve
the same result by combining a LEFT JOIN and RIGHT JOIN and using UNION.
Scenario:
We want to list all creators and all content, whether they are linked or not. If a creator has no
content, or if content has no creator, we still want to see them in the result.
Full Join
Common Joins
Join Type Description
Returns only the rows where there is a match in both tables (content linked to a
INNER JOIN
creator).
Returns all rows from the left table (Creator) and matched rows from the right table
LEFT JOIN
(Content). Non-matching rows in the right table are NULL.
Returns all rows from the right table (Content) and matched rows from the left table
RIGHT JOIN
(Creator). Non-matching rows in the left table are NULL.
Returns all rows when there is a match in one of the tables. Non-matching rows in
FULL JOIN either table are NULL. MySQL simulates this with a combination of LEFT JOIN and
RIGHT JOIN using UNION.
Other Joins
Join Type Description
Returns rows where there is a match in both tables
EQUI JOIN
based on equality.
Joins a table with itself, used to find relationships within
SELF JOIN
the same table.
Returns the Cartesian product of two tables, combining
CROSS JOIN each row of the first table with every row of the second
table.
name title
Alice Johnson Understanding Databases
Alice Johnson Advanced SQL Tips
Alice Johnson Intro to Media Production
Alice Johnson The Role of Producers
Alice Johnson How to Host a Podcast
Bob Smith Understanding Databases
Cross Join
Bob Smith Advanced SQL Tips
Bob Smith Intro to Media Production
Bob Smith The Role of Producers
Bob Smith How to Host a Podcast
• Every creator is paired with every Carla Brown Understanding Databases
content title in a CROSS JOIN. Carla Brown Advanced SQL Tips
• Since there are 5 creators and 5 Carla Brown Intro to Media Production
content items, the result contains 5 * 5 Carla Brown The Role of Producers
= 25 rows. Carla Brown How to Host a Podcast
Dan White Understanding Databases
• This is a Cartesian product, meaning Dan White Advanced SQL Tips
every possible combination of creators Dan White Intro to Media Production
and content is listed. Dan White The Role of Producers
Dan White How to Host a Podcast
Eva Green Understanding Databases
Eva Green Advanced SQL Tips
Eva Green Intro to Media Production
Eva Green The Role of Producers
Eva Green How to Host a Podcast
ID Name ManagerID
Self Join 15 Amanda Thomas 22
21 Angela Allen 8
The Employee table has the following columns: 22 Brandon Young NULL
• ID - integer, primary key
• Name - variable-length string 18 Christopher Lewis 17
• ManagerID - integer 12 Daniel Hernandez 30
• Write a SELECT statement to show a list of all
employees' first names and their managers’ 5 David Wilson 30
names. List only employees that have a
manager. Order the results by Employee
name. Use aliases to give the result columns
30 Dylan Carter NULL
distinctly different names, like "Employee"
and "Manager". 4 Emily Davis NULL
23 Hannah King 4
7 James Miller 8
SELECT E.Name AS Employee, M.Name AS Manager
FROM Employee E
INNER JOIN Employee M ON E.ManagerId = M.ID
ORDER BY E.Name;
Employee E EmployeeEmployee ManagerIDManager
ManagerName
AmandaAmanda
ThomasThomas Brandon Young
22 Brandon Young
Angela Angela
Allen Allen 8 Sarah
Sarah Lee Lee
Christopher Lewis Lewis Karen
Christopher 17 Karen White
White
Daniel Hernandez 30 Dylan
Daniel Hernandez Dylan Carter
Carter
David Wilson
David Wilson 30 Dylan
Dylan Carter
Carter
HannahHannah
King King 4 Emily
Emily DavisDavis
James Miller
James Miller 8 Sarah
Sarah Lee Lee
Jane Smith 8 Sarah Lee
Jane Smith Sarah Lee
John Doe 4 Emily Davis
John Doe Emily Davis
Joshua Jackson 4 Emily Davis
Joshua Jackson Emily Davis
Employee M
Yes… There is an order
SELECT (Required)
FROM (Required)
JOIN (Combines 2 tables based on a column)
WHERE (Filtering Rows)
GROUP BY ( Grouping values by unique categories – or groups-)
HAVING (Filtering the categories from the GROUP BY – ONLY used with GROUP BY)
ORDER BY (Sort values in Ascending –ASC- or descending –DESC- order)
LIMIT (Return the Top N values)
Which rows will always be
included in the result set if Table
A is inner joined with Table B?
Which rows will always be
included in the result set if Table
A is right joined with Table B?
Activity
Activity
Activity
Activity
• D427 : Implements Joins and
Aggregate Functions
• Problem 4
• Problem 8
Activity
Q&A
Joins & Aggregate
Functions
Next Cohort