đŸ’ģ Interview Prep: 25 SQL interview questions that freshers are asked again and again at TCS, Infosys, Wipro, Cognizant, Accenture and startups, with short answers and ready-to-use queries.

Almost every technical interview for freshers includes SQL, and many online tests have SQL questions too. The good news: the same questions come up repeatedly. Learn the concepts below and practise writing each query by hand, because interviewers often ask you to write it on paper or a shared screen.

All queries use MySQL syntax and these two sample tables:

employees(emp_id, name, email, dept_id, salary, manager_id, hire_date)
departments(dept_id, dept_name)

Part 1: Concepts (Questions 1–10)

1. What are DDL, DML, DCL and TCL?DDL defines structure: CREATE, ALTER, DROP, TRUNCATE. DML works with data: SELECT, INSERT, UPDATE, DELETE. DCL controls access: GRANT, REVOKE. TCL manages transactions: COMMIT, ROLLBACK, SAVEPOINT.
2. Primary key vs unique key vs foreign key?A primary key uniquely identifies each row and cannot be NULL; one per table. A unique key also prevents duplicates but allows NULLs, and a table can have several. A foreign key refers to a primary key in another table and keeps related data consistent.
3. WHERE vs HAVING?WHERE filters rows before grouping. HAVING filters groups after GROUP BY and can use aggregate functions like COUNT() or SUM().
4. DELETE vs TRUNCATE vs DROP?DELETE removes selected rows, can use WHERE, and can be rolled back inside a transaction. TRUNCATE quickly removes all rows and keeps the table. DROP removes the whole table, including its structure.
5. What are the types of joins?INNER: only matching rows. LEFT: all rows from the left table plus matches. RIGHT: all rows from the right table plus matches. FULL OUTER: all rows from both (not built into MySQL; use LEFT JOIN UNION RIGHT JOIN). CROSS: every combination. SELF: a table joined to itself.
6. UNION vs UNION ALL?UNION combines results and removes duplicates. UNION ALL keeps duplicates and is faster.
7. What is normalisation?Organising tables to avoid duplicate data. 1NF: each column holds single values. 2NF: no column depends on only part of a composite key. 3NF: no non-key column depends on another non-key column.
8. What are ACID properties?Atomicity (all or nothing), Consistency (data stays valid), Isolation (transactions don't interfere), Durability (committed data survives crashes).
9. What is an index?A structure (usually a B-tree) that makes searches on a column faster, at the cost of extra storage and slightly slower inserts and updates. A clustered index decides the physical order of rows (one per table); non-clustered indexes are separate lookup structures.
10. What is a view?A saved query that behaves like a virtual table. Useful to simplify complex queries or show only some columns to some users.

Part 2: Queries You Must Be Able to Write (Questions 11–20)

11. Find the second-highest salary.

SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

12. Find the Nth-highest salary (example: 3rd). Uses a window function (MySQL 8+):

SELECT DISTINCT salary
FROM (
    SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
    FROM employees
) ranked
WHERE rnk = 3;

13. Count employees in each department, showing only departments with more than 5.

SELECT d.dept_name, COUNT(*) AS total
FROM employees e
JOIN departments d ON e.dept_id = d.dept_id
GROUP BY d.dept_name
HAVING COUNT(*) > 5;

14. Find duplicate emails.

SELECT email, COUNT(*) AS times
FROM employees
GROUP BY email
HAVING COUNT(*) > 1;

15. Delete duplicate rows, keeping the one with the lowest ID.

DELETE e1
FROM employees e1
JOIN employees e2
  ON e1.email = e2.email
 AND e1.emp_id > e2.emp_id;

16. Find employees who earn more than their manager. (A self join.)

SELECT e.name AS employee, e.salary, m.name AS manager, m.salary AS manager_salary
FROM employees e
JOIN employees m ON e.manager_id = m.emp_id
WHERE e.salary > m.salary;

17. Find the highest-paid employee in each department.

SELECT dept_id, name, salary
FROM (
    SELECT dept_id, name, salary,
           RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk
    FROM employees
) ranked
WHERE rnk = 1;

18. Find departments that have no employees.

SELECT d.dept_name
FROM departments d
LEFT JOIN employees e ON e.dept_id = d.dept_id
WHERE e.emp_id IS NULL;

19. Find employees whose name starts with "A".

SELECT name FROM employees WHERE name LIKE 'A%';

(% matches any number of characters; _ matches exactly one.)

20. Find employees hired in the last 30 days.

SELECT name, hire_date
FROM employees
WHERE hire_date >= DATE_SUB(CURDATE(), INTERVAL 30 DAY);

Part 3: Questions That Separate Good Answers (21–25)

21. ROW_NUMBER vs RANK vs DENSE_RANK?For salaries 100, 90, 90, 80: ROW_NUMBER gives 1, 2, 3, 4 (always unique). RANK gives 1, 2, 2, 4 (skips after a tie). DENSE_RANK gives 1, 2, 2, 3 (no gaps).
22. How do you handle NULL?Use IS NULL / IS NOT NULL, never = NULL. Use COALESCE(column, default) to replace NULLs, e.g. COALESCE(manager_id, 0). Most aggregates such as SUM and AVG ignore NULLs.
23. CHAR vs VARCHAR?CHAR is fixed length and padded with spaces, which suits codes like state codes. VARCHAR is variable length and stores only what's needed, which suits names and emails.
24. In what order does SQL run a query?FROM and JOIN → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY → LIMIT. This is why you can't use a SELECT alias inside WHERE.
25. Stored procedure vs function?A function returns a single value and can be used inside a SELECT. A stored procedure can run several statements, return multiple results, and is called with CALL. Procedures can also manage transactions.

How to Practise (1 Week)

Day 1–2Install MySQL or use a free online SQL editor. Create the two sample tables above and insert 15–20 rows, including a few duplicates and NULLs.
Day 3–4Write queries 11–20 yourself without looking. Check the results against your data.
Day 5Practise joins: write the same question using INNER, LEFT and RIGHT joins and compare the outputs.
Day 6Window functions: ranking, running totals with SUM() OVER (ORDER BY ...).
Day 7Say the answers to questions 1–10 and 21–25 out loud, as you would in an interview.

Interview tip: if you're asked to write a query, say your approach out loud first ("I'll group by department, then filter groups with HAVING"). Interviewers care about how you think as much as about perfect syntax.

Find fresher jobs that ask for SQL on our latest jobs page, and get daily alerts on our Telegram channel.

Preparing for your placement interviews?

Get our free Starter Pack: 10 ATS resume rules, the 10 HR questions every fresher is asked (with sample answers), and a job scam checklist.

Download free PDF See the full Placement Kit →
Disclaimer: FreshersBlog.com is a job information portal. We do not charge any fee for job applications. The details provided above are curated from official sources for informational purposes. Please verify all details on the official company website before applying.