Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
LEFT, RIGHT and INNER JOINs | Joining Tables
Intermediate SQL
course content

Course Content

Intermediate SQL

Intermediate SQL

1. Grouping
2. Nested Subqueries
3. Joining Tables
4. DDL and DML in SQL

LEFT, RIGHT and INNER JOINs

Great job! The online store has placed an order with a supplier for products they were running low on in stock, so they won't need us until the shipment arrives.

This is a great opportunity to learn something new! Throughout the past 4 chapters, you've been using the standard JOIN without knowing that there are other ways to join tables.

There are 4 main types of table joins:

  • INNER JOIN: Returns only the rows that have matching values in both tables; (This join has the same functionality as the standard JOIN you've been using so far)
  • LEFT JOIN: Returns all rows from the left table and the matching rows from the right table. If there are no matches, it returns NULL for the right table;
  • RIGHT JOIN: Returns all rows from the right table and the matching rows from the left table. If there are no matches, it returns NULL for the left table;
  • FULL JOIN: Returns all rows when there is a match in one of the tables. If there are no matches, it returns NULL for the missing values in the other table.

Before we start working with these types of joins, let's take a look at the new two tables we'll be working with. These tables contain information about courses as well as students enrolled in these courses.

courses:

enrollments:

The syntax for using these types of joins is actually simple. Instead of the familiar JOIN or INNER JOIN, just specify LEFT JOIN or any other type of JOIN.

In this case, the syntax will look like this:

From this syntax, it's clear which table will be the left table and which will be the right table.

Let's move on to the task!

Task

Write a query to retrieve a list of all courses and the students enrolled, including courses with no registered students.

You need to fetch the following columns in this order:

Use the appropriate type of JOIN to solve this task!

Task

Write a query to retrieve a list of all courses and the students enrolled, including courses with no registered students.

You need to fetch the following columns in this order:

Use the appropriate type of JOIN to solve this task!

Everything was clear?

Section 3. Chapter 5
toggle bottom row

LEFT, RIGHT and INNER JOINs

Great job! The online store has placed an order with a supplier for products they were running low on in stock, so they won't need us until the shipment arrives.

This is a great opportunity to learn something new! Throughout the past 4 chapters, you've been using the standard JOIN without knowing that there are other ways to join tables.

There are 4 main types of table joins:

  • INNER JOIN: Returns only the rows that have matching values in both tables; (This join has the same functionality as the standard JOIN you've been using so far)
  • LEFT JOIN: Returns all rows from the left table and the matching rows from the right table. If there are no matches, it returns NULL for the right table;
  • RIGHT JOIN: Returns all rows from the right table and the matching rows from the left table. If there are no matches, it returns NULL for the left table;
  • FULL JOIN: Returns all rows when there is a match in one of the tables. If there are no matches, it returns NULL for the missing values in the other table.

Before we start working with these types of joins, let's take a look at the new two tables we'll be working with. These tables contain information about courses as well as students enrolled in these courses.

courses:

enrollments:

The syntax for using these types of joins is actually simple. Instead of the familiar JOIN or INNER JOIN, just specify LEFT JOIN or any other type of JOIN.

In this case, the syntax will look like this:

From this syntax, it's clear which table will be the left table and which will be the right table.

Let's move on to the task!

Task

Write a query to retrieve a list of all courses and the students enrolled, including courses with no registered students.

You need to fetch the following columns in this order:

Use the appropriate type of JOIN to solve this task!

Task

Write a query to retrieve a list of all courses and the students enrolled, including courses with no registered students.

You need to fetch the following columns in this order:

Use the appropriate type of JOIN to solve this task!

Everything was clear?

Section 3. Chapter 5
toggle bottom row

LEFT, RIGHT and INNER JOINs

Great job! The online store has placed an order with a supplier for products they were running low on in stock, so they won't need us until the shipment arrives.

This is a great opportunity to learn something new! Throughout the past 4 chapters, you've been using the standard JOIN without knowing that there are other ways to join tables.

There are 4 main types of table joins:

  • INNER JOIN: Returns only the rows that have matching values in both tables; (This join has the same functionality as the standard JOIN you've been using so far)
  • LEFT JOIN: Returns all rows from the left table and the matching rows from the right table. If there are no matches, it returns NULL for the right table;
  • RIGHT JOIN: Returns all rows from the right table and the matching rows from the left table. If there are no matches, it returns NULL for the left table;
  • FULL JOIN: Returns all rows when there is a match in one of the tables. If there are no matches, it returns NULL for the missing values in the other table.

Before we start working with these types of joins, let's take a look at the new two tables we'll be working with. These tables contain information about courses as well as students enrolled in these courses.

courses:

enrollments:

The syntax for using these types of joins is actually simple. Instead of the familiar JOIN or INNER JOIN, just specify LEFT JOIN or any other type of JOIN.

In this case, the syntax will look like this:

From this syntax, it's clear which table will be the left table and which will be the right table.

Let's move on to the task!

Task

Write a query to retrieve a list of all courses and the students enrolled, including courses with no registered students.

You need to fetch the following columns in this order:

Use the appropriate type of JOIN to solve this task!

Task

Write a query to retrieve a list of all courses and the students enrolled, including courses with no registered students.

You need to fetch the following columns in this order:

Use the appropriate type of JOIN to solve this task!

Everything was clear?

Great job! The online store has placed an order with a supplier for products they were running low on in stock, so they won't need us until the shipment arrives.

This is a great opportunity to learn something new! Throughout the past 4 chapters, you've been using the standard JOIN without knowing that there are other ways to join tables.

There are 4 main types of table joins:

  • INNER JOIN: Returns only the rows that have matching values in both tables; (This join has the same functionality as the standard JOIN you've been using so far)
  • LEFT JOIN: Returns all rows from the left table and the matching rows from the right table. If there are no matches, it returns NULL for the right table;
  • RIGHT JOIN: Returns all rows from the right table and the matching rows from the left table. If there are no matches, it returns NULL for the left table;
  • FULL JOIN: Returns all rows when there is a match in one of the tables. If there are no matches, it returns NULL for the missing values in the other table.

Before we start working with these types of joins, let's take a look at the new two tables we'll be working with. These tables contain information about courses as well as students enrolled in these courses.

courses:

enrollments:

The syntax for using these types of joins is actually simple. Instead of the familiar JOIN or INNER JOIN, just specify LEFT JOIN or any other type of JOIN.

In this case, the syntax will look like this:

From this syntax, it's clear which table will be the left table and which will be the right table.

Let's move on to the task!

Task

Write a query to retrieve a list of all courses and the students enrolled, including courses with no registered students.

You need to fetch the following columns in this order:

Use the appropriate type of JOIN to solve this task!

Section 3. Chapter 5
Switch to desktop for real-world practiceContinue from where you are using one of the options below
We're sorry to hear that something went wrong. What happened?
some-alt