πŸ”— JoinsintermediateCommonly used

How to use INNER JOIN in SQL

Return only rows that have matching records in both tables.

Table schema used in examplesβ€” users and orders tables
CREATE TABLE users (
  id      INTEGER PRIMARY KEY,
  name    TEXT,
  email   TEXT,
  country TEXT
);

CREATE TABLE orders (
  id         INTEGER PRIMARY KEY,
  user_id    INTEGER REFERENCES users(id),
  total      DECIMAL,
  status     TEXT,
  created_at TIMESTAMP
);
Basic INNER JOINAll databases
SELECT
  u.name,
  u.email,
  o.total,
  o.status
FROM users u
INNER JOIN orders o ON u.id = o.user_id;

Returns only users who have at least one order. Users with no orders are excluded.

When to use: When you only want rows that have a match in both tables.

JOIN with WHERE filterAll databases
SELECT
  u.name,
  o.total,
  o.created_at
FROM users u
INNER JOIN orders o ON u.id = o.user_id
WHERE o.total > 100
  AND o.status = 'completed';

Combines JOIN with filtering. The WHERE clause applies after the join.

JOIN with ORDER BY and LIMITAll databases
SELECT
  u.name,
  SUM(o.total) AS total_spent
FROM users u
INNER JOIN orders o ON u.id = o.user_id
GROUP BY u.id, u.name
ORDER BY total_spent DESC
LIMIT 10;

Top 10 customers by total spend β€” joins users and orders, aggregates, and limits results.

JOIN three tablesAll databases
SELECT
  u.name,
  o.total,
  p.name AS product_name
FROM users u
INNER JOIN orders o       ON u.id = o.user_id
INNER JOIN order_items oi ON o.id = oi.order_id
INNER JOIN products p     ON oi.product_id = p.id;

Chain multiple JOINs to connect three or more tables.

Common Mistakes

  • β†’Forgetting the ON clause β€” results in a cartesian product (every row Γ— every row)
  • β†’Not using table aliases β€” queries become hard to read with long table names
  • β†’INNER JOIN vs LEFT JOIN β€” INNER excludes non-matching rows, LEFT keeps them
← Back to SQL Reference