Exploring 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;
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;
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:
Дякуємо за ваш відгук!
Запитати АІ
Запитати АІ
Запитайте про що завгодно або спробуйте одне із запропонованих запитань, щоб почати наш чат
Чудово!
Completion показник покращився до 4.76
Exploring 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;
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;
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:
Дякуємо за ваш відгук!