SQL shows up in almost every fresher interview, whether you're applying for a developer, backend, data analyst or even a QA role. Interviewers ask concept questions, then hand you a table and say, "Write a query for this." Many candidates know the basics but freeze when asked to write a query on the spot.
This guide covers the top 25 SQL interview questions for freshers in 2026, from basic concepts to joins, window functions and the classic query-writing questions, with clear answers and example queries you can practise.
For the query examples, assume an employees table with columns: id, name, salary, department_id and manager_id.
Basic SQL Interview Questions
1. What is SQL?
SQL (Structured Query Language) is the standard language used to store, retrieve, update and manage data in relational databases like MySQL, PostgreSQL, Oracle and SQL Server.
2. What is the difference between SQL and MySQL?
SQL is a language. MySQL is a database management system that uses SQL. Think of SQL as the language and MySQL as one of the software products that understands it.
3. What are DDL, DML, DCL and TCL commands?
Type | Purpose | Examples |
|---|---|---|
DDL (Data Definition Language) | Define structure | CREATE, ALTER, DROP, TRUNCATE |
DML (Data Manipulation Language) | Work with data | SELECT, INSERT, UPDATE, DELETE |
DCL (Data Control Language) | Control access | GRANT, REVOKE |
TCL (Transaction Control Language) | Manage transactions | COMMIT, ROLLBACK, SAVEPOINT |
4. What is the difference between a primary key and a unique key?
Both enforce uniqueness. A primary key uniquely identifies each row, can't contain NULL, and a table can have only one. A unique key also prevents duplicates, but a table can have several, and they can usually contain NULL values.
5. What is a foreign key?
A foreign key is a column that references the primary key of another table. It maintains referential integrity. For example, department_id in employees can reference id in a departments table.
Practice
Practice this interview before the real one
Answer real interview questions out loud and get feedback on every answer from an AI interviewer.
6. What is the difference between DELETE, TRUNCATE and DROP?
Command | What it does | WHERE clause? |
|---|---|---|
DELETE | Removes selected rows; table structure stays | Yes |
TRUNCATE | Removes all rows quickly; table structure stays | No |
DROP | Deletes the entire table, including its structure | No |
DELETE can usually be rolled back within a transaction. Whether TRUNCATE can be rolled back depends on the database.
7. What is the difference between WHERE and HAVING?
WHERE filters rows before grouping. HAVING filters groups after GROUP BY, so it can use aggregate functions like COUNT or SUM.
SELECT department_id, COUNT(*) AS total
FROM employees
WHERE salary > 30000
GROUP BY department_id
HAVING COUNT(*) > 5;8. What does GROUP BY do?
GROUP BY groups rows with the same values in specified columns so you can apply aggregate functions to each group, such as total employees or average salary per department.
9. What are the types of JOINs?
INNER JOIN: Returns only matching rows from both tables.
LEFT JOIN: Returns all rows from the left table, plus matching rows from the right (NULL where there's no match).
RIGHT JOIN: Returns all rows from the right table, plus matching rows from the left.
FULL OUTER JOIN: Returns all rows from both tables, matched where possible.
CROSS JOIN: Returns every combination of rows from both tables.
SELF JOIN: Joins a table with itself.
10. Write a query to list employees with their department names.
SELECT e.name, d.name AS department
FROM employees e
LEFT JOIN departments d ON e.department_id = d.id;Using LEFT JOIN keeps employees who don't have a department assigned.
11. What is the difference between UNION and UNION ALL?
UNION combines results from two queries and removes duplicates. UNION ALL keeps all rows, including duplicates, and is usually faster because it skips duplicate removal.
12. What is normalization? Explain 1NF, 2NF and 3NF.
Normalization organises data to reduce redundancy and improve consistency.
1NF: Each column holds atomic (single) values, with no repeating groups.
2NF: 1NF, and every non-key column depends on the whole primary key, not just part of it.
3NF: 2NF, and non-key columns don't depend on other non-key columns.
13. What is an index? What are its pros and cons?
An index is a data structure that helps the database find rows faster, like an index at the back of a book. It speeds up reads and searches, but uses extra storage and can slow down inserts, updates and deletes, because the index must also be updated.
14. What are constraints in SQL?
Constraints enforce rules on data: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK and DEFAULT.
15. How do you handle NULL values?
Use IS NULL or IS NOT NULL to check for NULLs, since "= NULL" doesn't work. Use COALESCE to replace NULLs with a default value.
SELECT name, COALESCE(manager_id, 0) AS manager
FROM employees
WHERE department_id IS NOT NULL;Intermediate SQL Interview Questions
16. What is the difference between a subquery and a JOIN?
A subquery is a query inside another query. A JOIN combines rows from multiple tables based on a related column. Many problems can be solved either way. JOINs are often more readable and efficient for combining data, while subqueries are useful for filtering based on calculated values.
17. What is a correlated subquery?
A correlated subquery depends on values from the outer query and runs once for each row of the outer query. For example, finding employees who earn more than their department's average:
SELECT e.name, e.salary
FROM employees e
WHERE e.salary > (
SELECT AVG(salary)
FROM employees
WHERE department_id = e.department_id
);18. What is the difference between ROW_NUMBER, RANK and DENSE_RANK?
All three are window functions that assign numbers to rows in order. If salaries are 100, 90, 90 and 80:
ROW_NUMBER: 1, 2, 3, 4 (always unique)
RANK: 1, 2, 2, 4 (skips numbers after ties)
DENSE_RANK: 1, 2, 2, 3 (no gaps after ties)
19. What is a CTE?
A Common Table Expression (CTE) is a temporary named result set defined with WITH. It makes complex queries easier to read.
WITH dept_avg AS (
SELECT department_id, AVG(salary) AS avg_salary
FROM employees
GROUP BY department_id
)
SELECT * FROM dept_avg WHERE avg_salary > 50000;20. What is a transaction? What are ACID properties?
A transaction is a group of SQL operations treated as a single unit. ACID properties ensure reliability:
Atomicity: All operations succeed, or none do.
Consistency: Data moves from one valid state to another.
Isolation: Concurrent transactions don't interfere with each other.
Durability: Once committed, changes survive failures.
SQL Query Writing Questions
21. Find the second-highest salary.
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);22. Find the Nth-highest salary.
SELECT DISTINCT salary
FROM (
SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk
FROM employees
) ranked
WHERE rnk = 3; -- change 3 to N23. Find duplicate records.
SELECT name, COUNT(*) AS count
FROM employees
GROUP BY name
HAVING COUNT(*) > 1;24. Find employees who earn more than their manager.
SELECT e.name
FROM employees e
JOIN employees m ON e.manager_id = m.id
WHERE e.salary > m.salary;This is a classic self-join question.
25. Find the highest-paid employee in each department.
SELECT name, department_id, salary
FROM (
SELECT name, department_id, salary,
RANK() OVER (PARTITION BY department_id ORDER BY salary DESC) AS rnk
FROM employees
) ranked
WHERE rnk = 1;Using RANK returns all employees tied for the top salary in a department.
How to Prepare for SQL Interviews
Practise on real tables. Install PostgreSQL or MySQL, or use an online SQL editor, and run every query yourself.
Solve SQL problems regularly on platforms like LeetCode and HackerRank, which have dedicated SQL sections.
Master joins, GROUP BY and window functions. Most interview queries use them.
Explain your query out loud. Interviewers want to hear your logic, not just see the answer.
Use SQL in a project. A real app with a database gives you stories to tell in interviews.
Practise answering technical questions under pressure with DevsUnite Mock Interviews.
Frequently Asked Questions
Is SQL asked in fresher software developer interviews?
Yes. SQL is commonly asked in fresher interviews for developer, backend, data analyst and QA roles, through concept questions and simple query-writing tasks.
What SQL topics are most important for interviews?
Focus on keys and constraints, joins, GROUP BY and HAVING, subqueries, window functions like RANK and DENSE_RANK, normalization, indexes and transactions.
How do I find the second-highest salary in SQL?
A common approach is to select the maximum salary that is less than the overall maximum. For the Nth-highest salary, use DENSE_RANK in a subquery and filter by the rank you need.
How long does it take to learn SQL for interviews?
With daily practice, most freshers can learn interview-level SQL in 2 to 4 weeks. Regular query practice matters more than reading theory.
Which database should I practise SQL on?
PostgreSQL and MySQL are both great choices. Core SQL works similarly across them, so focus on writing queries rather than worrying about which one you pick.
Final Thoughts
SQL is one of the easiest ways to score points in a fresher interview, because the questions are predictable and practice pays off quickly. Learn the concepts, write every query yourself and explain your logic clearly. Combine strong SQL with good DSA and projects, and you'll be well prepared.
👉 Make sure your resume highlights your SQL skills with the DevsUnite ATS Resume Checker, keep your coding practice consistent with the DSA Sheets, and see which companies to target in Product-Based Companies in India Hiring Freshers.
Practice
Practice this interview before the real one
Answer real interview questions out loud and get feedback on every answer from an AI interviewer.