Easy Logical Thinking SQL

SQL Practice Questions with Answers

Solve 15 beginner SQL questions on the employees table and check each answer against its result table.

  • TimeO(n) for each filter or group query on this table; ORDER BY adds O(n log n); the subqueries scan the table twice
  • SpaceO(n) for the result set in the worst case
  • Hints3
  • Read5 min
Quick answer

Practise SQL by writing each query yourself before looking at the answer. Start with WHERE filters, then IN and LIKE, then GROUP BY with HAVING, then subqueries such as the second highest salary. The 15 questions on this page use one small employees table, and every answer below shows the query and its exact result.

Filtering with WHERE, IN and LIKEGROUP BY and HAVINGSubqueriesLIMIT and OFFSET
1

Problem statement

Use the same employees table as the basic SQL queries page. The script to create it is in the solution section.

emp_id name department salary city
1 Ravi IT 60000 Hyderabad
2 Priya HR 45000 Chennai
3 Kiran IT 75000 Bengaluru
4 Sneha Finance 55000 Hyderabad
5 Arjun IT 51000 Pune
6 Divya HR 52000 Hyderabad
7 Rahul Finance 68000 Chennai
8 Anjali Sales 40000 Pune

Write a query for each question. Answers are in the solution section below.

Level 1: filters

  1. Show the names of employees who work in Chennai.
  2. Show the name and salary of employees who earn less than 50000.
  3. Show the name and department of everyone who is not in IT.
  4. Show the name and city of employees in Hyderabad or Pune, using IN.
  5. Show the name and salary of the highest-paid employee.

Level 2: groups

  1. Count the employees in each city, sorted by city.
  2. Show departments that have more than one employee.
  3. Show the total salary of each department, highest total first.
  4. Show names that end with the letter a.

Level 3: subqueries and more

  1. Find the second highest salary.
  2. Show employees who earn more than the average salary, highest first.
  3. Show the lowest salary in each department.
  4. Show cities where the average salary is above 56000.
  5. Show the 2nd, 3rd and 4th highest-paid employees.
  6. Count how many different departments there are.
Input

The employees table above.

Output

The result table of each query.

Examples

Example 1
InputQuestion 1: employees in Chennai
OutputPriya, Rahul

Only these two rows have city = 'Chennai'.

Example 2
InputQuestion 15: number of departments
Output4

IT, HR, Finance and Sales.

Constraints

  • Answers use standard SQL that runs on MySQL 8; results were checked by running the queries
  • Every result with more than one row uses ORDER BY, so the row order is fixed
2

Think before you code

Reveal the questions to ask yourself first
  • Does the question filter rows (WHERE) or groups (HAVING)?
  • Does it need one value from the whole table first, like the average? That is a subquery.
  • Is the result one row per employee or one row per group?
3

Hints

Open one at a time — try again after each.
1 Hint 1
Use HAVING for conditions on COUNT, SUM or AVG. WHERE runs before grouping and cannot see them.
2 Hint 2
For the second highest salary, find the highest salary below the maximum.
3 Hint 3
LIMIT 3 OFFSET 1 skips one row, then returns the next three.
4

Approach

Reveal the step-by-step approach

How to read a SQL question

Words in the question SQL you need
"who", "show employees" with a condition WHERE
"one of these values" IN (...)
"starts with", "ends with", "contains" LIKE 'A%', '%a', '%an%'
"each", "per", "for every" GROUP BY
"groups with more than", "departments having" HAVING
"more than the average", "second highest" subquery
"top", "highest", "first n" ORDER BY ... DESC LIMIT n

Write the FROM and WHERE first, check the rows, then add grouping and sorting.

For a refresher on each clause, see SQL GROUP BY and subqueries in SQL.

5

Dry run

Walk through the example step by step

Question 7: departments with more than one employee.

Step 1, GROUP BY department:
Finance -> Sneha, Rahul        COUNT = 2
HR      -> Priya, Divya        COUNT = 2
IT      -> Ravi, Kiran, Arjun  COUNT = 3
Sales   -> Anjali              COUNT = 1

Step 2, HAVING COUNT(*) > 1:
Finance kept, HR kept, IT kept, Sales removed
6

Solution

Reveal the full SQL solution
solution.sql
-- Sample table (same as the basic SQL queries page)
CREATE TABLE employees (
    emp_id     INT PRIMARY KEY,
    name       VARCHAR(50),
    department VARCHAR(30),
    salary     INT,
    city       VARCHAR(30)
);

INSERT INTO employees (emp_id, name, department, salary, city) VALUES
(1, 'Ravi',   'IT',      60000, 'Hyderabad'),
(2, 'Priya',  'HR',      45000, 'Chennai'),
(3, 'Kiran',  'IT',      75000, 'Bengaluru'),
(4, 'Sneha',  'Finance', 55000, 'Hyderabad'),
(5, 'Arjun',  'IT',      51000, 'Pune'),
(6, 'Divya',  'HR',      52000, 'Hyderabad'),
(7, 'Rahul',  'Finance', 68000, 'Chennai'),
(8, 'Anjali', 'Sales',   40000, 'Pune');

-- Q1
SELECT name FROM employees WHERE city = 'Chennai';
-- Q2
SELECT name, salary FROM employees WHERE salary < 50000;
-- Q3
SELECT name, department FROM employees WHERE department <> 'IT';
-- Q4
SELECT name, city FROM employees WHERE city IN ('Hyderabad', 'Pune');
-- Q5
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 1;
-- Q6
SELECT city, COUNT(*) AS employees FROM employees GROUP BY city ORDER BY city;
-- Q7
SELECT department, COUNT(*) AS employees FROM employees
GROUP BY department HAVING COUNT(*) > 1 ORDER BY department;
-- Q8
SELECT department, SUM(salary) AS total_salary FROM employees
GROUP BY department ORDER BY total_salary DESC;
-- Q9
SELECT name FROM employees WHERE name LIKE '%a';
-- Q10
SELECT MAX(salary) AS second_highest FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
-- Q11
SELECT name, salary FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees) ORDER BY salary DESC;
-- Q12
SELECT department, MIN(salary) AS lowest FROM employees
GROUP BY department ORDER BY department;
-- Q13
SELECT city, AVG(salary) AS avg_salary FROM employees
GROUP BY city HAVING AVG(salary) > 56000 ORDER BY city;
-- Q14
SELECT name, salary FROM employees ORDER BY salary DESC LIMIT 3 OFFSET 1;
-- Q15
SELECT COUNT(DISTINCT department) AS departments FROM employees;

Answers with result tables

Q1. Employees in Chennai

name
Priya
Rahul

Q2. Salary below 50000

name salary
Priya 45000
Anjali 40000

Q3. Not in IT. <> means "not equal". != also works in MySQL.

name department
Priya HR
Sneha Finance
Divya HR
Rahul Finance
Anjali Sales

Q4. Hyderabad or Pune with IN

name city
Ravi Hyderabad
Sneha Hyderabad
Arjun Pune
Divya Hyderabad
Anjali Pune

Q1 to Q4 and Q9 have no ORDER BY. MySQL usually returns them in emp_id order as shown. Add ORDER BY if your order must be fixed.

Q5. Highest-paid employee

name salary
Kiran 75000

If two people shared the top salary, LIMIT 1 would show only one of them. To show all, use WHERE salary = (SELECT MAX(salary) FROM employees).

Q6. Employees per city

city employees
Bengaluru 1
Chennai 2
Hyderabad 3
Pune 2

Q7. Departments with more than one employee

department employees
Finance 2
HR 2
IT 3

Q8. Total salary per department

department total_salary
IT 186000
Finance 123000
HR 97000
Sales 40000

Q9. Names ending with a

name
Priya
Sneha
Divya

Q10. Second highest salary

second_highest
68000

The inner query finds 75000. The outer query finds the largest salary below it. More methods are on second highest salary in SQL.

Q11. Above the average salary. The average is 55750.

name salary
Kiran 75000
Rahul 68000
Ravi 60000

Q12. Lowest salary per department

department lowest
Finance 55000
HR 45000
IT 51000
Sales 40000

Q13. Cities with average salary above 56000

city avg_salary
Bengaluru 75000.0000
Chennai 56500.0000

Hyderabad's average is about 55666.67, so it is left out. MySQL prints AVG with four decimal places; SQLite would show 75000.0 and 56500.0.

Q14. 2nd to 4th highest-paid

name salary
Rahul 68000
Ravi 60000
Sneha 55000

Q15. Number of departments

departments
4

Score yourself

Correct answers Next step
0 to 7 Redo the basic SQL queries
8 to 12 Learn SQL joins
13 to 15 Move to SQL interview questions for freshers

For the full path from here, see the SQL developer roadmap.

TimeO(n) for each filter or group query on this table; ORDER BY adds O(n log n); the subqueries scan the table twice
SpaceO(n) for the result set in the worst case
7

Review before the interview

Common mistakes

  • Writing WHERE COUNT(*) > 1. Aggregates are filtered with HAVING.
  • Using = with a list: WHERE city = ('Hyderabad', 'Pune'). Use IN instead.
  • Answering Q10 with ORDER BY salary DESC LIMIT 1 OFFSET 1. It fails when the top salary appears twice.
  • Forgetting the % in LIKE. LIKE 'a' only matches the exact value a.
  • Comparing with AVG(salary) directly in WHERE. Put it in a subquery.

Edge cases to test

  • If no row matches, the query returns an empty result. MAX on no rows returns NULL.
  • Duplicate top salaries change the answer of LIMIT-based queries but not of the MAX-below-MAX method.
  • NULL salaries would be ignored by AVG, SUM, MIN and MAX, but counted by COUNT(*).
  • MySQL string comparison is usually case-insensitive, so LIKE '%A' also matches names ending in a.

Interview follow-ups

  • How would you find the highest-paid employee in each department?
  • How would you find the third highest salary without LIMIT?
  • How would you list employees whose salary is above their own department's average?

Keep practising

Practising SQL for interviews?

CodeBegun’s Data Analytics with AI program builds SQL skills on real business data, with mentor review and mock interviews.

Chat with us