Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Exploring LEFT JOIN | Core Join Types and Their Applications
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
SQL Joins in Depth

bookExploring LEFT JOIN

When you want to see all the records from one table, even if there are no related records in another, you use a LEFT JOIN. This type of join is especially useful when you need a complete list of entities—such as customers—and you want to include related data—like their orders—if it exists. For instance, if you want to know which customers have placed orders and which have not, a LEFT JOIN will let you see every customer, showing order details where available and leaving the order fields empty where there are none.

12345
-- List all customers and their order amounts (if any) SELECT c.customer_id, c.name, o.order_id, o.amount FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;
copy

When you use a LEFT JOIN, you may notice that some columns in your result contain NULL values. This happens when there is no corresponding record in the right table. In the example above, customers who have not placed any orders will appear in the list, but the order-related columns (order_id, amount) will show as NULL. This is SQL's way of indicating "no data" for those fields, helping you easily spot customers without orders.

123456
-- Show each customer and the total amount of their orders (NULL if none) SELECT c.name, SUM(o.amount) AS total_amount FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.name;
copy

1. What does LEFT JOIN return for rows in the left table with no matching rows in the right table?

2. How are NULL values produced in LEFT JOIN results?

3. Complete the following query to show all customers and their order dates, including customers with no orders:

question mark

What does LEFT JOIN return for rows in the left table with no matching rows in the right table?

Select the correct answer

question mark

How are NULL values produced in LEFT JOIN results?

Select the correct answer

question-icon

Complete the following query to show all customers and their order dates, including customers with no orders:

SELECT c.name, FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;
Alice Smith|2024-06-01
Alice Smith|2024-06-03
Bob Johnson|2024-06-02
Charlie Lee|NULL
Diana King|2024-06-04
Eve Adams|NULL

Натисніть або перетягніть елементи та заповніть пропуски

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 3

Запитати АІ

expand

Запитати АІ

ChatGPT

Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат

bookExploring LEFT JOIN

Свайпніть щоб показати меню

When you want to see all the records from one table, even if there are no related records in another, you use a LEFT JOIN. This type of join is especially useful when you need a complete list of entities—such as customers—and you want to include related data—like their orders—if it exists. For instance, if you want to know which customers have placed orders and which have not, a LEFT JOIN will let you see every customer, showing order details where available and leaving the order fields empty where there are none.

12345
-- List all customers and their order amounts (if any) SELECT c.customer_id, c.name, o.order_id, o.amount FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;
copy

When you use a LEFT JOIN, you may notice that some columns in your result contain NULL values. This happens when there is no corresponding record in the right table. In the example above, customers who have not placed any orders will appear in the list, but the order-related columns (order_id, amount) will show as NULL. This is SQL's way of indicating "no data" for those fields, helping you easily spot customers without orders.

123456
-- Show each customer and the total amount of their orders (NULL if none) SELECT c.name, SUM(o.amount) AS total_amount FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id GROUP BY c.name;
copy

1. What does LEFT JOIN return for rows in the left table with no matching rows in the right table?

2. How are NULL values produced in LEFT JOIN results?

3. Complete the following query to show all customers and their order dates, including customers with no orders:

question mark

What does LEFT JOIN return for rows in the left table with no matching rows in the right table?

Select the correct answer

question mark

How are NULL values produced in LEFT JOIN results?

Select the correct answer

question-icon

Complete the following query to show all customers and their order dates, including customers with no orders:

SELECT c.name, FROM customers c LEFT JOIN orders o ON c.customer_id = o.customer_id;
Alice Smith|2024-06-01
Alice Smith|2024-06-03
Bob Johnson|2024-06-02
Charlie Lee|NULL
Diana King|2024-06-04
Eve Adams|NULL

Натисніть або перетягніть елементи та заповніть пропуски

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

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 1. Розділ 3
some-alt