SQL
SQL Joins

SQL Joins

A JOIN clause is used to combine rows from two or more tables based on a related column between them.

Example Tables

Orders Table:

OrderIDCustomerIDOrderDate
1030821996-09-18
10309371996-09-19
10310771996-09-20

Customers Table:

CustomerIDCustomerNameContactNameCountry
1Alfreds FutterkisteMaria AndersGermany
2Ana Trujillo Emparedados y heladosAna TrujilloMexico
3Antonio Moreno TaqueríaAntonio MorenoMexico

The relationship between the tables is via the CustomerID column.


INNER JOIN Example

This query selects records that have matching values in both tables:

SELECT Orders.OrderID, Customers.CustomerName, Orders.OrderDate
FROM Orders
INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID;

SQL Joins

JOIN Types Summary

  • INNER JOIN: Matching records in both tables
  • LEFT JOIN: All from left + matched from right
  • RIGHT JOIN: All from right + matched from left
  • FULL OUTER JOIN: All records from both tables