25 SQL Interview Questions for Freshers
The 25 SQL questions freshers meet most often, with short answers on keys, joins, NULL, indexes and classic query puzzles.
Short answer: These 25 SQL interview questions for freshers cover what interviewers ask most: basic definitions, keys, joins, grouping, NULL, indexes, and a few classic query puzzles. Read the one-line answers, then type each query yourself in a real database, because interviewers usually ask you to write SQL, not recite it.
Basics: what do freshers get asked first?
1. What is SQL? Structured Query Language, the standard language for storing, reading and changing data in relational databases. See What is SQL and what is it used for.
2. What is a primary key? A column (or set of columns) that uniquely identifies each row. It cannot be NULL and cannot repeat.
3. What is a foreign key? A column that points to the primary key of another table, which links the two tables and keeps the data consistent.
4. DELETE vs TRUNCATE vs DROP? DELETE removes chosen rows (it accepts WHERE). TRUNCATE empties the whole table but keeps its structure. DROP removes the table itself.
5. WHERE vs HAVING? WHERE filters rows before grouping. HAVING filters groups after GROUP BY, so it can use aggregates like COUNT(*).
6. How does NULL behave? NULL means unknown, not zero or empty text. Use
IS NULL, because= NULLis never true.7. What does DISTINCT do? It removes duplicate rows from the result.
8. What is LIKE? Pattern matching on text.
%matches any number of characters,_matches exactly one.
Joins and aggregation: what is the difference between the common joins?
9. INNER JOIN? Returns only rows that have a match in both tables.
10. LEFT JOIN? Returns every row from the left table, plus matches from the right. Unmatched right-side columns are NULL.
11. Other joins? RIGHT JOIN is the mirror of LEFT. FULL JOIN keeps unmatched rows from both sides (not available in every database). CROSS JOIN pairs every row with every row.
12. What does GROUP BY do? It collapses rows with the same value into one group so you can apply COUNT, SUM, AVG, MIN or MAX per group.
13. COUNT(*) vs COUNT(column)? COUNT(*) counts rows. COUNT(column) counts only rows where that column is not NULL.
14. UNION vs UNION ALL? UNION removes duplicates. UNION ALL keeps them and is usually faster.
15. How do you sort and limit?
ORDER BY salary DESCsorts. ThenLIMIT 5(orTOP 5/FETCH FIRSTin some databases) keeps the first rows.
Design and performance: which concepts do interviewers like?
16. What is normalization? Organizing tables to reduce duplicated data, for example storing a customer once and referring to it by id.
17. What is an index? A data structure that speeds up lookups on a column. The trade-off is extra storage and slower writes.
18. What is a view? A saved query that behaves like a virtual table.
19. Name some constraints. PRIMARY KEY, FOREIGN KEY, UNIQUE, NOT NULL, CHECK and DEFAULT.
20. What is a subquery? A query inside another query, for example in WHERE or FROM.
21. What is a transaction (ACID)? A group of statements that succeed or fail together. ACID stands for atomicity, consistency, isolation and durability.
Which query puzzles come up again and again?
22. Second-highest salary. Select the maximum salary that is below the overall maximum (code below).
23. Find duplicates. GROUP BY the column and keep groups with
HAVING COUNT(*) > 1.24. What is a window function? It calculates across related rows without collapsing them, for example
RANK() OVER (PARTITION BY dept ORDER BY salary DESC).25. What is a CTE? A named temporary result introduced with
WITH, which makes long queries easier to read.
These three queries use a table employees(id, name, dept, salary):
-- Second-highest salary
SELECT MAX(salary) FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);
-- Duplicate names
SELECT name, COUNT(*) FROM employees
GROUP BY name HAVING COUNT(*) > 1;
-- Rank inside each department
SELECT name, dept, salary,
RANK() OVER (PARTITION BY dept ORDER BY salary DESC) AS dept_rank
FROM employees;Window functions need a reasonably modern database version; most current ones support them. To practise joins in depth, read SQL vs NoSQL for the bigger picture of where SQL fits.
Interview tip and common mistakes
Writing
WHERE salary = NULLinstead ofIS NULL.Using WHERE with an aggregate such as
WHERE COUNT(*) > 1. Use HAVING.Selecting a non-grouped column next to an aggregate without putting it in GROUP BY.
Memorizing answers. Say the idea in your own words, then write a small example on the whiteboard or editor.
Key Takeaway
Know the keys, joins, WHERE vs HAVING, NULL and a handful of classic queries, and you can answer most fresher SQL questions. Practise by typing queries, not just reading them.
Ready to start? Go beyond the basics with joins, window functions, CTEs and query design for real data work. Begin with the free course SQL and Data Handling for AI: Advanced.
Related articles
More in SQL and Data
SQL vs Python for Data Analysis
SQL and Python do different jobs in data analysis. See what each is best at, a side-by-side example, and which one to learn first.
4 min readNov 15, 2026Primary Key vs Foreign Key in SQL
What primary and foreign keys are, how they differ, and how a runnable customers-and-orders example shows the database enforcing them.
4 min readNov 13, 2026WHERE vs GROUP BY vs HAVING in SQL
WHERE filters rows, GROUP BY builds groups and HAVING filters groups. See one worked example, the order SQL runs them in, and the mistakes to avoid.
3 min readNov 10, 2026MySQL vs PostgreSQL vs SQLite: Which Should You Learn?
SQL is the language; MySQL, PostgreSQL and SQLite are database systems. See how they differ and which one to learn first.
3 min readNov 5, 2026SQL vs NoSQL: Differences and When to Use Each
SQL stores data in structured tables, NoSQL in flexible documents, key-values or graphs. Here are the differences, a comparison table and a simple guide to choosing.
4 min readNov 1, 2026What Is SQL and What Is It Used For?
SQL is the standard language for working with relational databases. Learn what it is, the four core statements, and why AI engineers need it.
3 min readOct 30, 2026
Comments
Sign in to join the discussion.

