SQL Keywords

SQL JOIN

What is SQL JOIN?

SQL JOIN merges rows from two or more tables based on a related column or logical relationship.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL JOIN: PostgreSQL, MySQL, MariaDB, SQL Server, Oracle, SQLite, Snowflake, BigQuery, Redshift and most ANSI-compliant databases.

SQL JOIN Full Explanation

SQL JOIN is a relational operation that horizontally combines result sets. It evaluates each row in one table against rows in another (or several others) and returns a single row set that contains columns from all participating tables when the join condition evaluates to TRUE. The ANSI-standard defines several join types: • INNER JOIN – returns only matching rows. • LEFT (OUTER) JOIN – returns all rows from the left table plus matching rows from the right. Non-matches in the right table appear as NULLs. • RIGHT (OUTER) JOIN – mirror of LEFT JOIN. • FULL (OUTER) JOIN – returns matches plus non-matches from both sides, filling NULLs where data is missing. • CROSS JOIN – Cartesian product of the two tables (every row with every row). • SELF JOIN – joins a table to itself (usually with aliases). • NATURAL JOIN – automatically matches columns with identical names (not recommended for production because schema changes can break queries). Join processing order, indexes, data volume and predicate selectivity strongly influence performance. Always use explicit JOIN ... ON syntax (introduced in SQL-92) rather than listing tables in FROM and moving predicates to WHERE; it is clearer and avoids accidental CROSS joins. Watch out for duplicate rows caused by one-to-many relationships, and remember that OUTER joins preserve NULLs, so downstream filters in WHERE clauses can unintentionally turn them into INNER joins.

SQL JOIN Syntax

SELECT column_list
FROM table1
     [INNER | LEFT | RIGHT | FULL | CROSS] JOIN table2
     ON table1.column_name = table2.column_name
[WHERE additional_predicates]
[GROUP BY ...]
[ORDER BY ...];

SQL JOIN Parameters

Example Queries Using SQL JOIN

-- 1. Inner join customers and orders
SELECT c.id, c.name, o.order_date
FROM customers c
INNER JOIN orders o ON o.customer_id = c.id;

-- 2. Left join to list customers with or without orders
SELECT c.id, c.name, o.id AS order_id
FROM customers c
LEFT JOIN orders o ON o.customer_id = c.id;

-- 3. Self join to find employees and their managers
SELECT e.name  AS employee,
       m.name  AS manager
FROM employees e
JOIN employees m ON e.manager_id = m.id;

-- 4. Full outer join to reconcile two inventory tables
SELECT COALESCE(a.sku, b.sku) AS sku,
       a.qty  AS qty_a,
       b.qty  AS qty_b
FROM inventory_2023 a
FULL JOIN inventory_2024 b USING (sku);

Expected Output Using SQL JOIN

  • Each query returns a combined result set containing columns from all referenced tables
  • Rows appear once per successful join match, or (for OUTER joins) once per preserved row with NULLs filling missing side columns
  • No data is modified

Use Cases with SQL JOIN

  • Retrieve fact and dimension data in star schemas.
  • Combine user records with activity logs for analytics.
  • Build reports that require attributes stored in multiple tables.
  • Reconcile data between old and new tables during migrations.
  • Perform hierarchical lookups via self joins.

Common Mistakes with SQL JOIN

  • Omitting the ON clause and accidentally creating a CROSS join.
  • Filtering NULL-producing side of an OUTER join in the WHERE clause, effectively turning it into an INNER join.
  • Forgetting table aliases, leading to ambiguous column references.
  • Expecting NATURAL JOIN to be stable after schema changes.
  • Joining on non-indexed columns, causing slow full table scans.

Related Topics

INNER JOIN, LEFT JOIN, RIGHT JOIN, FULL JOIN, CROSS JOIN, ON clause, USING clause, subqueries, UNION, set operations

First Introduced In

SQL-92

Frequently Asked Questions

What is the fastest type of JOIN?

Typically INNER JOIN is fastest because it eliminates non-matching rows early. However, with correct indexes, LEFT and RIGHT joins can be nearly as fast.

Can I join more than two tables?

Yes. Chain multiple JOIN clauses: SELECT ... FROM a JOIN b ON ... JOIN c ON ...; Each additional join combines the current result set with another table.

Why does my LEFT JOIN act like an INNER JOIN?

Filtering columns from the right table in the WHERE clause removes NULL rows, turning the logic into an INNER JOIN. Move such filters into the JOIN ... ON condition instead.

When should I use a CROSS JOIN?

Use CROSS JOIN only when you intentionally need every combination of rows, such as generating test data or building a date dimension table.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!