SQL FOUNDATIONS:SQL Joins (INNER JOIN)

Combine rows from two tables using INNER JOIN.

SQL INNER JOIN Explained with Examples + Practice Questions

When to Use INNER JOIN

Use INNER JOIN when you want only matching data from two tables.

It returns rows where the join condition is satisfied in both tables.

This is one of the most important SQL topics for interviews.

Example Dataset

Table: employees

idnamedepartment_idsalary
1John140000
2Alice260000
3Bob255000
4Emma370000
5DavidNULL45000

Table: departments

iddepartment_name
1HR
2IT
3Finance

What is INNER JOIN

INNER JOIN combines rows from two tables based on a related column.

Basic Syntax

sql
SELECT columns
FROM table1
INNER JOIN table2
ON table1.column = table2.column;

Example: Join Employees with Departments

sql
SELECT employees.name, departments.department_name
FROM employees
INNER JOIN departments
ON employees.department_id = departments.id;

Output:

  • John -> HR
  • Alice -> IT
  • Bob -> IT
  • Emma -> Finance

David is excluded because department_id is NULL.

Using Table Aliases

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

Selecting Multiple Columns

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

INNER JOIN with WHERE

sql
SELECT e.name, d.department_name
FROM employees e
INNER JOIN departments d
ON e.department_id = d.id
WHERE d.department_name = 'IT';

Key Concepts

  • INNER JOIN returns only matching rows
  • Rows without matches are excluded
  • Requires a join condition using ON
  • Aliases improve readability
  • Most interview problems use JOIN + filtering

INNER JOIN Flow (Concept)

  • Match rows where keys are equal
  • Combine columns from both tables
  • Ignore unmatched rows

Common Questions (FAQ)

What does INNER JOIN do

INNER JOIN returns only rows where there is a match in both tables.

What happens to unmatched rows

They are excluded from the result.

Why use aliases in JOIN

Aliases make queries shorter and easier to read.

Is INNER JOIN same as JOIN

Yes, JOIN by default means INNER JOIN.

Internal Learning Links

  • Learn SELECT basics
  • Learn WHERE filtering
  • Learn GROUP BY
  • Learn OUTER JOIN

Practice CTA

Practice SQL JOIN questions on Schoolabe to master real interview scenarios.

SQL Joins (INNER JOIN) Missions

Solve exercises in sequence to unlock the next mission.

1

Employee + department

Join employees with departments and show employee name and department name.

Solve Mission
2

Employee salary + dept

Show employee name and salary along with department name.

Locked
3

IT employees

Retrieve employees who belong to IT department.

Locked
4

Salary > 50000 with dept

Retrieve employees with salary greater than 50000 along with department name.

Locked
5

Count per department

Count number of employees in each department using JOIN.

Locked
6

Average salary per dept

Find average salary per department using JOIN.

Locked
7

HR or Finance

Retrieve employees working in HR or Finance.

Locked
8

Sort by salary

Retrieve employee names sorted by salary in descending order with department name.

Locked
9

Name starts with A

Retrieve employees whose name starts with 'A' along with department name.

Locked
10

Highest avg salary

Find department with highest average salary.

Locked