SQL

INNER JOIN — Match Rows in Both Tables

admin by @admin ADMIN
53m ago
May 31, 2026
Public
0 0 up · 0 down Sign in to vote
Returns rows where the join condition matches in BOTH tables. The default JOIN. Use table aliases (`u`, `o`) for readability when joining 3+ tables.
SQL
Raw
-- Each order with its user's email
SELECT o.id          AS order_id,
       o.amount,
       o.created_at,
       u.email
FROM   orders o
INNER  JOIN users u ON u.id = o.user_id
WHERE  o.amount > 100
ORDER  BY o.created_at DESC;

-- Multi-table join — orders + users + products
SELECT o.id          AS order_id,
       u.email,
       p.name        AS product,
       oi.qty,
       oi.unit_price
FROM   orders o
JOIN   users u           ON u.id = o.user_id
JOIN   order_items oi    ON oi.order_id = o.id
JOIN   products p        ON p.id = oi.product_id
WHERE  o.created_at >= NOW() - INTERVAL '7 days';

-- USING() — shorthand when both columns have the same name
SELECT u.name, o.id
FROM   users u
JOIN   orders o USING (user_id);
Tags

Save your own code snippets

Create a free account and build your private vault. Share publicly whenever you want.