Last updated

SQL Joins

Definition: A JOIN combines rows from two tables based on a related column. It is how you pull together data that lives in separate tables.

In our database, the Orders table links to Customers through CustomerID, and to Products through ProductID.

Example 1 — INNER JOIN (matching rows only)

Show each order together with the customer who placed it:

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

Example 2 — join to bring in product details

SELECT Orders.OrderID, Products.ProductName, Orders.Quantity
FROM Orders
INNER JOIN Products ON Orders.ProductID = Products.ProductID;

INNER vs LEFT JOIN

  • INNER JOIN — only rows that match in both tables
  • LEFT JOIN — every row from the left table, plus matches from the right (NULL where there is no match)

💡 Tip: when two tables share a column name, write TableName.ColumnName so the database knows which one you mean.

Try it Yourself
Output

          
Ad · responsive