SQL Practice Logo

SQLPractice Online

INNER JOIN Deep Dive: Examples

Module: Joins & Relationships

Basic INNER JOIN - Orders with Customer Names

basic

Show order details with customer names instead of customer IDs

-- Instead of seeing customer_id (meaningless number)

SELECT order_id, customer_id, total_amount, order_date

FROM orders;

-- Use INNER JOIN to see customer names (meaningful data)

SELECT o.order_id, c.customer_name, o.total_amount, o.order_date

FROM orders o

INNER JOIN customers c ON o.customer_id = c.customer_id;

Shows orders with actual customer names instead of cryptic IDs

INNER JOIN matches orders.customer_id with customers.customer_id to combine data from both tables

All

INNER JOIN with WHERE Filter

intermediate

Find high-value orders (>$1000) with customer information

SELECT c.customer_name, o.order_id, o.total_amount, o.order_date

FROM customers c

INNER JOIN orders o ON c.customer_id = o.customer_id

WHERE o.total_amount > 1000

ORDER BY o.total_amount DESC;

Returns only orders over $1000 with customer names, sorted by amount

JOIN first combines tables, then WHERE filters the results, finally ORDER BY sorts them

All

Multiple Conditions in ON Clause

advanced

Join orders to customers but only for recent orders (last 30 days)

SELECT c.customer_name, o.order_id, o.total_amount, o.order_date

FROM customers c

INNER JOIN orders o ON c.customer_id = o.customer_id

AND o.order_date >= CURRENT_DATE - INTERVAL 30 DAY

WHERE c.status = 'active';

Shows active customers with their recent orders only

ON clause can have multiple conditions with AND/OR. This joins only recent orders, then filters for active customers

MySQL