Inner Join

SQL · Joining Multiple Tables

Theory

A join combines rows from two tables using a condition, usually matching a foreign key to a primary key, such as employees.dept_id = departments.id. INNER JOIN keeps only the pairs where the condition is TRUE: an employee appears once for every department it matches, and rows with no match on the other side disappear from the result.

Conceptually the engine pairs every left row with every right row and keeps the pairs passing the ON condition; in practice it uses hash, merge or nested-loop algorithms to do this fast. Plain JOIN means INNER JOIN. Because NULL never equals anything, a row with a NULL key is always dropped. If the right table has two matching rows, the left row is duplicated, so watch row counts after a join.

Why we use it

Data is split across tables to avoid repetition: employees store a dept_id, not the department name. INNER JOIN puts the pieces back together so you can see related facts from both tables in one row.

When to use it

Use INNER JOIN when you only want rows that have a match on both sides. If you must keep rows with no match (e.g. employees without a department), use LEFT JOIN. Always join on key columns to avoid accidental duplicates.

How it works

  1. Take a row from employees
  2. Look for departments rows where e.dept_id = d.id
  3. Is there at least one match? Yes: Output one combined row per match. No: Drop the employee row.
  4. Repeat for every employees row
  5. Return only matched pairs

Syntax

SELECT columns
FROM t1
INNER JOIN t2 ON t1.id = t2.t1_id;

Example

SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d ON e.dept_id = d.id;

Key points

SQL topics

Introduction to SQL

Querying Data

Sorting Rows

Limiting Rows

Filtering Rows

Joining Multiple Tables

Grouping Rows

SET Operators

Working with Tables

Modifying Data

Views & Triggers

Conditional Expressions

SQL Subqueries

Aggregate Functions

Window Functions

String Functions

Date Functions

Math Functions

Extra Concepts

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Master SQL from Zero to Pro

All SQL topics in one place. Click any topic in the sidebar — content opens right here without leaving the page.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice SQL Online Solve 150 SQL interview problems in a live editor with instant checking. Start practising →
Section {{ sec.num }}

{{ sec.label }}

SQL  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs against a real sample database
{{ liveError }}
{{ col }}
{{ cell }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic