All guides

Interview preparation

SQL Interview Questions for Freshers With Simple Answers

The SQL questions freshers are asked most often, from joins and GROUP BY to window functions, each with a clear answer, a small example and tips for explaining your thinking.

By Prathmesh BobadePublished 21 October 2026 5 min read

SQL is the one skill that shows up in almost every analyst, developer and testing interview for freshers. The questions are rarely tricky, but interviewers pay close attention to how clearly you explain your approach. This guide covers the questions that appear again and again, with short answers you can adapt in your own words and small examples you can try on any practice database.

Start with the basics

Most interviews begin with definitions. Be ready to explain what SQL is, which is a language used to read and manage data stored in relational databases, and what a table, row and column are. Know the difference between a primary key, which uniquely identifies each row and cannot be empty, and a foreign key, which links a row to the primary key of another table. If you can describe a simple example, such as a customers table and an orders table connected by customer id, you will sound confident.

DELETE, TRUNCATE and DROP

This question tests whether you understand the effect of each command. DELETE removes selected rows and can use a WHERE condition. TRUNCATE removes all rows from a table quickly but keeps the table structure. DROP removes the whole table, including its structure. A good extra point is that DELETE can usually be rolled back inside a transaction, while TRUNCATE and DROP are more drastic and should be used carefully.

WHERE and HAVING

WHERE filters rows before they are grouped. HAVING filters the groups after aggregation. Because of this, you cannot use a function such as SUM in WHERE, but you can in HAVING. A short example makes the answer memorable:

SELECT city, SUM(amount) AS revenue
FROM orders
WHERE status = 'Paid'
GROUP BY city
HAVING SUM(amount) > 50000;

Here the paid orders are selected first, then grouped by city, and only the cities with revenue above fifty thousand are kept.

Joins explained simply

An INNER JOIN returns only the rows that have a match in both tables. A LEFT JOIN returns every row from the left table and the matching rows from the right table, with empty values where there is no match. A RIGHT JOIN is the opposite, and a FULL JOIN returns everything from both sides. A common follow-up is how to find customers who have never placed an order. The answer is a LEFT JOIN from customers to orders with a condition that the order id is empty.

  • Use INNER JOIN when you only care about matching records.
  • Use LEFT JOIN when you want to keep every record from the main table.
  • After any join, compare the row count with what you expected. A sudden increase usually means a one-to-many relationship.

Finding duplicates and removing them

Interviewers love this because it combines grouping and filtering. To find duplicates, group by the column that should be unique and keep the groups that appear more than once using HAVING COUNT greater than one. To remove them, you can use a window function such as ROW_NUMBER to number the rows inside each group and then delete the ones numbered above one. Even if you do not remember the exact syntax, explain the idea clearly.

The second highest salary

This classic question has several valid answers. One approach selects the maximum salary that is lower than the overall maximum. Another uses a window function, giving each salary a rank and picking rank two. Mention that DENSE_RANK is safer than RANK when several people can share the same salary, because RANK leaves gaps after ties.

Window functions

Window functions calculate a value across a set of related rows without collapsing them into one row. ROW_NUMBER gives a unique number to each row, RANK and DENSE_RANK handle ties differently, and LAG and LEAD read the previous or next row. A running total uses SUM with an OVER clause ordered by date. If asked when you would use them, say that they are ideal for rankings, comparisons with the previous period, and running totals.

Subqueries and CTEs

A subquery is a query inside another query. A common table expression, written with the WITH keyword, gives a name to a subquery so that long logic reads from top to bottom. Interviewers like to hear that you use CTEs to make complex queries easier to read and to reuse an intermediate result.

Handling NULL values

NULL means unknown, not zero or an empty string. You cannot compare it using an equals sign, so you must write IS NULL or IS NOT NULL. COALESCE returns the first value that is not null and is useful for replacing missing values. Remember that aggregate functions such as AVG ignore nulls, which can change your result.

Normalisation and indexes

Normalisation organises data into separate tables to reduce repetition and keep it consistent. An index works like the index of a book: it helps the database find rows faster, but it uses extra storage and can slow down inserts and updates. A balanced answer shows you understand the trade-off.

How to practise and answer

Practise writing queries by hand on a small dataset every day for two weeks, and focus on joins, grouping and window functions. In the interview, repeat the question in your own words, state the tables and columns you will use, write the query step by step, and then check it with a small example. If you make a mistake, calmly correct it. Interviewers value a candidate who can debug, so keep practising on the coding and interview sections of Hire Daily and build the habit of explaining your thinking aloud.

Keep reading