Skip to main content
Back to Documentation
DocsJoinsINNER JOIN

INNER JOIN

Joins

Concept

INNER JOIN combines rows from two tables where the join condition is true. If a row in either table has no match in the other, it is excluded from the result. This makes INNER JOIN the most restrictive join type — you only get rows that exist in both tables.

The join condition is specified with ON followed by a Boolean expression, most commonly an equality between a foreign key and a primary key. You can join on multiple conditions using AND, and you can chain multiple INNER JOINs to bring in data from three or more tables.

INNER JOIN is the default join type — writing just JOIN without a prefix means INNER JOIN. It is the most commonly used join in practice because most queries need rows that have matching records on both sides (e.g., orders and their customers, products and their categories).

Syntax

-- Basic inner join
SELECT o.order_id, c.first_name, c.last_name
FROM orders o
INNER JOIN customers c ON c.customer_id = o.customer_id;

-- Multi-table join
SELECT
  o.order_id,
  c.first_name,
  p.name AS product
FROM orders o
INNER JOIN customers c ON c.customer_id = o.customer_id
INNER JOIN order_items oi ON oi.order_id = o.order_id
INNER JOIN products p ON p.product_id = oi.product_id;

Practical Example

Using the ecommerce schema (customers, orders, order_items, products)

-- Order details with customer and product info
SELECT
  o.order_id,
  o.order_date,
  c.first_name || ' ' || c.last_name AS customer,
  p.name AS product,
  oi.quantity,
  oi.unit_price,
  oi.quantity * oi.unit_price AS line_total
FROM orders o
INNER JOIN customers c ON c.customer_id = o.customer_id
INNER JOIN order_items oi ON oi.order_id = o.order_id
INNER JOIN products p ON p.product_id = oi.product_id
ORDER BY o.order_date DESC;

Common Pitfalls & Tips

  • 1If the join condition is wrong or missing, you get a Cartesian product (every row paired with every other row). Always double-check your ON clause.
  • 2INNER JOIN excludes rows with no match. If you need all customers including those without orders, use LEFT JOIN instead.
  • 3Joining on non-indexed columns can be very slow on large tables. Ensure foreign key columns are indexed.
Practice INNER JOIN queries