DataBolt

All Lessons

Introduction to SQL
SQL Lesson 1: Your Sample Database
SQL Lesson 2: SELECT — Reading data
SQL Lesson 3: WHERE — Filtering rows
SQL Lesson 4: AND, OR, NOT
SQL Lesson 5: BETWEEN, IN, LIKE
SQL Lesson 6: NULL — The Mystery Value
SQL Lesson 7: ORDER BY
SQL Lesson 8: LIMIT & OFFSET
SQL Lesson 9: Aggregate Functions
SQL Lesson 10: GROUP BY
SQL Lesson 11: HAVING
SQL Lesson 12: INNER JOINs
SQL Lesson 13: LEFT JOINs
SQL Lesson 14: RIGHT JOINs
SQL Lesson 15: SELF JOINs
SQL Lesson 16: UNION JOINs
SQL Lesson 17: Joining Multiple Tables
SQL Lesson 18: Subqueries
SQL Lesson 19: CTEs (WITH)
SQL Lesson 20: CASE Statements
SQL Lesson 21: Window Functions
SQL Lesson 22: String Functions
SQL Lesson 23: Date & Time Functions
SQL Lesson 24: INSERT, UPDATE, DELETE
SQL Lesson 25: CREATE TABLE & DDL
SQL Lesson 26: Indexes & Performance
SQL Lesson 27: Transactions & ACID
SQL Lesson 28: SQL Execution Order
SQL Lesson 29: 50 Practice Problems

CHAPTER 15

RIGHT JOINs

Keeping all right table rows — even when there is no match.

IN THIS CHAPTER

  • RIGHT JOIN returns ALL rows from the right table
  • Matching rows from the left table are included
  • If no match is found, NULL values are returned
  • In SQLite, RIGHT JOIN is not supported directly
  • We simulate RIGHT JOIN using LEFT JOIN by reversing table order

🌟 Think of it this way: You want all orders, even if some customers are missing. RIGHT JOIN keeps all rows from the right table (orders). In SQLite, we achieve this by reversing the tables and using LEFT JOIN.

15.1 - RIGHT JOIN (Simulated)

Returns all orders, including those that may not have matching customers (simulated using LEFT JOIN).

SQL

SELECT o.order_id, c.name, o.total
FROM orders o
LEFT JOIN customers c
ON c.customer_id = o.customer_id
ORDER BY o.order_id;

15.2 - RIGHT JOIN (Orders without Customers)

Find orders that do NOT have matching customers using LEFT JOIN + IS NULL.

SQL

SELECT o.order_id
FROM orders o
LEFT JOIN customers c
ON c.customer_id = o.customer_id
WHERE c.customer_id IS NULL;

Pro Tip: RIGHT JOIN is rarely used in practice. Most developers prefer LEFT JOIN because it is more readable and widely supported.

Included Tables in Exercise

Table: customers

customer_idnamecitycountry
1AnanyaHyderabadIndia
2RohanBangaloreIndia
3SamMumbaiIndia
4LisaLondonUK
5RaviDelhiIndia

Table: orders

order_idcustomer_idorder_datetotalstatus
112024-01-1579999completed
222024-01-203498completed
312024-02-1065000completed
432024-02-14499pending
522024-03-01248completed
652024-03-152999shipped
712024-04-05149completed

Exercise 👇

Exercise:

Tasks

1.👉Get all orders with customer names (include missing customers)
2.Show order totals with orders
3.Orders above 5000
4.Only completed orders
5.Sort orders by amount
6.Count matches per order
7.Total value per order
8.Orders with at least one matching customer
9.Latest date per order
10.Average value per order
11.Join orders with order_items
12.Get product names per order
13.Count orders per product
14.Orders that include Electronics products
Stuck? Read this task's