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
course content

Зміст курсу

Intermediate SQL

LEFT, RIGHT and INNER JOINsLEFT, 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:

course_id course_name description
1 Introduction to Programming Learn the basics of programming using Python.
2 Data Structures and Algorithms In-depth study of data structures and algorithms.
3 Web Development Learn how to build web applications using HTML, CSS, and JavaScript.
4 Database Management Understand database concepts and SQL.
... ... ...
11 DevOps Fundamentals Learn the principles and practices of DevOps.

enrollments:

enrollment_id student_name course_id enrollment_date
1 Alice Johnson 1 2023-01-15T00:00:00Z
2 Bob Brown 2 2023-01-20T00:00:00Z
3 Charlie Davis 1 2023-01-25T00:00:00Z
4 David Evans 2023-02-01T00:00:00Z
... ... ... ...
20 Tina Upton 7 2023-05-01T00:00:00Z

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!

Завдання

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!

Все було зрозуміло?

Секція 3. Розділ 5
toggle bottom row
course content

Зміст курсу

Intermediate SQL

LEFT, RIGHT and INNER JOINsLEFT, 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:

course_id course_name description
1 Introduction to Programming Learn the basics of programming using Python.
2 Data Structures and Algorithms In-depth study of data structures and algorithms.
3 Web Development Learn how to build web applications using HTML, CSS, and JavaScript.
4 Database Management Understand database concepts and SQL.
... ... ...
11 DevOps Fundamentals Learn the principles and practices of DevOps.

enrollments:

enrollment_id student_name course_id enrollment_date
1 Alice Johnson 1 2023-01-15T00:00:00Z
2 Bob Brown 2 2023-01-20T00:00:00Z
3 Charlie Davis 1 2023-01-25T00:00:00Z
4 David Evans 2023-02-01T00:00:00Z
... ... ... ...
20 Tina Upton 7 2023-05-01T00:00:00Z

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!

Завдання

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!

Все було зрозуміло?

Секція 3. Розділ 5
toggle bottom row
some-alt