🎯 Placement season: Python & DSA for Placement — recorded lessons + live classes · from ₹1,499Start preparing →
+91 95189 33742 · WhatsApp Request a call back·Student help desk·Log in·Sign up free
Placement Prep

25 SQL Interview Questions for Freshers (2026) — With Simple Answers and Queries

By CodeMBS · 02 Dec 2026

SQL shows up in almost every fresher interview today — for software developer, data analyst, tester, support and even business analyst roles. The good news is that interviewers ask a fairly predictable set of questions. Here are 25 SQL interview questions for freshers in 2026, with short answers and example queries you can practise on any database like MySQL or PostgreSQL.

Key takeaways

  • SQL (Structured Query Language) is used to store, read and change data in relational databases such as MySQL, PostgreSQL, Oracle and SQL Server.
  • Fresher interviews usually focus on keys, joins, GROUP BY with HAVING, subqueries and the difference between DELETE, TRUNCATE and DROP.
  • Window functions like ROW_NUMBER() and RANK() are now common in data analyst and GCC interviews.
  • Writing queries by hand on a small sample table is the fastest way to prepare.

For the examples, imagine a table employees(id, name, dept_id, salary, manager_id) and a table departments(id, dept_name).

Basic SQL interview questions

1. What is SQL?

SQL is a standard language for working with relational databases. You use it to create tables, insert and update data, and query data with conditions.

2. What is the difference between SQL and MySQL?

SQL is the language. MySQL is a database management system (software) that understands SQL. PostgreSQL, Oracle and SQL Server are other such systems.

3. What are DDL, DML, DCL and TCL?

DDL defines structure (CREATE, ALTER, DROP). DML changes data (INSERT, UPDATE, DELETE). DCL controls access (GRANT, REVOKE). TCL manages transactions (COMMIT, ROLLBACK, SAVEPOINT). SELECT is sometimes listed separately as DQL.

4. What is a primary key?

A column (or set of columns) that uniquely identifies each row. It cannot be NULL and cannot repeat. A table has only one primary key.

5. What is a foreign key?

A column that refers to the primary key of another table. For example, employees.dept_id refers to departments.id. It keeps data between tables consistent.

6. What is the difference between a primary key and a unique key?

Both prevent duplicates. A table can have only one primary key but many unique keys, and a unique key column can usually hold NULL values.

7. What is the difference between DELETE, TRUNCATE and DROP?

DELETE removes selected rows (with WHERE) and can be rolled back inside a transaction. TRUNCATE removes all rows quickly but keeps the table structure. DROP removes the whole table, structure and all.

8. 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.

9. What are aggregate functions?

Functions that return one value from many rows: COUNT, SUM, AVG, MIN and MAX.

10. What is NULL? How do you check for it?

NULL means a missing or unknown value — not zero or an empty string. Use IS NULL or IS NOT NULL; = NULL does not work.

Joins and query questions

11. What are the types of joins?

INNER JOIN returns matching rows from both tables. LEFT JOIN returns all rows from the left table plus matches. RIGHT JOIN does the opposite. FULL OUTER JOIN returns all rows from both. CROSS JOIN returns every combination. A self join joins a table to itself.

12. Write a query to show each employee with their department name.

SELECT e.name, d.dept_name
FROM employees e
INNER JOIN departments d ON e.dept_id = d.id;

13. Find employees who have no department assigned.

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

14. Show each employee with their manager’s name (self join).

SELECT e.name AS employee, m.name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.id;

15. Count employees in each department and show only departments with more than 5.

SELECT dept_id, COUNT(*) AS total
FROM employees
GROUP BY dept_id
HAVING COUNT(*) > 5;

16. Find the second highest salary.

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

Interviewers often ask for a second method too — see the DENSE_RANK() answer below.

17. Find duplicate names in a table.

SELECT name, COUNT(*)
FROM employees
GROUP BY name
HAVING COUNT(*) > 1;

18. What is the difference between UNION and UNION ALL?

UNION combines results and removes duplicates. UNION ALL keeps duplicates and is faster.

19. What is a subquery? What is a correlated subquery?

A subquery is a query inside another query. A correlated subquery uses a value from the outer query, so it runs once per outer row. Example — employees earning above their department average:

SELECT name, salary
FROM employees e
WHERE salary > (SELECT AVG(salary) FROM employees
                WHERE dept_id = e.dept_id);

Intermediate questions (common in data and GCC roles)

20. What are window functions?

Window functions calculate a value across a set of related rows without collapsing them into one row, unlike GROUP BY. Common ones are ROW_NUMBER(), RANK(), DENSE_RANK(), LAG() and LEAD(). See the PostgreSQL window functions tutorial for more.

21. What is the difference between RANK() and DENSE_RANK()?

Both give tied rows the same rank. RANK() leaves gaps after ties (1, 1, 3); DENSE_RANK() does not (1, 1, 2). Nth highest salary using DENSE_RANK():

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

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

SELECT name, dept_id, salary FROM (
  SELECT name, dept_id, salary,
         ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rn
  FROM employees
) t
WHERE rn = 1;

23. What is an index?

An index is a data structure that helps the database find rows faster, like a book index. It speeds up SELECT queries but uses extra storage and can slow down inserts and updates.

24. What is normalization?

Normalization organises tables to reduce duplicate data. 1NF means atomic values with no repeating groups; 2NF removes partial dependency on part of a composite key; 3NF removes transitive dependency (non-key columns depending on other non-key columns).

25. What are ACID properties?

They make transactions reliable: Atomicity (all or nothing), Consistency (data stays valid), Isolation (transactions do not interfere), and Durability (committed data survives crashes).

How to prepare for SQL interviews

  • Install MySQL or use an online SQL editor and create the two sample tables above with 15–20 rows.
  • Write every query in this list yourself, then change it (third highest salary, departments with no employees, and so on).
  • Learn to explain the output in plain words — interviewers check understanding, not memory.
  • Pair SQL with Python and Excel if you are targeting analyst roles. Our covers SQL, Excel and Power BI step by step, and the Python for Placement course covers the coding round.
  • Practise aptitude and coding tests too — see our placement preparation page for company-wise patterns.

Related guides

FAQ

Is SQL enough to get a job as a fresher?

SQL alone is rarely enough, but combined with Python or Java, Excel and basic statistics it opens doors to developer, analyst and testing roles.

Which database should I practise on?

MySQL or PostgreSQL. Both are free and use standard SQL, so what you learn carries over to Oracle and SQL Server.

How long does it take to prepare SQL for interviews?

With one to two hours of daily practice, most freshers can cover these questions comfortably in two to three weeks.

Do service companies like TCS and Infosys ask SQL?

SQL and DBMS basics often come up in technical interviews at service companies, especially questions on keys, joins and normalization.

Conclusion

SQL is one of the most reliable skills a fresher can add in 2026. Master these 25 questions, practise writing queries by hand, and you will walk into technical rounds with confidence. For guided practice with mentors, explore CodeMBS membership.

Preparing for placements? Python for Placement and DSA for Placement — recorded lessons, live classes with faculty, topic tests and a certificate. See the placement courses →

📢 Get verified job alerts every day — free

Fresher drives, internships and IT jobs with official apply links only. No fake links, no fees.

Share with friends:WhatsAppTelegramLinkedIn

More articles

CodeMBS Placement Courses

Get placement-ready with Python & DSA.

Python for Placement and DSA for Placement — recorded lessons you can watch anytime, regular live classes with faculty, topic tests and a certificate. Built around the coding rounds and technical interviews of campus placements.

Python & DSA for Placementfrom ₹1,999 ₹1,499
View courses