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
Table: departments
What is INNER JOIN
INNER JOIN combines rows from two tables based on a related column.
Basic Syntax
sqlSELECT columnsFROM table1INNER JOIN table2ON table1.column = table2.column;
Example: Join Employees with Departments
sqlSELECT employees.name, departments.department_nameFROM employeesINNER JOIN departmentsON 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
sqlSELECT e.name, d.department_nameFROM employees eINNER JOIN departments dON e.department_id = d.id;
Selecting Multiple Columns
sqlSELECT e.name, d.department_name, e.salaryFROM employees eINNER JOIN departments dON e.department_id = d.id;
INNER JOIN with WHERE
sqlSELECT e.name, d.department_nameFROM employees eINNER JOIN departments dON e.department_id = d.idWHERE 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.
Employee + department
Join employees with departments and show employee name and department name.
Solve MissionEmployee salary + dept
Show employee name and salary along with department name.
IT employees
Retrieve employees who belong to IT department.
Salary > 50000 with dept
Retrieve employees with salary greater than 50000 along with department name.
Count per department
Count number of employees in each department using JOIN.
Average salary per dept
Find average salary per department using JOIN.
HR or Finance
Retrieve employees working in HR or Finance.
Sort by salary
Retrieve employee names sorted by salary in descending order with department name.
Name starts with A
Retrieve employees whose name starts with 'A' along with department name.
Highest avg salary
Find department with highest average salary.