DataBolt

SQL LEARNING PLATFORM

13

CHAPTER 13

RIGHT JOINs

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

6

Learning Blocks

SQL

Interactive Queries

In This Chapter

Concepts you'll master

1

RIGHT JOIN returns ALL rows from the right table

2

Matching rows from the left table are included

3

If no match is found, NULL values are returned

4

In SQLite, RIGHT JOIN is not supported directly

5

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 QUERY
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 QUERY
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.

Exercise

Practice your SQL skills

SQL EDITOR
Press semicolon (;) to auto-run query

Tasks

Complete all SQL challenges

1

Get all orders with customer names (include missing customers)

Current Task
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

Need help solving this task?