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 13

INNER JOINs

Aggregating by category — the analytics workhorse.

IN THIS CHAPTER

  • Why JOINs exist and what problem they solve
  • INNER JOIN — only rows with a match in both tables
  • LEFT JOIN — all left rows, matched right rows (NULLs where no match)
  • RIGHT JOIN — the mirror of LEFT JOIN
  • SELF JOIN — a table joined to itself
  • Finding rows with no match using LEFT JOIN + IS NULL

🌟 Think of it this way: Two spreadsheets: one with customer names+IDs, another with order amounts+customer IDs. A JOIN merges them so you can see 'Ananya spent ₹1,45,148'. Without JOINs, you would have to manually cross-reference the sheets.

13.1 - INNER JOIN

Returns rows only where there is a match in BOTH tables. Lisa (no orders) is excluded.

SQL

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

13.2 - INNER JOIN (Customers with High Orders)

Find customers and their order details only for orders greater than 3000. This shows how INNER JOIN works with filtering conditions.

SQL

SELECT c.name, o.order_id, o.total
FROM customers c
INNER JOIN orders o
ON c.customer_id = o.customer_id
WHERE o.total > 3000;

Pro Tip: This is commonly used in business to identify high-value customers.

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 customer names with their order IDs
2.Show order totals with customer names
3.Orders above 5000
4.Only completed orders
5.Sort by order amount
6.Count orders per customer
7.Total spending per customer
8.Customers with more than 1 order
9.Latest order date per customer
10.Average order value per customer
11.Join 3 tables (customers + orders + order_items)
12.Product names with customer orders
13.Total quantity ordered per product
14.Customers who bought Electronics
15.Highest order per customer
Stuck? Read this task's