How to Write a SQL JOIN — With Live Runnable Examples
Relational databases store information across multiple normalized tables. In this guide, you will learn how to connect those tables using SQL JOIN statements, with live interactive queries you can run right here.
1. What is an INNER JOIN?
An INNER JOIN returns rows when there is at least one match in both tables based on a foreign key relationship. If a customer has no orders, they will not appear in an INNER JOIN with orders.
SELECT customers.name, orders.total_amount FROM customers INNER JOIN orders ON customers.id = orders.customer_id;
2. What is a LEFT JOIN?
A LEFT JOIN (or LEFT OUTER JOIN) returns all rows from the left table, and matching rows from the right table. If there is no match, NULL values are returned for columns of the right table.
3. Multi-Table JOINs
In real-world applications, data often spans 3 or more tables. For example, connecting an order to its line items, and each line item to its product catalog details.
Ready for Full Dual-Pane SQL Editing?
Launch SQLite Studio to view table relationships on an interactive ER diagram and execute queries line by line.
Open Full SQLite Studio