SQL Interview Questions for Freshers 2027 with Answers

sql-interview-questions-freshers
sql-interview-questions-freshers

 

SQL Interview Questions for Freshers 2027 with Answers and Sample Queries

Last reviewed: November 2026 | Written by: Prashant Kumar, founder of GetYourFirstJob

SQL is one of the few topics that appears in almost every fresher hiring process in India, from written tests to technical interviews. Candidates who shared their experience for the Capgemini Exceller process mentioned SQL along with project discussion, and many service-company interviews start with a few SQL questions even when the job is not a database role. The reason is simple: SQL is quick to test, and it shows whether you can think in a logical way.

This guide gives you 30 SQL interview questions that freshers are commonly asked, with short answers you can say in an interview and sample queries you can run yourself. All examples use the same small practice tables, so you can copy the data into MySQL, PostgreSQL or any free online SQL editor and check every result with your own eyes. That is the best way to learn: do not just read a query, run it and change it.

A note before you start: exact questions change from company to company and from drive to drive. Use this article to build your fundamentals, and check the official notification and recent candidate reports for the drive you are applying to.

Contents

  1. Practice tables used in this article
  2. Basic SQL questions (1 to 10)
  3. Joins and relationships (11 to 13)
  4. Grouping, deleting and combining data (14 to 17)
  5. Most asked SQL query questions (18 to 22, 30)
  6. Database concepts (23 to 26)
  7. Window functions, CASE and execution order (27 to 29)
  8. 7-day SQL practice plan
  9. Mistakes freshers make in SQL interviews
  10. Frequently asked questions

Practice Tables Used in This Article

Create these two tables and insert the rows below. Every answer in this guide uses this data.

CREATE TABLE departments (
  dept_id INT PRIMARY KEY,
  dept_name VARCHAR(50)
);

CREATE TABLE employees (
  emp_id INT PRIMARY KEY,
  emp_name VARCHAR(50),
  dept_id INT,
  salary INT,
  manager_id INT,
  FOREIGN KEY (dept_id) REFERENCES departments(dept_id)
);

INSERT INTO departments VALUES (1, 'HR'), (2, 'IT'), (3, 'Finance'), (4, 'Marketing');

INSERT INTO employees VALUES
(101, 'Asha',   2,    60000, NULL),
(102, 'Rahul',  2,    45000, 101),
(103, 'Neha',   1,    40000, 101),
(104, 'Vikram', 3,    55000, 101),
(105, 'Pooja',  2,    45000, 102),
(106, 'Karan',  NULL, 30000, 103);

Things to notice in this data: Karan has no department (NULL), Marketing has no employees, Rahul and Pooja have the same salary, and Asha has no manager. These small details are what make interview questions interesting, so keep them in mind.

sql-interview-questions-freshers
sql-interview-questions-freshers

Basic SQL Questions (1 to 10)

1. What is SQL?

SQL stands for Structured Query Language. It is the standard language used to create, read, update and delete data in a relational database. You use it to talk to systems such as MySQL, PostgreSQL, Oracle and SQL Server. The core idea is that you describe what data you want, and the database decides how to find it.

2. What is an RDBMS?

An RDBMS is a Relational Database Management System. It stores data in tables made of rows and columns, and the tables are connected through keys. In our practice data, the employees table and the departments table are connected through dept_id.

3. What are DDL, DML, DCL and TCL?

These are the four groups of SQL commands. DDL (Data Definition Language) changes structure: CREATE, ALTER, DROP, TRUNCATE. DML (Data Manipulation Language) works with the data: INSERT, UPDATE, DELETE, and SELECT in most textbooks. DCL (Data Control Language) controls permission: GRANT and REVOKE. TCL (Transaction Control Language) manages transactions: COMMIT, ROLLBACK and SAVEPOINT.

4. What is a primary key?

A primary key is a column, or a group of columns, that identifies each row in a table in a unique way. It cannot contain duplicate values and it cannot be NULL. A table can have only one primary key. In our data, emp_id is the primary key of employees.

5. What is a foreign key?

A foreign key is a column that points to the primary key of another table. It creates a link between the two tables and keeps data consistent, because you cannot add a department that does not exist. Here, employees.dept_id is a foreign key that refers to departments.dept_id.

6. What is the difference between a primary key and a unique key?

Both keep values unique, but there are two main differences. A table can have only one primary key but many unique keys. A primary key never accepts NULL, while a unique key usually accepts NULL (the exact number of NULLs allowed depends on the database system).

7. What is the difference between WHERE and HAVING?

WHERE filters individual rows before grouping happens. HAVING filters groups after GROUP BY has been applied, so it can use aggregate functions such as COUNT or SUM.

-- WHERE: filter rows first
SELECT emp_name FROM employees WHERE salary > 40000;

-- HAVING: filter groups
SELECT dept_id, COUNT(*) AS total
FROM employees
GROUP BY dept_id
HAVING COUNT(*) > 1;

The second query returns only dept_id 2 (IT) with a count of 3.

8. What does DISTINCT do?

DISTINCT removes duplicate rows from the result. For example, SELECT DISTINCT salary FROM employees; returns 60000, 55000, 45000, 40000 and 30000, and the value 45000 appears only once even though two employees have it.

9. How do you handle NULL values?

NULL means unknown or missing. It is not zero and not an empty string. You cannot compare it with an equal sign. The condition dept_id = NULL never works. Use IS NULL or IS NOT NULL instead.

SELECT emp_name FROM employees WHERE dept_id IS NULL;   -- Karan
SELECT emp_name, COALESCE(dept_id, 0) AS dept FROM employees;

COALESCE returns the first value that is not NULL, so it is useful for replacing NULL with a default.

10. How does LIKE work?

LIKE searches for a pattern in text. The percent sign (%) matches any number of characters and the underscore (_) matches exactly one character.

SELECT emp_name FROM employees WHERE emp_name LIKE 'A%';    -- starts with A: Asha
SELECT emp_name FROM employees WHERE emp_name LIKE '%a';    -- ends with a: Asha, Neha, Pooja
SELECT emp_name FROM employees WHERE emp_name LIKE '_a%';   -- second letter a: Rahul, Karan

Joins and Relationships (11 to 13)

11. What is an INNER JOIN?

An INNER JOIN returns only the rows that have a match in both tables.

SELECT e.emp_name, d.dept_name
FROM employees e
INNER JOIN departments d ON e.dept_id = d.dept_id;

This returns five rows. Karan is missing because his dept_id is NULL and cannot match anything, and Marketing is missing because no employee belongs to it.

12. What is the difference between LEFT, RIGHT and FULL JOIN?

A LEFT JOIN keeps every row from the left table and fills NULL where the right table has no match. A RIGHT JOIN does the opposite. A FULL JOIN keeps unmatched rows from both sides (MySQL does not support FULL JOIN directly, so a UNION of LEFT and RIGHT is used there).

-- All employees, even without a department
SELECT e.emp_name, d.dept_name
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.dept_id;

-- Departments that have no employees
SELECT d.dept_name
FROM departments d
LEFT JOIN employees e ON d.dept_id = e.dept_id
WHERE e.emp_id IS NULL;

The first query returns Karan with a NULL department. The second returns Marketing. The second pattern, a LEFT JOIN followed by IS NULL, is a very common interview question.

13. What is a self join?

A self join joins a table with itself. It is used when rows in a table relate to other rows in the same table, such as an employee and a manager.

SELECT e.emp_name AS employee, m.emp_name AS manager
FROM employees e
LEFT JOIN employees m ON e.manager_id = m.emp_id;

Rahul, Neha and Vikram report to Asha. Pooja reports to Rahul and Karan reports to Neha. Asha appears with a NULL manager because we used a LEFT JOIN.

Grouping, Deleting and Combining Data (14 to 17)

14. How does GROUP BY work?

GROUP BY puts rows with the same value into one group so that you can apply aggregate functions such as COUNT, SUM, AVG, MIN and MAX to each group.

SELECT dept_id, COUNT(*) AS total, AVG(salary) AS avg_salary
FROM employees
GROUP BY dept_id;

Remember the rule: every column in SELECT that is not inside an aggregate function must also appear in GROUP BY. Also note that the NULL department forms its own group.

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

DELETE removes selected rows (you can use WHERE), it is a DML command, and in most systems it can be rolled back inside a transaction. TRUNCATE removes all rows quickly and keeps the table structure, and it is a DDL command that has no WHERE clause. DROP removes the whole table, including its structure. Rollback behaviour for TRUNCATE and DROP differs between database systems, so say this carefully in an interview.

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

UNION combines the results of two queries and removes duplicates. UNION ALL combines them and keeps duplicates, so it is faster. Both queries must return the same number of columns with compatible data types.

17. What is the difference between a subquery and a join?

A subquery is a query inside another query. A join combines columns from two tables in one result. Many problems can be solved both ways. Joins are often easier to read and optimise when you need columns from both tables, while subqueries are convenient for filters such as “salary greater than the average”.

SELECT emp_name, salary
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

The average salary is about 45833, so this returns Asha and Vikram.

Most Asked SQL Query Questions (18 to 22 and 30)

18. Find the second highest salary.

This is probably the most asked SQL question for freshers. Know at least two methods.

-- Method 1: subquery
SELECT MAX(salary) AS second_highest
FROM employees
WHERE salary < (SELECT MAX(salary) FROM employees);

-- Method 2: LIMIT with OFFSET (MySQL, PostgreSQL)
SELECT DISTINCT salary
FROM employees
ORDER BY salary DESC
LIMIT 1 OFFSET 1;

Both return 55000. Notice the DISTINCT in method 2. Without it, a repeated top salary would be returned as the second row, which gives the wrong answer. In SQL Server you would use TOP or OFFSET FETCH, and in Oracle you would use FETCH FIRST, because LIMIT is not available everywhere.

19. Find the Nth highest salary.

The cleanest method uses DENSE_RANK, which handles ties correctly.

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

For N = 3 the answer is 45000. Because Rahul and Pooja share that salary, a plain ROW_NUMBER would break this, but DENSE_RANK gives both the same rank without skipping numbers.

20. How do you find duplicate records?

Group by the column that should be unique and keep groups with a count greater than one.

SELECT salary, COUNT(*) AS times
FROM employees
GROUP BY salary
HAVING COUNT(*) > 1;

This returns 45000 with a count of 2. In a real table you would group by something like email or phone number.

21. How do you delete duplicate records but keep one?

Keep the row with the smallest id for each duplicate group and delete the rest.

DELETE FROM employees
WHERE emp_id NOT IN (
  SELECT keep_id FROM (
    SELECT MIN(emp_id) AS keep_id
    FROM employees
    GROUP BY emp_name
  ) AS t
);

The extra wrapper query is needed in MySQL, which does not allow you to select from the same table that you are deleting from in a direct subquery. Always run the matching SELECT first and check what will be deleted before you run a DELETE.

22. Find the highest salary in each department.

SELECT d.dept_name, MAX(e.salary) AS highest_salary
FROM employees e
JOIN departments d ON e.dept_id = d.dept_id
GROUP BY d.dept_name;

The result is IT 60000, HR 40000 and Finance 55000. If the interviewer also asks for the name of the employee, use a window function (see question 27) or a subquery, because adding emp_name to the query above would break the GROUP BY rule.

30. Find employees who earn more than their manager.

This is a self join question that interviewers like because it tests both joins and logic.

SELECT e.emp_name
FROM employees e
JOIN employees m ON e.manager_id = m.emp_id
WHERE e.salary > m.salary;

In our data this returns no rows, because every manager earns more than their team members. Always say this out loud: an empty result is a valid answer. To test your query, change one salary and run it again.

Database Concepts (23 to 26)

23. What is an index?

An index is a data structure that helps the database find rows faster without scanning the whole table, like the index at the back of a book. It speeds up reads (SELECT with WHERE, JOIN, ORDER BY) but slows down writes (INSERT, UPDATE, DELETE) a little and uses extra storage. So you add indexes on columns that are searched often, not on every column.

24. What is a view?

A view is a saved query that behaves like a virtual table. It does not store its own data in the normal case; it shows the result of the query each time. Views are used to simplify complex queries and to hide sensitive columns from some users.

CREATE VIEW it_employees AS
SELECT emp_id, emp_name, salary
FROM employees
WHERE dept_id = 2;

25. What is normalization?

Normalization is the process of organising data into separate tables to reduce repetition and avoid update problems. First Normal Form (1NF) means each cell holds one value and there are no repeating groups. Second Normal Form (2NF) means 1NF plus no column depends on only part of a composite key. Third Normal Form (3NF) means 2NF plus no non-key column depends on another non-key column. In our practice data, storing dept_name in the departments table instead of repeating it in every employee row is normalization in action.

26. What are ACID properties?

ACID describes the guarantees of a reliable transaction. Atomicity means all steps happen or none happen. Consistency means the database moves from one valid state to another. Isolation means transactions running at the same time do not break each other. Durability means that once a transaction is committed, it stays saved even if the system crashes. A bank transfer is the classic example: money must not leave one account unless it reaches the other.

Window Functions, CASE and Execution Order (27 to 29)

27. What is the difference between ROW_NUMBER, RANK and DENSE_RANK?

All three give a number to each row based on an ordering. ROW_NUMBER gives a unique number to every row, even when values are equal. RANK gives equal values the same rank and then skips numbers. DENSE_RANK gives equal values the same rank and does not skip numbers.

SELECT emp_name, salary,
  ROW_NUMBER() OVER (ORDER BY salary DESC) AS row_num,
  RANK()       OVER (ORDER BY salary DESC) AS rank_no,
  DENSE_RANK() OVER (ORDER BY salary DESC) AS dense_rank_no
FROM employees;

For our data the salaries in order are 60000, 55000, 45000, 45000, 40000, 30000. ROW_NUMBER gives 1 to 6. RANK gives 1, 2, 3, 3, 5, 6. DENSE_RANK gives 1, 2, 3, 3, 4, 5. If you can explain this one example clearly, you stand out from most freshers.

You can also use PARTITION BY to restart the ranking inside each group, for example the top earner in each department:

SELECT emp_name, dept_id, salary
FROM (
  SELECT emp_name, dept_id, salary,
    DENSE_RANK() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rnk
  FROM employees
  WHERE dept_id IS NOT NULL
) t
WHERE rnk = 1;

28. How does CASE work?

CASE is the if-else of SQL. It returns a value based on conditions.

SELECT emp_name, salary,
  CASE
    WHEN salary >= 55000 THEN 'High'
    WHEN salary >= 40000 THEN 'Medium'
    ELSE 'Low'
  END AS salary_band
FROM employees;

Asha and Vikram are High, Rahul, Neha and Pooja are Medium, and Karan is Low.

29. What is the order of execution of a SQL query?

You write a query starting with SELECT, but the database does not run it in that order. The logical order is FROM and JOIN, then WHERE, then GROUP BY, then HAVING, then SELECT, then ORDER BY, and finally LIMIT. This explains common errors. For example, you cannot use a column alias from SELECT inside WHERE in most systems, because WHERE runs before SELECT. It also explains why WHERE cannot use aggregate functions but HAVING can.

7-Day SQL Practice Plan

Day Topic What to do
Day 1 Setup and basics Create the practice tables. Practise SELECT, WHERE, DISTINCT, ORDER BY, LIKE and NULL questions (1 to 10).
Day 2 Joins Write INNER, LEFT and self joins from memory without looking (11 to 13).
Day 3 Grouping Practise GROUP BY, HAVING, UNION and subqueries (14 to 17).
Day 4 Classic queries Solve second highest salary, duplicates and department maximum (18 to 22) using two methods each.
Day 5 Window functions Practise ROW_NUMBER, RANK, DENSE_RANK and PARTITION BY (27) and CASE (28).
Day 6 Theory Revise keys, indexes, views, normalization and ACID (23 to 26). Say each answer aloud in under one minute.
Day 7 Mock Change the table data, invent 10 new questions yourself, and solve them with a timer.

After this week, add SQL practice to your daily routine for 20 minutes. If you are preparing for a specific test, combine it with aptitude and coding practice. The Infosys System Engineer guide and the TCS NQT preparation guide will help you plan the other sections, and our Infosys System Engineer Mock Test 1 lets you practise under time pressure.

Mistakes Freshers Make in SQL Interviews

  1. Memorising queries without running them. The interviewer changes one condition and the memorised answer breaks.
  2. Writing = NULL instead of IS NULL.
  3. Using INNER JOIN when the question needs unmatched rows (employees without a department, or departments without employees).
  4. Forgetting DISTINCT when finding the second or Nth highest salary, so tied values give a wrong answer.
  5. Adding a column to SELECT that is not in GROUP BY.
  6. Using WHERE where HAVING is needed, or the other way round.
  7. Not saying the assumptions aloud. Always tell the interviewer which database you are using, how you handle ties, and how you handle NULL.
  8. Staying silent when stuck. Explain your approach step by step. Interviewers often give marks for clear thinking even when the final query has a small error.

Frequently Asked Questions

Which SQL database should I practise on?

MySQL or PostgreSQL is enough for most fresher interviews, and both are free. The core syntax is the same everywhere. Learn the small differences, such as LIMIT, TOP and FETCH FIRST, so you are not surprised.

How many SQL questions should I solve before an interview?

Aim for about 50 to 60 well-understood queries covering joins, grouping, subqueries and window functions. Understanding 50 queries properly is better than copying 200.

Do I need window functions as a fresher?

For many fresher roles basic SQL is enough, but ROW_NUMBER, RANK and DENSE_RANK are asked often enough that learning them gives you a real advantage, especially for data and analyst roles.

Is SQL asked in written tests or only in interviews?

It depends on the company and the role. Some drives include SQL or database questions in the online assessment, and others ask them only in the technical interview. Check the official notification and recent candidate experiences for your drive.

Are the queries in this article safe to run?

Yes, on the practice tables created in this guide. Never run DELETE, UPDATE or TRUNCATE on a real database without a backup, and always test with SELECT first.

Final Words

SQL rewards practice more than any other interview topic. Set up the practice tables today, run every query in this guide, and then change the data to see how the answers change. When you can explain why a LEFT JOIN returns Karan, why DENSE_RANK handles tied salaries, and why HAVING runs after GROUP BY, you are ready for most fresher SQL questions. Keep your answers short, clear and honest, and keep applying to new drives while you practise.

Disclaimer: This article is for learning and interview preparation. Question patterns differ by company and drive. Always confirm details on the official career website.