SQLite Joins
SQLite JOIN
A JOIN combines rows from two or more tables, based on a related column between them.
Here is a selection from the "Orders" table:
| OrderID | CustomerID | EmployeeID | OrderDate | ShipperID |
|---|---|---|---|---|
| 10308 | 2 | 7 | 1996-09-18 | 3 |
| 10309 | 37 | 3 | 1996-09-19 | 1 |
| 10310 | 77 | 8 | 1996-09-20 | 2 |
And a selection from the "Customers" table:
| CustomerID | CustomerName | ContactName | Country |
|---|---|---|---|
| 1 | Alfreds Futterkiste | Maria Anders | Germany |
| 2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Mexico |
| 3 | Antonio Moreno Taquería | Antonio Moreno | Mexico |
The CustomerID column in the Orders table refers to the CustomerID in the Customers table. That is the relationship between the two tables.
Example
This example combines each order with the name of the customer who placed it:
Example
SELECT Orders.OrderID, Customers.CustomerName,
Orders.OrderDate
FROM Orders
INNER JOIN Customers
ON Orders.CustomerID = Customers.CustomerID;
Try it Yourself »
Types of Joins
SQLite supports these joins:
- INNER JOIN: Returns the records that have matching values in both tables
- LEFT JOIN: Returns all the records from the left table, and the matching records from the right table
- RIGHT JOIN: Returns all the records from the right table, and the matching records from the left table
- FULL JOIN: Returns all the records from both tables
- CROSS JOIN: Returns every combination of records from the two tables
Note: RIGHT JOIN and FULL JOIN work in SQLite 3.39.0 and later. Older versions support only INNER JOIN, LEFT JOIN and CROSS JOIN.