Programming Languages (Freshers)

SQL Interview Questions & Answers for Freshers (2026)

SQL is asked in almost every data, backend and analyst interview. These fresher questions cover querying, joins, aggregation and indexing with clear answers and a practice path.

SQL Interview Questions & Answers

Q1.What is the difference between WHERE and HAVING?medium

WHERE filters rows before grouping and cannot use aggregate functions. HAVING filters groups after GROUP BY and can use aggregates like COUNT or SUM. Example: WHERE country = "IN" then GROUP BY city HAVING COUNT(*) > 10.

Q2.Explain the types of JOIN.medium

INNER JOIN returns matching rows in both tables. LEFT JOIN returns all left rows plus matches (NULLs where none). RIGHT JOIN is the mirror. FULL OUTER JOIN returns all rows from both. CROSS JOIN returns the Cartesian product.

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

DELETE removes rows (optionally with WHERE) and is logged/rollback-able. TRUNCATE quickly removes all rows, cannot use WHERE, and resets identity. DROP removes the entire table (structure and data).

Q4.What is a primary key vs a foreign key?easy

A primary key uniquely identifies each row in a table and cannot be NULL. A foreign key is a column that references a primary key in another table, enforcing referential integrity between related tables.

Q5.What is an index and why use it?medium

An index is a data structure (usually a B-tree) that speeds up lookups and sorting on a column, at the cost of extra storage and slower writes. Index columns used in WHERE, JOIN and ORDER BY; avoid over-indexing.

Q6.What is the difference between GROUP BY and ORDER BY?easy

GROUP BY aggregates rows that share values into summary rows (used with COUNT, SUM, AVG). ORDER BY sorts the result set ascending (default) or descending. They serve different purposes and can be used together.

Q7.What are aggregate functions?easy

Functions that compute a single value over a set of rows: COUNT, SUM, AVG, MIN, MAX. They are commonly used with GROUP BY to summarise data per group.

Q8.What is a subquery? Explain correlated vs non-correlated.hard

A subquery is a query nested inside another. A non-correlated subquery runs once and its result is used by the outer query. A correlated subquery references the outer query and runs once per outer row, so it is usually slower.

Q9.What is normalization?medium

Organising tables to reduce redundancy and improve integrity. 1NF: atomic values; 2NF: no partial dependency on part of a composite key; 3NF: no transitive dependency on non-key columns. Denormalization trades redundancy for read speed.

Q10.How do you find the second highest salary?medium

A common approach: SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees); or use DENSE_RANK() OVER (ORDER BY salary DESC) and filter rank = 2. Mention handling of duplicates.

Q11.What is the difference between UNION and UNION ALL?easy

UNION combines result sets and removes duplicates (extra sort cost). UNION ALL combines them keeping duplicates and is faster. Both require the same number and compatible types of columns.

SQL — Self-Learning Path

1. SELECT basics

SELECT, FROM, WHERE, DISTINCT, ORDER BY, LIMIT. Practise filtering with AND/OR, IN, BETWEEN, LIKE and IS NULL.

2. Aggregation

COUNT, SUM, AVG, MIN, MAX with GROUP BY and HAVING. Learn to summarise data per category.

3. Joins

INNER, LEFT, RIGHT and FULL joins across multiple tables. Understand keys and how rows match.

4. Subqueries & set operations

Subqueries in WHERE/FROM/SELECT, EXISTS/IN, and UNION/INTERSECT/EXCEPT.

5. Window functions & design

ROW_NUMBER, RANK, DENSE_RANK, and running totals; plus indexing and normalization basics.

6. Practice

Solve query challenges on a sample schema (employees, orders, products). Write the "second highest", "top N per group" and "running total" queries from memory.

Interview Tips

  • Always state your assumptions about the schema before writing a query.
  • Know how each JOIN behaves with NULLs.
  • Practise the classic "Nth highest" and "top N per group" problems.
  • Mention indexing when asked about slow queries.

Ready to apply what you’ve learned?

Browse fresh internships and first jobs in SQL and related fields.

Browse Internships