SQL Interview Questions and Answers for Freshers (2026) with Queries

SQL is asked in almost every fresher interview: software developer, data analyst, tester, even support roles. The good news is that the same 30 to 40 questions come again and again. This page covers them in simple words, with small queries you can write on paper in the interview.
Read the quick table first, then go question by question. For theory around databases, also read our DBMS interview questions.
Quick summary
| Topic | What they usually ask | Must know |
|---|---|---|
| Basics | SQL vs MySQL, DDL/DML/DCL/TCL | Command types |
| Keys | Primary, foreign, unique, candidate | Difference table |
| Joins | Inner, left, right, full, self | Draw the venn idea |
| Filtering | WHERE vs HAVING | Order of execution |
| Aggregates | COUNT, SUM, AVG, GROUP BY | Group by with HAVING |
| Queries | Second highest salary, duplicates | 3 to 4 fixed queries |
| Advanced | Window functions, CTE, index | ROW_NUMBER, RANK |
Basic SQL interview questions
1. What is SQL?
SQL (Structured Query Language) is the standard language to create, read, update and delete data in a relational database. MySQL, PostgreSQL, Oracle and SQL Server are database systems that use SQL.
2. What is the difference between SQL and MySQL?
SQL is the language. MySQL is a database software that understands SQL. In short: SQL is like English, MySQL is one person who speaks it.
3. What are the types of SQL commands?
- DDL (Data Definition): CREATE, ALTER, DROP, TRUNCATE
- DML (Data Manipulation): INSERT, UPDATE, DELETE
- DQL (Data Query): SELECT
- DCL (Data Control): GRANT, REVOKE
- TCL (Transaction Control): COMMIT, ROLLBACK, SAVEPOINT
4. What is the difference between DELETE, TRUNCATE and DROP?
| Point | DELETE | TRUNCATE | DROP |
|---|---|---|---|
| Type | DML | DDL | DDL |
| Removes | Selected rows (WHERE) | All rows | Whole table |
| Rollback | Yes | Usually no | No |
| Structure kept | Yes | Yes | No |
| Speed | Slow | Fast | Fast |
5. What is a primary key?
A primary key uniquely identifies each row. It cannot be NULL and cannot repeat. A table has only one primary key, but it can be made of more than one column (composite key).
6. What is a foreign key?
A foreign key is a column in one table that points to the primary key of another table. It keeps the link between tables correct (referential integrity). Example: orders.customer_id refers to customers.id.
7. Primary key vs unique key?
Both stop duplicate values. A primary key does not allow NULL and there is only one per table. A unique key allows one NULL (in most databases) and a table can have many unique keys.
8. What are constraints?
Rules on columns: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK and DEFAULT.
9. What is the difference between CHAR and VARCHAR?
CHAR has a fixed length (CHAR(10) always uses 10 characters). VARCHAR has a variable length and only uses the space it needs. Use CHAR for fixed codes like a PIN, VARCHAR for names.
10. What is NULL?
NULL means “no value” or “unknown”. It is not zero and not an empty string. Check it with IS NULL, never with = NULL.
Joins (asked in every interview)
11. What is a join?
A join combines rows from two or more tables using a related column.
12. Types of joins
| Join | Returns |
|---|---|
| INNER JOIN | Only matching rows in both tables |
| LEFT JOIN | All rows of left table + matches from right |
| RIGHT JOIN | All rows of right table + matches from left |
| FULL OUTER JOIN | All rows of both, NULL where no match |
| CROSS JOIN | Every row with every row |
| SELF JOIN | A table joined with itself |
13. Write an inner join query
SELECT e.name, d.dept_name FROM employee e INNER JOIN department d ON e.dept_id = d.id;
14. What is a self join? Give an example.
A table joined with itself. Classic question: show each employee with their manager name.SELECT e.name AS employee, m.name AS manager FROM employee e LEFT JOIN employee m ON e.manager_id = m.id;
15. Find employees who are not in any department
SELECT e.name FROM employee e LEFT JOIN department d ON e.dept_id = d.id WHERE d.id IS NULL;
Filtering and grouping
16. WHERE vs HAVING?
WHERE filters rows before grouping. HAVING filters groups after GROUP BY. You cannot use an aggregate like COUNT() in WHERE.
17. What is the order of execution of a SELECT query?
FROM, JOIN, WHERE, GROUP BY, HAVING, SELECT, DISTINCT, ORDER BY, LIMIT. This is why a column alias from SELECT does not work in WHERE.
18. What are aggregate functions?
Functions that return one value from many rows: COUNT, SUM, AVG, MIN, MAX.
19. Count employees in each department with more than 5 people
SELECT dept_id, COUNT(*) AS total FROM employee GROUP BY dept_id HAVING COUNT(*) > 5;
20. COUNT(*) vs COUNT(column)?
COUNT(*) counts all rows. COUNT(column) skips rows where that column is NULL.
21. What is DISTINCT?
It removes duplicate rows from the result: SELECT DISTINCT city FROM student;
22. UNION vs UNION ALL?
UNION joins two results and removes duplicates. UNION ALL keeps duplicates and is faster.
Most asked query questions
23. Find the second highest salary
SELECT MAX(salary) FROM employee WHERE salary < (SELECT MAX(salary) FROM employee);
Another way: SELECT DISTINCT salary FROM employee ORDER BY salary DESC LIMIT 1 OFFSET 1;
24. Find the Nth highest salary
SELECT salary FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employee) t WHERE rnk = 3;
Change 3 to any N.
25. Find duplicate emails
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1;
26. Delete duplicate rows but keep one
DELETE FROM users WHERE id NOT IN (SELECT MIN(id) FROM users GROUP BY email);
(In MySQL wrap the inner query in one more SELECT.)
27. Highest salary in each department
SELECT dept_id, MAX(salary) FROM employee GROUP BY dept_id;
28. Names starting with A
SELECT name FROM student WHERE name LIKE 'A%';
% means any number of characters, _ means exactly one character.
29. Fetch the top 3 rows
MySQL and PostgreSQL use LIMIT 3, SQL Server uses TOP 3, Oracle uses FETCH FIRST 3 ROWS ONLY. Mention this; interviewers like it.
Subqueries, views and indexes
30. What is a subquery?
A query inside another query. A correlated subquery uses a value from the outer query and runs once per outer row, so it is slower.
31. What is a view?
A view is a saved SELECT query that acts like a virtual table. It does not store data by itself. Use it to hide complex joins or limit which columns users see.
32. What is an index?
An index is like the index at the back of a book. It makes searching fast but makes INSERT and UPDATE a little slower and uses extra space.
33. Clustered vs non-clustered index?
A clustered index decides the physical order of rows, so a table has only one. A non-clustered index is a separate structure with pointers to rows, and a table can have many.
Advanced (for good companies)
34. What are window functions?
They calculate over a set of rows without merging them into one row. Common ones: ROW_NUMBER, RANK, DENSE_RANK, LEAD, LAG, and SUM() OVER.
35. RANK vs DENSE_RANK vs ROW_NUMBER?
Salaries 100, 100, 90:
- ROW_NUMBER gives 1, 2, 3
- RANK gives 1, 1, 3 (skips 2)
- DENSE_RANK gives 1, 1, 2 (no gap)
36. What is a CTE?
A Common Table Expression is a named temporary result written with WITH. It makes long queries easy to read.WITH high AS (SELECT * FROM employee WHERE salary > 50000) SELECT dept_id, COUNT(*) FROM high GROUP BY dept_id;
37. What is a transaction and ACID?
A transaction is a group of steps that either all happen or none happen. ACID means Atomicity, Consistency, Isolation and Durability. Bank transfer is the standard example.
38. What is normalization?
Splitting data into well-designed tables to remove repetition. 1NF: atomic values. 2NF: no partial dependency. 3NF: no transitive dependency. BCNF: every determinant is a candidate key. Full theory is in our DBMS questions post.
39. What is a stored procedure?
A saved set of SQL statements that you can run by name with CALL. It reduces repeated code and network trips.
40. What is a trigger?
SQL code that runs on its own when an INSERT, UPDATE or DELETE happens on a table, for example to write an audit log.
How to prepare in 2 days
- Day 1: questions 1 to 22, and write every query by hand once.
- Day 2: questions 23 to 40, then practise second highest salary, duplicates and joins 3 times each.
- Say the query out loud while writing. Interviewers check your thinking, not only the answer.
- Keep one sample table (employee, department) in mind and use it in every answer.




Doubts? Ask here