Skip to content
Quantoros

How do you join on multiple columns?

SQLLevel: IntermediateJOINs Deep Dive

How do you join on multiple columns?

Hints
  • Write the join keys and relationship cardinality.
  • What happens to duplicates and unmatched rows? Check WHERE versus ON.

Answer

Use AND in the ON clause.

SELECT *
FROM sales_2024 s
JOIN sales_budget b
  ON s.region = b.region
 AND s.product_id = b.product_id
 AND s.quarter = b.quarter;

Source and licence: SQL Interview Questions — Arda Kocadoru · MIT

Related questions