Use SQL JOIN types correctly
INNER, LEFT, RIGHT, FULL, and CROSS joins combine rows from multiple tables. Learn when to use each and how to avoid common pitfalls.
- 1
JOIN types: INNER (matching rows only), LEFT (all left + matching right, NULL for non-matches), RIGHT (all right + matching left), FULL (all rows from both, NULL for non-matches), CROSS (Cartesian product). Most common: INNER and LEFT. RIGHT is rarely used (rewrite as LEFT). CROSS is for generating combinations.
- 2 SQL Formatter →
Format queries with JOINs to verify the join conditions. A missing ON clause (except CROSS JOIN) is a common error. Format makes the join structure visible: which tables, what type, what condition. Use table aliases for readability.
- 3 sql-format → word-count
Use the SQL format workflow to format complex multi-join queries. This ensures consistent aliasing and indentation, making the join logic clear and debuggable.
Frequently asked questions
- INNER vs LEFT JOIN?
- INNER JOIN returns only rows that match in both tables. LEFT JOIN returns all rows from the left table, with NULL for non-matching right table columns. Use INNER when you only want matches; LEFT when you want all left rows regardless of matches (e.g., all users and their optional orders).
- Can I join more than two tables?
- Yes — chain JOINs: SELECT * FROM a JOIN b ON a.id = b.a_id JOIN c ON b.id = c.b_id. Each JOIN adds one table. The optimizer determines the join order, but you can hint with leading tables. Be careful with LEFT JOINs — the order matters for NULL propagation.
Workflows that solve this
Learn more
Want to understand why?