InterviewPrepKit

Home / SQL / Joins

Inner Join Basics: Employees and Departments

easy
Solving tips
  • An INNER JOIN keeps only rows that match on both sides, so unmatched employees and empty departments disappear.
  • Join on the foreign key (employees.dept_id) equaling the primary key (departments.id), not on names.
  • Rows where the join key is NULL never match in an INNER JOIN, so an employee with no dept_id is dropped.

Pair each employee with the department they belong to.

Schema

CREATE TABLE departments (
    id        INT PRIMARY KEY,
    dept_name VARCHAR(50) NOT NULL
);

CREATE TABLE employees (
    id      INT PRIMARY KEY,
    name    VARCHAR(50) NOT NULL,
    dept_id INT REFERENCES departments(id)
);

departments

iddept_name
10Engineering
20Sales
30Marketing
40Finance

employees

idnamedept_id
1Alice10
2Bob20
3Carol10
4Dave30
5EveNULL

Task

Return one row per employee who is assigned to a department, with columns name (the employee) and dept_name. Order by name ascending.

Expected output

namedept_name
AliceEngineering
BobSales
CarolEngineering
DaveMarketing
Your workspace Not runnable by design — this is your interview scratchpad. Saved on this device.