SQL Interview Questions for Freshers: 40 Questions with Answers

SQL is tested in almost every technical interview for freshers in India, whether the role is software tester, data analyst, ETL tester or developer. These 40 SQL interview questions for freshers go from basics to the "write the query" questions that decide most rounds.

SQL Basics (Q1–Q12)

1. What is SQL?

Structured Query Language, used to create, read, update and delete data in relational databases.

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

DDL defines structure (CREATE, ALTER, DROP), DML changes data (INSERT, UPDATE, DELETE), DCL controls access (GRANT, REVOKE), TCL controls transactions (COMMIT, ROLLBACK, SAVEPOINT). SELECT is sometimes called DQL.

3. What is a primary key?

A column or set of columns that uniquely identifies each row and cannot be NULL.

4. Primary key vs unique key?

One primary key per table, no NULLs; several unique keys allowed, and they usually allow NULLs.

5. What is a foreign key?

A column referencing another table's primary key, enforcing referential integrity.

6. What is a constraint?

A rule on data: NOT NULL, UNIQUE, PRIMARY KEY, FOREIGN KEY, CHECK, DEFAULT.

7. DELETE vs TRUNCATE vs DROP?

DELETE removes chosen rows (can use WHERE); TRUNCATE removes all rows quickly; DROP removes the table.

8. WHERE vs HAVING?

WHERE filters rows before grouping; HAVING filters groups after GROUP BY.

9. What is NULL?

Unknown or missing value. Test it with IS NULL, never = NULL.

10. UNION vs UNION ALL?

UNION removes duplicates; UNION ALL keeps all rows and is faster.

11. What is an index?

A structure that speeds up lookups on columns, at the cost of extra storage and slower writes.

12. What is a view?

A saved query used like a virtual table.

Query Questions (Q13–Q24)

13. Types of joins?

INNER, LEFT, RIGHT, FULL OUTER, CROSS and SELF. Full guide: SQL joins with examples.

14. What does GROUP BY do?

Groups rows with the same values so aggregate functions (COUNT, SUM, AVG) work per group.

15. COUNT(*) vs COUNT(column)?

COUNT(*) counts all rows; COUNT(column) skips NULLs.

16. What is a subquery?

A query inside another query, used in WHERE, FROM or SELECT.

17. Correlated subquery?

A subquery that refers to the outer query and runs once per outer row.

18. IN vs EXISTS?

IN compares to a list of values; EXISTS checks whether the subquery returns any row and is often faster for large subqueries.

19. What is ORDER BY default order?

Ascending.

20. What does DISTINCT do?

Removes duplicate rows from the result.

21. What is LIKE used for?

Pattern matching: % for any characters, _ for one character.

22. CHAR vs VARCHAR?

CHAR is fixed length and padded; VARCHAR is variable length.

23. What is a CASE expression?

If-then-else logic inside a query.

24. What is COALESCE?

Returns the first non-NULL value in its list, often used to replace NULLs.

Slightly Advanced (Q25–Q32)

25. What are window functions?

Functions such as ROW_NUMBER, RANK, DENSE_RANK, LAG and SUM OVER that calculate across related rows without collapsing them.

26. RANK vs DENSE_RANK vs ROW_NUMBER?

ROW_NUMBER gives unique numbers; RANK leaves gaps after ties; DENSE_RANK does not leave gaps.

27. What is a CTE?

A named temporary result defined with WITH, used to make queries readable or recursive.

28. What is normalisation?

Splitting data into related tables to reduce redundancy (1NF, 2NF, 3NF). See DBMS interview questions.

29. What is a transaction?

A unit of work that commits or rolls back entirely, following ACID properties.

30. What is a stored procedure?

Saved SQL code with parameters that runs on the database.

31. What is a trigger?

Code that runs automatically on INSERT, UPDATE or DELETE.

32. What is an execution plan?

The database's plan for running a query; used to find slow steps such as full table scans.

Write the Query (Q33–Q40)

SQL
-- 33. Second highest salary
SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);

-- 34. Nth highest salary (N = 3)
SELECT DISTINCT salary FROM (
  SELECT salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS rnk FROM employees
) t WHERE rnk = 3;

-- 35. Duplicate emails
SELECT email, COUNT(*) FROM employees GROUP BY email HAVING COUNT(*) > 1;

-- 36. Delete duplicates, keep the lowest id (PostgreSQL/SQL Server style)
DELETE FROM employees
WHERE id NOT IN (SELECT MIN(id) FROM employees GROUP BY email);

-- 37. Employees who earn more than their manager
SELECT e.name FROM employees e JOIN employees m ON m.id = e.manager_id
WHERE e.salary > m.salary;

-- 38. Department with the highest total salary
SELECT dept_id, SUM(salary) AS total FROM employees
GROUP BY dept_id ORDER BY total DESC LIMIT 1;

-- 39. Employees who joined in the last 30 days
SELECT * FROM employees WHERE join_date >= CURRENT_DATE - INTERVAL '30' DAY;

-- 40. Highest-paid employee in each department
SELECT * FROM (
  SELECT e.*, ROW_NUMBER() OVER (PARTITION BY dept_id ORDER BY salary DESC) AS rn FROM employees e
) t WHERE rn = 1;

Syntax for LIMIT, date arithmetic and deleting duplicates varies slightly between MySQL, PostgreSQL, SQL Server and Oracle; mention which database you are using in the interview.

What Changes by Role and Experience

Role / experienceExtra focus
Software tester (fresher)Verifying data after UI actions, duplicates, NULL checks: SQL for testers
Data analystGROUP BY, window functions, date functions, CASE for business metrics
ETL testerEXCEPT/MINUS, reconciliation, orphans: SQL for ETL testing interviews
2–3 yearsWindow functions, CTEs, query performance and real project examples

Practise many more SQL and ETL questions with the ETL Testing Interview Questions & Answers – Practice Tests.

Prepare for your ETL testing interview: top 50 questions · in-depth answers · for experienced (3–5 years) · for 10 years experience · SQL query questions · scenario-based questions · data warehouse questions · questions for freshers · free mock test · database testing questions · Python ETL testing questions · Informatica questions · DBMS questions · data validation questions · service-company questions

Data testing skills: SQL for testers · data migration testing · Power BI testing · big data testing · data quality checks · ETL tools list · ADF & Databricks testing · database testing interview questions · Python for testers · data reconciliation · SQL joins · test data management · ETL defects examples · Snowflake ETL testing · AWS Glue testing · data lake testing

Frequently Asked Questions

What SQL questions are asked to freshers?

Basics such as keys, constraints, DDL/DML, WHERE vs HAVING, joins, GROUP BY, subqueries, and query tasks like second highest salary and finding duplicates.

How should freshers prepare for SQL interviews?

Practise writing queries by hand on sample tables daily for a few weeks, focusing on joins, GROUP BY, subqueries and window functions.

Is SQL enough to get a job as a fresher?

SQL alone is rarely enough, but combined with testing, ETL, data analysis or development basics it is one of the most valuable skills for a first job.

Which database should I practise SQL on?

MySQL or PostgreSQL are free and widely used. Core SQL is similar across databases.

What is the most common SQL interview question?

Finding the second (or Nth) highest salary is probably the most common, along with the difference between WHERE and HAVING and between join types.

Asim Noaman Lodhi
Written by

Asim Noaman Lodhi

Certified Google Partner · QA Consultant · 12+ Years IT

QA consultant specializing in ETL testing and data quality. Trained 913+ students to transition into data testing roles through hands-on, real-world instruction.

4.5 Rating 913+ Students Google Partner 79 Lectures

Practise SQL and ETL Interview Questions

500+ ETL testing interview questions with detailed answers, from fresher to experienced level.

Practice 500+ Questions