☰ Learn SQL Tutorial Menu

Joining Tables: INNER JOIN

Written by Faisal Adnan · Updated 14 Oct 2026 · 3 min read


So far every query has looked at one table. Real questions usually need data from multiple tables — that's what JOIN is for.

Why we need joins

The employees table only stores a department_id number — not the department's name. To show employee names alongside their department name, we need to combine employees with departments.

INNER JOIN syntax

SELECT e.first_name, e.last_name, d.name AS department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id;

Result: first 5 of 26 rows (the order can differ on other engines unless you add ORDER BY)

first_namelast_namedepartment_name
AliSheikhEngineering
SaraQureshiSales
AhmedQureshiMarketing
AyeshaSheikhHuman Resources
BilalAhmedFinance

Breaking this down:

  • e and d are aliases — short nicknames for the tables, used to keep column references unambiguous and queries shorter.
  • ON e.department_id = d.id is the join condition — it tells the database how the two tables relate.
  • INNER JOIN returns only rows where the condition matches on both sides. An employee with a department_id that doesn't exist in departments (or a NULL one) would be excluded entirely.

Joining three or more tables

Chain joins together to pull in as many related tables as you need:

SELECT o.id AS order_id, c.customer_name, e.first_name AS handled_by, o.order_date
FROM orders o
INNER JOIN customers c ON o.customer_id = c.id
INNER JOIN employees e ON o.employee_id = e.id;

Result: first 5 of 50 rows (the order can differ on other engines unless you add ORDER BY)

order_idcustomer_namehandled_byorder_date
1Danish RazaRabia2023-10-24
2Hina ChaudhrySadia2024-06-09
3Tariq AhmedSara2023-11-26
4Omar MalikZara2023-08-28
5Tariq ChaudhryZara2024-06-03

Joining with a WHERE clause

SELECT e.first_name, d.name AS department_name
FROM employees e
INNER JOIN departments d ON e.department_id = d.id
WHERE d.location = 'Lahore';

Result: first 5 of 9 rows (the order can differ on other engines unless you add ORDER BY)

first_namedepartment_name
AliEngineering
AyeshaHuman Resources
UsmanEngineering
MahnoorHuman Resources
TariqEngineering

JOIN vs INNER JOIN

Plain JOIN is exactly the same as INNER JOIN — the INNER keyword is optional but recommended for clarity, especially once you start using outer joins in the next article.

Open the Code Playground, pick any SQL engine (SQLite, PostgreSQL, MySQL, or SQL Server), and paste these queries in directly — the practice database above is already loaded for you, every time.

Lesson 10 of 27

Sign in to track your progress and earn XP for every lesson you finish.

Practice tip: the built-in code runner is being upgraded. Until it is back, copy the examples into any SQL or Python tool you have (for example SQLite, PostgreSQL, MySQL, SQL Server or a free online editor) and run them there. You can keep reading and earning XP here.