SQL Interview Questions for Freshers (With Answers)

Almost every IT company that hires freshers asks SQL in the technical round. It does not matter whether the role is developer, tester, support engineer or data analyst — basic database questions are very commonly asked. The good news is that interviewers rarely ask very deep questions to freshers. They check whether you understand tables, keys, JOINs, filtering and grouping, and whether you can write simple queries on the spot.

This guide covers the most common sql interview questions for freshers with short, clear answers and example queries you can practise, using one sample table in most examples:

CREATE TABLE employees (
  id INT PRIMARY KEY,
  name VARCHAR(100),
  department VARCHAR(50),
  salary INT,
  manager_id INT
);

Table: employees

idnamedepartmentsalarymanager_id
1AaravIT60000NULL
2DiyaIT550001
3KabirHR400001
4MeeraFinance70000NULL
5RohanIT550001

Basic SQL Interview Questions for Freshers

1. What is SQL? What is the difference between a DBMS and an RDBMS?

SQL (Structured Query Language) is the standard language used to create, read, update and delete data in a database. A DBMS stores data in tables or files, while an RDBMS (like MySQL, PostgreSQL or Oracle) stores data in related tables and supports relationships between them using keys. In simple words, every RDBMS is a DBMS, but every DBMS is not an RDBMS.

2. What are a primary key, a foreign key and a unique key?

  • Primary key: A column (or set of columns) that uniquely identifies each row. It cannot contain NULL values, and each value must be unique. Example: id in the employees table.
  • Foreign key: A column that refers to the primary key of another table and creates a link between the two tables. Example: manager_id can refer to the id of another employee.
  • Unique key: A column where all values must be different, but unlike a primary key, it can accept one NULL value in most databases. The exact rule depends on the database system.

3. What is the difference between WHERE and HAVING?

WHERE filters rows before grouping, while HAVING filters groups after GROUP BY is applied. You cannot use aggregate functions like SUM() or COUNT() with WHERE, but you can use them with HAVING.

-- Departments with more than 1 employee
SELECT department, COUNT(*) AS total
FROM employees
GROUP BY department
HAVING COUNT(*) > 1;

4. What are the different types of JOINs?

A JOIN combines rows from two or more tables based on a related column.

  • INNER JOIN: Returns only the matching rows from both tables.
  • LEFT JOIN: Returns all rows from the left table, plus matching rows from the right table.
  • RIGHT JOIN: Returns all rows from the right table, plus matching rows from the left table.
  • FULL JOIN: Returns all rows from both tables (supported in PostgreSQL and SQL Server; MySQL does not support it directly).
-- Employee name 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;

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

  • DELETE removes rows one by one based on a condition. It can be rolled back, and you can use it with a WHERE clause.
  • TRUNCATE removes all rows from a table quickly. It cannot be used with WHERE and resets identity counters in most databases.
  • DROP removes the complete table structure from the database. The table itself stops existing.

Practical Query-Based SQL Interview Questions for Freshers

6. How do you find the second highest salary?

This is one of the most repeated sql interview questions for freshers. One simple way uses a subquery:

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

You can also use DENSE_RANK() in databases that support window functions:

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

Use DENSE_RANK() because two employees can share the same salary, as Diya and Rohan do in our table. ROW_NUMBER() assigns unique sequential numbers and never repeats or skips; RANK() gives tied rows the same rank and then skips the following rank(s); DENSE_RANK() gives tied rows the same rank without skipping.

7. What is the difference between GROUP BY and ORDER BY?

GROUP BY combines rows that have the same value into summary rows, usually with aggregate functions. ORDER BY only sorts the final result in ascending or descending order; it does not group anything.

-- Number of employees in each department, highest first
SELECT department, COUNT(*) AS total
FROM employees
GROUP BY department
ORDER BY total DESC;

8. How do you find duplicate records in a table?

Group the data by the column you want to check and keep only the groups that appear more than once:

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

9. What is the difference between COUNT(*) and COUNT(column)?

COUNT(*) counts all rows, including rows where values are NULL. COUNT(column) counts only the rows where that column is not NULL. In our table, COUNT(*) returns 5, while COUNT(manager_id) returns 3, because Aarav and Meera have NULL in manager_id.

10. How do you fetch employees whose name starts with the letter ‘A’?

Use the LIKE operator with the % wildcard, which matches any number of characters:

SELECT *
FROM employees
WHERE name LIKE 'A%';

Similarly, '%a' means the name ends with ‘a’, and '%a%' means the letter ‘a’ appears anywhere in the name.

11. What is a subquery? What is a correlated subquery?

A subquery is a query written inside another query. A correlated subquery depends on the outer query — it uses a value from the outer query and runs once for each row. Example: find employees who earn more than the average salary of their own department:

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

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

Both combine the results of two SELECT queries. UNION removes duplicate rows, so it does extra work and is slower. UNION ALL keeps all rows, including duplicates, and is faster. Use UNION ALL when you are sure duplicates are acceptable or impossible.

Concept Questions Interviewers Love to Ask

13. What is normalization?

Normalization is the process of organising database tables to reduce repeated data and avoid mistakes during insert, update and delete. The first three normal forms are usually enough for a fresher interview:

  • 1NF: Each cell holds a single value, and there are no repeating groups.
  • 2NF: The table is in 1NF, and every non-key column depends on the full primary key.
  • 3NF: The table is in 2NF, and non-key columns do not depend on other non-key columns.

Example: storing the department name in every employee row repeats data; keeping departments in a separate table linked by an ID is the normalised design.

14. How do you handle NULL values in SQL?

You cannot compare NULL with = or !=, because NULL means “unknown”. Use IS NULL and IS NOT NULL instead:

SELECT name
FROM employees
WHERE manager_id IS NULL;

To show a default value in place of NULL, use COALESCE(manager_id, 0) in most databases, or IFNULL(manager_id, 0) in MySQL.

15. How do you get the total salary paid by each department?

This simple query checks your grip on GROUP BY with aggregate functions:

SELECT department, SUM(salary) AS total_salary
FROM employees
GROUP BY department;

Common Mistakes Freshers Make in SQL Interviews

  • Writing WHERE with aggregate functions. Conditions on SUM() or COUNT() belong in HAVING, not WHERE.
  • Forgetting that NULL is not zero or empty. Rows with NULL silently disappear from COUNT(column), AVG() and comparisons, which confuses many candidates.
  • Using ROW_NUMBER() for the “nth highest salary” question without thinking about duplicate salaries. Interviewers often add a tie on purpose.
  • Answering only the definition. For every concept, be ready with one small example query. Practical answers create a much stronger impression.
  • Ignoring the order of execution: WHERE runs before GROUP BY, and HAVING runs after it — many output-prediction questions are based on this alone.

Frequently Asked Questions

How many days does it take to learn SQL for interviews?

If you already know basic programming, for the basics, one to two weeks of daily practice is a reasonable start for many freshers. Spend more time writing queries than reading theory.

Which SQL database should freshers learn first?

MySQL is a very common choice in Indian IT companies and coaching material, and its syntax transfers easily to PostgreSQL and SQL Server. Pick one database and practise properly instead of switching again and again.

Are JOINs really that important for freshers?

Yes. JOINs are asked in almost every technical round because real projects always fetch data from multiple tables. Practise INNER JOIN and LEFT JOIN until you can write them without thinking.

Do service-based companies ask advanced SQL to freshers?

Usually no. They focus on basics: keys, JOINs, GROUP BY, subqueries and simple real-life queries. Advanced topics like window functions are a bonus, not a requirement, at the fresher level.

Where can I practise these sql interview questions for freshers?

Install MySQL on your laptop, create the employees table from this article, and type every query yourself. Changing the data and predicting the output before running the query is the fastest way to learn.

Conclusion

SQL is one of the easiest subjects to score in during a fresher interview because the syllabus is small and the questions repeat. Master keys, JOINs, GROUP BY with HAVING, subqueries and NULL handling, and practise the query-writing questions until the syntax feels natural. If you can write the queries in this guide from memory and explain them in your own words, you are well prepared for the SQL round in most IT interviews.

About the Author

Ayush Rai

Ayush Rai is the Founder & Content Creator of NinjaWay, where he writes and reviews careers and placements guides for freshers. Based in Noida, India, he focuses on practical, easy-to-follow explanations that help new developers learn and grow. He also publishes PowerBean, a beginner-friendly coding guide for new developers.

View all posts by Ayush Rai

Leave a Comment