Updated: 13 October 2026. SQL appears in many fresher interviews—not just database administrator jobs. Software developers, data analysts, QA testers and operations analysts often need to filter, join, group and validate records. Instead of memorising isolated answers, practise the following 12 queries on one fictional employee dataset. Each exercise includes a goal, the SQL, an explanation and a checkable result.
Practice database: six fictional employees
Use this simplified employees table. It has no personal or real company data, so it is safe for learning and public GitHub examples.
| id | name | department | salary | manager_id |
|---|---|---|---|---|
| 1 | Asha | Engineering | 62000 | NULL |
| 2 | Ravi | Engineering | 58000 | 1 |
| 3 | Neha | Support | 42000 | NULL |
| 4 | Omar | Support | 45000 | 3 |
| 5 | Pooja | Analytics | 55000 | NULL |
| 6 | Ishan | Engineering | 58000 | 1 |
The salary values are fictional arbitrary practice numbers, not pay statistics or Hiring365 salary estimates. You can enter them in SQLite, MySQL or PostgreSQL; a few syntax details vary by database.
CREATE TABLE employees (
id INTEGER PRIMARY KEY,
name VARCHAR(40) NOT NULL,
department VARCHAR(40) NOT NULL,
salary INTEGER NOT NULL,
manager_id INTEGER NULL
);
INSERT INTO employees
(id, name, department, salary, manager_id) VALUES
(1, 'Asha', 'Engineering', 62000, NULL),
(2, 'Ravi', 'Engineering', 58000, 1),
(3, 'Neha', 'Support', 42000, NULL),
(4, 'Omar', 'Support', 45000, 3),
(5, 'Pooja', 'Analytics', 55000, NULL),
(6, 'Ishan', 'Engineering', 58000, 1);1. Select only the fields you need
SELECT name, department FROM employees;What to explain: Using explicit column names makes an API query easier to understand and avoids retrieving fields the feature does not use. The result has six rows and only two columns.
2. Find employees in one department
SELECT name, salary
FROM employees
WHERE department = 'Engineering';Expected names: Asha, Ravi and Ishan. The WHERE clause filters rows before aggregation.
3. Sort salaries without losing tied records
SELECT name, salary
FROM employees
ORDER BY salary DESC, name ASC;Expected first result: Asha with 62000. Ravi and Ishan are tied at 58000, so the second sorting key decides their relative order. Interviewers often ask why ordering only by salary may be unstable when values are equal.
4. Count employees by department
SELECT department, COUNT(*) AS employees_count
FROM employees
GROUP BY department
ORDER BY department;Expected: Analytics 1, Engineering 3, Support 2. A grouped result has one row per distinct department.
5. Calculate an average correctly
SELECT department, AVG(salary) AS avg_salary
FROM employees
GROUP BY department;Engineering’s average is (62000 + 58000 + 58000) / 3, approximately 59,333.33. Support’s average is 43,500. The numbers are practice values. Explain why using integer division in application code might give a different display result.
6. Filter grouped results with HAVING
SELECT department, COUNT(*) AS headcount
FROM employees
GROUP BY department
HAVING COUNT(*) > 1;Expected: Engineering (3) and Support (2). Use WHERE to filter individual rows before grouping; use HAVING to filter the aggregated groups.
7. Find duplicate salary values
SELECT salary, COUNT(*) AS repetitions
FROM employees
GROUP BY salary
HAVING COUNT(*) > 1;Only 58000 appears more than once (two employees). Matching salaries do not imply duplicate employee records; explain the difference between repeated values and duplicated entities.
8. Get the second-highest distinct salary
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;In MySQL, PostgreSQL and SQLite, this gives 58000. DISTINCT prevents the repeated 58000 values from occupying separate ranks. The LIMIT/OFFSET syntax is not identical in all databases (such as SQL Server), so learn your interview’s database dialect.
9. Find employees who report to a manager
SELECT e.name AS employee, m.name AS manager
FROM employees e
JOIN employees m ON e.manager_id = m.id
ORDER BY e.name;This self-join matches two different aliases of the same table. It returns Ishan–Asha, Omar–Neha and Ravi–Asha. Employees with NULL manager IDs are excluded by an inner join.
10. Keep employees without a manager
SELECT e.name, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id
ORDER BY e.id;All six employees appear. Asha, Neha and Pooja have NULL in the manager column. A LEFT JOIN preserves rows from the left table even when there is no matching row on the right.
11. Handle missing values explicitly
SELECT e.name,
COALESCE(m.name, 'No manager recorded') AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;COALESCE uses the first non-null argument, useful for displaying a fallback in reports. It does not magically fill missing source data or prove that an employee is manager-free in the real world.
12. Find salaries above their department average
SELECT e.name, e.department, e.salary
FROM employees e
WHERE e.salary > (
SELECT AVG(x.salary)
FROM employees x
WHERE x.department = e.department
);This is a correlated subquery. Asha is above Engineering’s average; Omar is above Support’s average; Analytics has only one person whose salary equals that team’s average, not exceeds it. Explain how the inner query changes with the department of the outer row.
How to approach unfamiliar SQL interview questions
- Identify the output: Does the interviewer need individual rows, one value, or one value per group?
- Name the source tables: Sketch the columns and what connects them.
- Filter at the right stage: Use
WHEREfor row conditions andHAVINGfor grouped results. - Check missing and repeated data: Think about
NULL, ties and duplicate values. - Test with six rows first: Hand-calculate expected answers before optimising.
- Explain the query: An interviewer may value clear reasoning more than one obscure trick.
Mini practice project: support ticket analytics
After these exercises, replace the employee table with fictional support tickets: ticket ID, category, date, status, assigned engineer and response time. Write queries for open-ticket counts by category, oldest unresolved tickets, duplicate IDs and average response time. Define how you handle missing response times. These tasks resemble reporting work in analyst, support and QA roles but are not a test promised by any employer.
Common SQL interview mistakes
Mixing WHERE and HAVING: filtering aggregate counts in WHERE will not work the way beginners expect. Ignoring ties: second-highest distinct salary is different from second row after sorting. Misunderstanding NULL: use IS NULL instead of = NULL. Overusing SELECT *: production apps should generally request what they need. Assuming joins always add one row: one-to-many joins can multiply results, so understand the keys.
Frequently asked questions
Is SQL necessary for a fresher software job?
Not every job requires SQL, but it is common in backend development, testing, operations and analytics roles. Check each employer’s actual job description.
Should I learn MySQL or PostgreSQL first?
Either can teach the fundamentals of tables, queries, joins and aggregation. Use the database that fits your project and understand where syntax differs.
Does this sheet cover the exact questions asked by a company?
No. These are independently written practice problems to help build understanding, not leaked or guaranteed interview questions.
Helpful resources
Read our Spring Boot API portfolio guide and software skills guide. For syntax differences, refer to the official PostgreSQL SQL tutorial or MySQL statement reference.
Editorial note: The dataset, exercises and explanation are original Hiring365 examples. The fictional names and figures do not represent real employees. Featured photograph: Walkator / Unsplash.
