Top SQL Interview Questions for Freshers (2026) — Queries & Answers
Updated August 2026
SQL is the most reliably asked topic in service-company interviews — TCS, Cognizant Elevate and Accenture all test it, and it is the easiest place to score if you have practised actual queries.
Interviewers ask you to write queries, not define terms. Every answer below is built around the query you would write.
Frequently asked questions
Write a query to find the second-highest salary.
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); — or with window functions: SELECT salary FROM (SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) r FROM employees) t WHERE r = 2. Know both.
Difference between INNER JOIN and LEFT JOIN?
INNER JOIN returns only rows that match in both tables. LEFT JOIN returns all rows from the left table, with NULLs where the right table has no match. Follow-up: RIGHT and FULL OUTER complete the family.
WHERE vs HAVING?
WHERE filters rows before grouping; HAVING filters groups after GROUP BY. "Departments with more than 5 employees" needs HAVING COUNT(*) > 5.
Primary key vs unique key vs foreign key?
Primary key: uniquely identifies rows, no NULLs, one per table. Unique key: also enforces uniqueness but allows a NULL and a table can have several. Foreign key: references another table's key to enforce referential integrity.
DELETE vs TRUNCATE vs DROP?
DELETE removes selected rows, can have WHERE, is logged and rollback-able. TRUNCATE removes all rows fast, no WHERE. DROP removes the table itself, structure included.
What is normalization? Explain up to 3NF.
1NF: atomic values, no repeating groups. 2NF: 1NF plus no partial dependency on part of a composite key. 3NF: 2NF plus no transitive dependencies (non-key columns depending on other non-key columns). It reduces redundancy and update anomalies.
Write a query to find duplicate values in a column.
SELECT email, COUNT(*) FROM users GROUP BY email HAVING COUNT(*) > 1; — the GROUP BY + HAVING pattern answers a whole family of interview questions.
What are indexes and when do they hurt?
Indexes (usually B-trees) speed up reads on the indexed columns but slow down writes and take space, because every INSERT/UPDATE must maintain them. Index columns you filter and join on frequently.
What is a subquery vs a JOIN — which is better?
Both can express the same logic; JOINs are usually more efficient and readable for combining tables, while subqueries shine for existence checks (EXISTS) and single aggregated values. Say: "I would check the execution plan."
UNION vs UNION ALL?
UNION merges result sets and removes duplicates (costs a sort/hash); UNION ALL keeps duplicates and is faster. Use UNION ALL unless you specifically need de-duplication.
What is an ACID transaction?
Atomicity (all or nothing), Consistency (valid state to valid state), Isolation (concurrent transactions don't interfere), Durability (committed data survives crashes). Give the bank-transfer example.
Don't just read SQL questions — get asked them
Phiny's AI interviews you on exactly these topics, follows up on weak answers, and tells you what a stronger answer looks like. Text interviews are free and unlimited.
Start a free AI mock interviewHow to prepare
- Write 30–40 queries by hand against a sample schema (employees/departments) before your interview week — recognition is not recall.
- When asked a query, say the clause order aloud as you write: SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY.
- The second-highest-salary question appears in some form in a majority of fresher interviews. Master its variants (Nth highest, per department).
Where these questions get asked
- TCS NQT guide and Infosys hiring guide — the two biggest exams these questions appear in.
- All company placement guides — pattern, syllabus and rounds for every mass recruiter.