SQL Interview Questions with Answers and Query Examples
Why SQL Interview Questions Matter for Job Readiness
SQL Interview Questions are consistently searched by learners because Structured Query Language powers databases, reports, analytics, and many backend systems. From data roles to application development, employers expect candidates to think in queries and explain how data is retrieved and managed.
Practicing SQL Interview Questions with hands-on examples builds real confidence. In interviews, you’re often asked to write or explain a query live. A clear understanding of joins, filters, grouping, and indexing helps you respond logically instead of relying on memorized lines.
Basic SQL Interview Questions (with answers and queries)
Sample table: employees(id, name, dept, salary)
1) What is SQL?
A: SQL is a language used to create, read, update, and delete data stored in relational databases.
2) How do you display all records from a table?
A: SELECT * FROM employees;
3) How do you filter employees from the HR department?
A: SELECT name FROM employees WHERE dept = 'HR';
4) What is a primary key?
A: A column (or set of columns) that uniquely identifies every row in a table.
5) How do you insert a new record?
A: INSERT INTO employees VALUES (101, 'Ravi', 'IT', 50000);
6) How do you update a salary?
A: UPDATE employees
SET salary = 55000
WHERE id = 101;
7) How do you delete a specific record?
A: DELETE FROM employees WHERE id = 101;
8) What does NULL mean?
A: It represents a missing or unknown value.
9) How do you sort employees by salary in descending order?
A: SELECT * FROM employees ORDER BY salary DESC;
10) How do you list unique departments?
A: SELECT DISTINCT dept FROM employees;
Intermediate SQL Interview Questions on Joins and Aggregation
Sample tables:
employees(id, name, dept_id, salary)
departments(dept_id, dept_name)
11) What is an INNER JOIN?
A: It returns matching rows from both tables.
SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d
ON e.dept_id = d.dept_id;
12) What is a LEFT JOIN?
A: Returns all records from the left table and matched ones from the right.
SELECT e.name, d.dept_name
FROM employees e
LEFT JOIN departments d
ON e.dept_id = d.dept_id;
13) How do you count employees in each department?
A: SELECT dept_id, COUNT(*)
FROM employees
GROUP BY dept_id;
14) What is the HAVING clause used for?
A: It filters grouped results.
SELECT dept_id, COUNT(*)
FROM employees
GROUP BY dept_id
HAVING COUNT(*) > 5;
15) How do you calculate the average salary?
A: SELECT AVG(salary) FROM employees;
16) How to find the maximum salary by department?
A: SELECT dept_id, MAX(salary)
FROM employees
GROUP BY dept_id;
17) What is a subquery?
A: A query nested inside another query.
SELECT name FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);
18) What is a view, and how to create one?
A: A virtual table based on a query.
CREATE VIEW high_salary AS
SELECT name, salary FROM employees WHERE salary > 60000;
19) What is a foreign key?
A: A column that links two related tables.
20) How do you sort results alphabetically?
A: SELECT * FROM employees ORDER BY name ASC;
Advanced SQL Interview Questions with Practical Queries
21) How to find the second-highest salary?
A: SELECT MAX(salary)
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
22) How to identify duplicate names?
A: SELECT name, COUNT(*)
FROM employees
GROUP BY name
HAVING COUNT(*) > 1;
23) How to remove duplicates using ROW_NUMBER()?
A: DELETE FROM employees
WHERE id IN (
SELECT id FROM (
SELECT id,
ROW_NUMBER() OVER (PARTITION BY name ORDER BY id) rn
FROM employees
) t WHERE rn > 1
);
24) How to fetch the top 3 salaries?
A: SELECT * FROM employees
ORDER BY salary DESC
LIMIT 3;
25) What is a self-join?
A: SELECT a.name, b.name AS manager
FROM employees a
JOIN employees b
ON a.manager_id = b.id;
26) How to create an index?
A: CREATE INDEX idx_salary ON employees(salary);
27) What is a transaction?
A: BEGIN;
UPDATE employees SET salary = salary + 5000;
COMMIT;
28) What is a CTE?
WITH avg_sal AS (
SELECT AVG(salary) avg_salary FROM employees
)
SELECT name
FROM employees, avg_sal
WHERE employees.salary > avg_sal.avg_salary;
29) What is a window function?
A: SELECT name, RANK() OVER (ORDER BY salary DESC) rnk
FROM employees;
30) Difference between UNION and UNION ALL?
A: SELECT name FROM table1
UNION
SELECT name FROM table2;
Real Query-Based SQL Interview Questions (with answers)
31) Employees earning more than the department average
A: SELECT e.name
FROM employees e
JOIN (
SELECT dept_id, AVG(salary) avg_sal
FROM employees
GROUP BY dept_id
) d
ON e.dept_id = d.dept_id
WHERE e.salary > d.avg_sal;
32) Departments with no employees
A: SELECT d.dept_name
FROM departments d
LEFT JOIN employees e
ON d.dept_id = e.dept_id
WHERE e.id IS NULL;
33) Monthly salary expense by department
A: SELECT dept_id, SUM(salary) total_salary
FROM employees
GROUP BY dept_id;
34) Retrieve last inserted record (generic)
A: SELECT * FROM employees
ORDER BY id DESC
LIMIT 1;
35) Count employees per department
A: SELECT dept_id, COUNT(*) FROM employees GROUP BY dept_id;
Learn SQL Practically with Hachion Online Training
Studying SQL Interview Questions is helpful, but practicing queries on real datasets is what makes you confident. Hachion’s training includes live practice, query drills, project work, and interview guidance to help you write queries smoothly during interviews.
Frequently Asked Questions (FAQs)
1. Which SQL topics are most asked?
A: Joins, subqueries, indexes, CTEs, and window functions.
2. Do interviewers ask for live queries?
A: Yes, writing queries is very common.
3. Is SQL enough for data roles?
A: It is a core skill and works well with BI and analytics tools.
4. How should beginners practice SQL?
A: By creating small tables and solving query problems daily.
Final Thoughts
Preparing for SQL interviews with real query practice helps you approach interviews with clarity. When you can explain logic and write queries confidently, you stand out as a practical candidate ready for real-world tasks.

