SQL Queries for Practice on an Employee Table
Write 20 SQL queries of rising difficulty on a single employee table and check each one against its result table.
- Time
O(n) for filters and GROUP BY; ORDER BY adds O(n log n); correlated subqueries can be O(n^2) without an index on department - Space
O(n) for the result set in the worst case - Hints3
- Read9 min
The best way to practise SQL on an employee table is to use one small table with a manager_id column, hire dates and some NULL values, then work through queries in order: filters, NULL checks, grouping, duplicates, the second highest salary, self joins for employee and manager names, and per-department queries with correlated subqueries or DENSE_RANK. The 20 queries on this page follow that order, and every answer shows its result table.
Problem statement
This page uses a different table from the
SQL practice questions with answers
page. This employee table has a manager_id that points to another employee, hire
dates, and a bonus column with some NULL values. That lets you practise self
joins, NULL handling and per-department queries.
| emp_id | emp_name | job_title | department | salary | hire_date | manager_id | bonus |
|---|---|---|---|---|---|---|---|
| 1 | Meera | Director | Management | 150000 | 2016-04-01 | NULL | 20000 |
| 2 | Vikram | Manager | Engineering | 110000 | 2018-07-15 | 1 | 10000 |
| 3 | Lakshmi | Manager | Sales | 95000 | 2019-01-10 | 1 | 15000 |
| 4 | Suresh | Developer | Engineering | 72000 | 2020-06-01 | 2 | NULL |
| 5 | Ananya | Developer | Engineering | 72000 | 2021-03-22 | 2 | 5000 |
| 6 | Farhan | Tester | Engineering | 48000 | 2022-08-05 | 2 | NULL |
| 7 | Pooja | Sales Executive | Sales | 42000 | 2022-11-14 | 3 | 3000 |
| 8 | Naveen | Sales Executive | Sales | 98000 | 2020-09-01 | 3 | 12000 |
| 9 | Shreya | HR Executive | HR | 50000 | 2023-02-01 | 1 | NULL |
| 10 | Karthik | Developer | Engineering | 35000 | 2024-07-01 | 4 | NULL |
Write a query for each task. Answers and result tables are in the solution section.
Level 1: easy (filters and NULLs)
- List the name, job title and salary of everyone in Engineering, sorted by name.
- Show employees with a salary from 45000 to 75000, highest first.
- Show employees who joined on or after 1 January 2022, oldest joiner first.
- Show employees whose name starts with S.
- Find the employee who has no manager.
- List employees who get no bonus.
- List each job title once, in alphabetical order.
Level 2: easy to medium (grouping, duplicates, ranking by value)
- Show the top 5 employees by total pay (salary + bonus), treating a missing bonus as 0.
- Show the headcount and total salary of each department, largest total first.
- Show departments whose average salary is above 60000, with the average rounded.
- Show the three most recently hired employees.
- Find salaries that are shared by more than one employee.
- Find the second highest salary.
- Find the third highest distinct salary.
Level 3: medium (self joins and per-group queries)
- Show every employee next to their manager's name.
- Find employees who earn more than their own manager.
- Count the direct reports of each manager.
- Find the highest-paid employee in each department.
- Find employees who earn more than their department's average.
- Show the top 2 salaries in each department, keeping ties.
The employee table above, created with the script in the solution.
The result table of each query.
Examples
Query 5: employee with no managerMeera, DirectorOnly her manager_id is NULL. It must be tested with IS NULL, not = NULL.
Query 13: second highest salary110000The highest is 150000; the largest salary below it is 110000.
Constraints
- Standard SQL; every query was run in SQLite to produce the results shown, and uses syntax MySQL 8 also accepts
- Results with more than one row use ORDER BY, so the row order is fixed
- 10 rows, 8 columns
Think before you code
Reveal the questions to ask yourself first
- Does the task need a value from another row of the same table, like the manager's name? That is a self join.
- Does it compare each employee with a value from their own group, like the department average? That is a correlated subquery.
- Can a NULL change the answer? Think about bonus and manager_id before you write the query.
Hints
Open one at a time — try again after each.1 Hint 1
2 Hint 2
3 Hint 3
Approach
Reveal the step-by-step approach
How this table is different
| Column | What it lets you practise |
|---|---|
manager_id |
Self joins: employee and manager in the same table |
bonus with NULLs |
IS NULL, COALESCE, and how NULL breaks arithmetic |
hire_date |
Date filters and "most recent" queries |
| Two salaries of 72000 | Duplicates, DISTINCT, and ties in ranking |
| Naveen earning more than Lakshmi | Comparing an employee with their manager |
Picking the right tool
| Task says | SQL you need |
|---|---|
| "no manager", "no bonus" | IS NULL |
| "treat missing as 0" | COALESCE(col, 0) |
| "shared by more than one" | GROUP BY ... HAVING COUNT(*) > 1 |
| "second highest", "nth highest" | MAX below MAX, or DISTINCT ... LIMIT 1 OFFSET n-1 |
| "their manager" | self join |
| "in each department" plus a per-row value | correlated subquery or window function |
Refresh the clauses with SQL joins explained, subqueries in SQL and SQL window functions.
Dry run
Walk through the example step by step
Query 16: employees who earn more than their manager.
Self join e (employee) with m (manager) on e.manager_id = m.emp_id:
e.emp_name | e.salary | m.emp_name | m.salary | e.salary > m.salary?
-----------+----------+------------+----------+---------------------
Vikram | 110000 | Meera | 150000 | no
Lakshmi | 95000 | Meera | 150000 | no
Suresh | 72000 | Vikram | 110000 | no
Ananya | 72000 | Vikram | 110000 | no
Farhan | 48000 | Vikram | 110000 | no
Pooja | 42000 | Lakshmi | 95000 | no
Naveen | 98000 | Lakshmi | 95000 | yes
Shreya | 50000 | Meera | 150000 | no
Karthik | 35000 | Suresh | 72000 | no
Meera has no manager, so the inner join drops her row. Only Naveen passes the WHERE condition.
Solution
Reveal the full SQL solution
-- Sample table
CREATE TABLE employee (
emp_id INT PRIMARY KEY,
emp_name VARCHAR(50),
job_title VARCHAR(40),
department VARCHAR(30),
salary INT,
hire_date DATE,
manager_id INT,
bonus INT
);
INSERT INTO employee (emp_id, emp_name, job_title, department, salary, hire_date, manager_id, bonus) VALUES
(1, 'Meera', 'Director', 'Management', 150000, '2016-04-01', NULL, 20000),
(2, 'Vikram', 'Manager', 'Engineering', 110000, '2018-07-15', 1, 10000),
(3, 'Lakshmi', 'Manager', 'Sales', 95000, '2019-01-10', 1, 15000),
(4, 'Suresh', 'Developer', 'Engineering', 72000, '2020-06-01', 2, NULL),
(5, 'Ananya', 'Developer', 'Engineering', 72000, '2021-03-22', 2, 5000),
(6, 'Farhan', 'Tester', 'Engineering', 48000, '2022-08-05', 2, NULL),
(7, 'Pooja', 'Sales Executive', 'Sales', 42000, '2022-11-14', 3, 3000),
(8, 'Naveen', 'Sales Executive', 'Sales', 98000, '2020-09-01', 3, 12000),
(9, 'Shreya', 'HR Executive', 'HR', 50000, '2023-02-01', 1, NULL),
(10, 'Karthik', 'Developer', 'Engineering', 35000, '2024-07-01', 4, NULL);
-- Q1
SELECT emp_name, job_title, salary FROM employee
WHERE department = 'Engineering' ORDER BY emp_name;
-- Q2
SELECT emp_name, salary FROM employee
WHERE salary BETWEEN 45000 AND 75000 ORDER BY salary DESC, emp_name;
-- Q3
SELECT emp_name, hire_date FROM employee
WHERE hire_date >= '2022-01-01' ORDER BY hire_date;
-- Q4
SELECT emp_name, department FROM employee
WHERE emp_name LIKE 'S%' ORDER BY emp_name;
-- Q5
SELECT emp_name, job_title FROM employee WHERE manager_id IS NULL;
-- Q6
SELECT emp_name, department FROM employee WHERE bonus IS NULL ORDER BY emp_id;
-- Q7
SELECT DISTINCT job_title FROM employee ORDER BY job_title;
-- Q8
SELECT emp_name, salary, COALESCE(bonus, 0) AS bonus,
salary + COALESCE(bonus, 0) AS total_pay
FROM employee ORDER BY total_pay DESC, emp_name LIMIT 5;
-- Q9
SELECT department, COUNT(*) AS headcount, SUM(salary) AS total_salary
FROM employee GROUP BY department ORDER BY total_salary DESC;
-- Q10
SELECT department, ROUND(AVG(salary)) AS avg_salary
FROM employee GROUP BY department
HAVING AVG(salary) > 60000 ORDER BY avg_salary DESC;
-- Q11
SELECT emp_name, hire_date FROM employee ORDER BY hire_date DESC LIMIT 3;
-- Q12
SELECT salary, COUNT(*) AS employees FROM employee
GROUP BY salary HAVING COUNT(*) > 1;
-- Q13
SELECT MAX(salary) AS second_highest FROM employee
WHERE salary < (SELECT MAX(salary) FROM employee);
-- Q14
SELECT DISTINCT salary AS third_highest FROM employee
ORDER BY salary DESC LIMIT 1 OFFSET 2;
-- Q15
SELECT e.emp_name AS employee, m.emp_name AS manager
FROM employee e LEFT JOIN employee m ON e.manager_id = m.emp_id
ORDER BY e.emp_id;
-- Q16
SELECT e.emp_name, e.salary, m.emp_name AS manager, m.salary AS manager_salary
FROM employee e JOIN employee m ON e.manager_id = m.emp_id
WHERE e.salary > m.salary;
-- Q17
SELECT m.emp_name AS manager, COUNT(*) AS direct_reports
FROM employee e JOIN employee m ON e.manager_id = m.emp_id
GROUP BY m.emp_id, m.emp_name ORDER BY direct_reports DESC, manager;
-- Q18
SELECT department, emp_name, salary FROM employee e
WHERE salary = (SELECT MAX(salary) FROM employee x WHERE x.department = e.department)
ORDER BY department;
-- Q19
SELECT emp_name, department, salary FROM employee e
WHERE salary > (SELECT AVG(salary) FROM employee x WHERE x.department = e.department)
ORDER BY department, salary DESC, emp_name;
-- Q20
SELECT department, emp_name, salary, salary_rank FROM (
SELECT department, emp_name, salary,
DENSE_RANK() OVER (PARTITION BY department ORDER BY salary DESC) AS salary_rank
FROM employee
) ranked
WHERE salary_rank <= 2
ORDER BY department, salary_rank, emp_name;Level 1 answers
Q1. Engineering employees
| emp_name | job_title | salary |
|---|---|---|
| Ananya | Developer | 72000 |
| Farhan | Tester | 48000 |
| Karthik | Developer | 35000 |
| Suresh | Developer | 72000 |
| Vikram | Manager | 110000 |
Q2. Salary from 45000 to 75000. BETWEEN includes both ends.
| emp_name | salary |
|---|---|
| Ananya | 72000 |
| Suresh | 72000 |
| Shreya | 50000 |
| Farhan | 48000 |
Q3. Joined on or after 1 January 2022. Dates in 'YYYY-MM-DD' form compare
correctly.
| emp_name | hire_date |
|---|---|
| Farhan | 2022-08-05 |
| Pooja | 2022-11-14 |
| Shreya | 2023-02-01 |
| Karthik | 2024-07-01 |
Q4. Names starting with S
| emp_name | department |
|---|---|
| Shreya | HR |
| Suresh | Engineering |
Q5. No manager. manager_id = NULL would return no rows, because a comparison
with NULL is never true.
| emp_name | job_title |
|---|---|
| Meera | Director |
Q6. No bonus
| emp_name | department |
|---|---|
| Suresh | Engineering |
| Farhan | Engineering |
| Shreya | HR |
| Karthik | Engineering |
Q7. Job titles, once each
| job_title |
|---|
| Developer |
| Director |
| HR Executive |
| Manager |
| Sales Executive |
| Tester |
Level 2 answers
Q8. Top 5 by total pay. Without COALESCE, salary + bonus is NULL for anyone
whose bonus is NULL.
| emp_name | salary | bonus | total_pay |
|---|---|---|---|
| Meera | 150000 | 20000 | 170000 |
| Vikram | 110000 | 10000 | 120000 |
| Lakshmi | 95000 | 15000 | 110000 |
| Naveen | 98000 | 12000 | 110000 |
| Ananya | 72000 | 5000 | 77000 |
Lakshmi and Naveen tie at 110000, so emp_name is used as a second sort key.
Q9. Headcount and total salary by department
| department | headcount | total_salary |
|---|---|---|
| Engineering | 5 | 337000 |
| Sales | 3 | 235000 |
| Management | 1 | 150000 |
| HR | 1 | 50000 |
Q10. Departments with average salary above 60000
| department | avg_salary |
|---|---|
| Management | 150000.0 |
| Sales | 78333.0 |
| Engineering | 67400.0 |
HR (50000) is filtered out by HAVING. Sales is 235000 / 3 = 78333.33, rounded to
78333. This output is from SQLite, which prints .0; MySQL prints the rounded values
without it, for example 78333.
Q11. Three most recent joiners
| emp_name | hire_date |
|---|---|
| Karthik | 2024-07-01 |
| Shreya | 2023-02-01 |
| Pooja | 2022-11-14 |
Q12. Salaries shared by more than one employee
| salary | employees |
|---|---|
| 72000 | 2 |
Q13. Second highest salary
| second_highest |
|---|
| 110000 |
The inner query returns 150000, and the outer query finds the largest salary below it. Other methods are on how to find the second highest salary in SQL.
Q14. Third highest distinct salary
| third_highest |
|---|
| 98000 |
The distinct salaries in order are 150000, 110000, 98000, 95000, 72000 and so on.
OFFSET 2 skips two and returns the third. DISTINCT matters here: without it, the
two 72000 rows would count as two places in a longer list.
Level 3 answers
Q15. Employee and manager names
| employee | manager |
|---|---|
| Meera | NULL |
| Vikram | Meera |
| Lakshmi | Meera |
| Suresh | Vikram |
| Ananya | Vikram |
| Farhan | Vikram |
| Pooja | Lakshmi |
| Naveen | Lakshmi |
| Shreya | Meera |
| Karthik | Suresh |
LEFT JOIN keeps Meera even though she has no manager. An inner JOIN would return
9 rows.
Q16. Earns more than their manager
| emp_name | salary | manager | manager_salary |
|---|---|---|---|
| Naveen | 98000 | Lakshmi | 95000 |
Q17. Direct reports per manager
| manager | direct_reports |
|---|---|
| Meera | 3 |
| Vikram | 3 |
| Lakshmi | 2 |
| Suresh | 1 |
Q18. Highest-paid in each department. The subquery runs once per row, using that row's department. If two people shared the top salary, both would appear.
| department | emp_name | salary |
|---|---|---|
| Engineering | Vikram | 110000 |
| HR | Shreya | 50000 |
| Management | Meera | 150000 |
| Sales | Naveen | 98000 |
Q19. Above their department's average. Engineering's average is 67400 and Sales' is about 78333.
| emp_name | department | salary |
|---|---|---|
| Vikram | Engineering | 110000 |
| Ananya | Engineering | 72000 |
| Suresh | Engineering | 72000 |
| Naveen | Sales | 98000 |
| Lakshmi | Sales | 95000 |
HR and Management have one employee each, who is equal to the average, not above it.
Q20. Top 2 salaries in each department, ties kept
| department | emp_name | salary | salary_rank |
|---|---|---|---|
| Engineering | Vikram | 110000 | 1 |
| Engineering | Ananya | 72000 | 2 |
| Engineering | Suresh | 72000 | 2 |
| HR | Shreya | 50000 | 1 |
| Management | Meera | 150000 | 1 |
| Sales | Naveen | 98000 | 1 |
| Sales | Lakshmi | 95000 | 2 |
Ananya and Suresh both get rank 2 because DENSE_RANK gives equal salaries the same
rank. ROW_NUMBER() would give them 2 and 3 and drop one of them. Window functions
need MySQL 8 or SQLite 3.25 or later.
Score yourself
| Correct answers | Next step |
|---|---|
| 0 to 7 | Go back to basic SQL queries with examples |
| 8 to 14 | Try the SQL practice questions with answers, then read SQL joins explained |
| 15 to 20 | Move on to SQL interview questions for freshers and window functions |
O(n) for filters and GROUP BY; ORDER BY adds O(n log n); correlated subqueries can be O(n^2) without an index on departmentO(n) for the result set in the worst caseReview before the interview
Common mistakes
- Writing WHERE manager_id = NULL. It matches nothing. Use IS NULL.
- Adding salary + bonus without COALESCE, so total pay becomes NULL for employees with no bonus.
- Using an inner JOIN for the employee-manager list, which silently drops the top boss.
- Using ROW_NUMBER() for "top 2 with ties", which cuts off one of two equal salaries.
- Leaving out DISTINCT in the nth-highest query, so repeated salaries take up extra places.
- Selecting emp_name with GROUP BY department. MySQL with ONLY_FULL_GROUP_BY rejects it; use a subquery as in Q18.
Edge cases to test
- NULL bonus: ignored by SUM and AVG, counted by COUNT(*), but not by COUNT(bonus).
- Ties at the top: LIMIT 1 shows one row; the MAX subquery and DENSE_RANK show all tied rows.
- A department with one employee: that employee is the highest-paid and is not above the average.
- An employee whose manager_id points to a missing emp_id: an inner join drops them; a LEFT JOIN shows NULL for the manager.
Interview follow-ups
- How would you list each employee with the full chain of managers above them, using a recursive CTE?
- How would you find the department with the highest average salary?
- How would you find employees hired in the same year as their manager?
Keep practising
SQL Practice Questions with Answers
Hints · Dry run · SolutionSolve Similar problemBasic SQL Queries with Examples and Output
Hints · Dry run · SolutionSolve Related learningSql Joins Explained
TutorialLearn Related learningSubqueries In Sql
TutorialLearn Related learningSql Window Functions Introduction
TutorialLearn Related learningHow To Find The Second Highest Salary In Sql
TutorialLearn Related learningSql Freshers
TutorialLearn CareerPilot AIPractice under pressure
Take an AI mock interview and get question-by-question feedback.
Mock interviewStartPractising SQL for interviews?
CodeBegun’s Data Analytics with AI program builds SQL skills on real business data, with mentor review and mock interviews.
