Skip to content

Join Strategies

Database

Master SQL joins: inner, left, right, full, cross, and self joins.

Official Site
01

Overview

INNER JOIN: matching rows only. LEFT JOIN: all left + matching right. RIGHT JOIN: all right + matching left. FULL JOIN: all from both. CROSS JOIN: Cartesian product. SELF JOIN: join table to itself. Use JOINs to avoid N+1 queries.

bash
SELECT u.name,o.total FROM users u INNER JOIN orders o ON o.user_id=u.id;
SELECT u.name,o.total FROM users u LEFT JOIN orders o ON o.user_id=u.id;
-- Self join: employee-manager
SELECT e.name,m.name AS mgr FROM employees e LEFT JOIN employees m ON e.mgr_id=m.id;
-- Aggregation with join
SELECT u.name,COUNT(o.id) FROM users u LEFT JOIN orders o ON o.user_id=u.id GROUP BY u.id;
02

Best Practices

Prefer JOINs over N+1 queries. Use LEFT JOIN to preserve all left rows. Alias tables for readability. Join on indexed columns (FKs). Avoid CROSS JOIN (expensive). Use EXISTS over IN for subqueries. Filter in WHERE not HAVING when possible.

Ready to try Join Strategies?

Visit the official site for the latest version and full documentation.

Visit Join Strategies