Top 25 SQL Interview Questions and Answers for 2025 (With Real Examples)

Top 25 SQL Interview Questions and Answers for 2025 (With Real Examples)

Top 25 SQL Interview Questions and Answers for 2025 (With Real Examples)

Updated: July 2025 | Estimated Reading Time: 8 mins

Preparing for a SQL interview in 2025? Whether you’re aiming for a role as a data analyst, backend developer, or database administrator, this article covers the most commonly asked SQL interview questions — with clear explanations and real examples.


1. What is SQL?

Answer: SQL (Structured Query Language) is used to store, manipulate, and retrieve data in relational databases. It’s the standard language for interacting with RDBMS like MySQL, PostgreSQL, SQL Server, and Oracle.

2. What are the different types of SQL commands?

  • DML: SELECT, INSERT, UPDATE, DELETE
  • DDL: CREATE, ALTER, DROP
  • DCL: GRANT, REVOKE
  • TCL: COMMIT, ROLLBACK, SAVEPOINT

3. What is a Primary Key?

A Primary Key uniquely identifies each record in a table. It cannot contain NULLs or duplicate values.

4. What is a Foreign Key?

A Foreign Key is a column that creates a relationship between two tables. It references the Primary Key in another table.

5. What is the difference between WHERE and HAVING?

WHEREHAVING
Used to filter rows before groupingUsed to filter groups after grouping
Applies to individual rowsApplies to grouped rows

6. What are the types of JOINs in SQL?

SQL JOINs are used to combine rows from two or more tables. Types include:

  • INNER JOIN
  • LEFT JOIN (or LEFT OUTER JOIN)
  • RIGHT JOIN (or RIGHT OUTER JOIN)
  • FULL JOIN (or FULL OUTER JOIN)
  • CROSS JOIN

7. Give an example of an INNER JOIN.

SELECT employees.name, departments.dept_name
FROM employees
INNER JOIN departments
ON employees.dept_id = departments.id;

8. What is a subquery?

A subquery is a query nested inside another query. It is often used in the WHERE clause to filter results based on dynamic values.

SELECT name
FROM employees
WHERE salary > (SELECT AVG(salary) FROM employees);

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

  • DELETE: Removes rows one at a time and can be rolled back.
  • TRUNCATE: Removes all rows instantly; cannot be rolled back in many DBMS.
  • DROP: Deletes the entire table structure from the database.

10. What is normalization?

Normalization is the process of organizing data to reduce redundancy and improve data integrity. It involves dividing large tables into smaller ones and defining relationships.

11. What are the normal forms?

  • 1NF: No repeating groups or arrays
  • 2NF: Follows 1NF + no partial dependency
  • 3NF: Follows 2NF + no transitive dependency
  • BCNF: Stronger version of 3NF

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

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

13. What is an Index?

An Index improves the speed of data retrieval from a table. It works like a pointer to data in a table similar to an index in a book.

14. What is a Composite Key?

A Composite Key is a Primary Key made up of two or more columns.

15. What is the difference between CHAR and VARCHAR?

  • CHAR(n): Fixed length string of n characters
  • VARCHAR(n): Variable length string up to n characters

16. How do you fetch the current date in SQL?

SELECT CURRENT_DATE;

17. What is a view?

A view is a virtual table based on the result of a SELECT query. It does not store data itself but fetches it from underlying tables.

18. What is the BETWEEN operator?

BETWEEN filters the result set within a specific range.

SELECT * FROM orders
WHERE order_date BETWEEN '2024-01-01' AND '2024-12-31';

19. How to get the second highest salary?

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

20. What are aggregate functions?

  • COUNT()
  • SUM()
  • AVG()
  • MIN()
  • MAX()

21. What is GROUP BY?

GROUP BY is used with aggregate functions to group the result-set by one or more columns.

SELECT dept_id, COUNT(*) FROM employees GROUP BY dept_id;

22. What is the use of CASE statement?

CASE is used for conditional logic inside queries.

SELECT name, salary,
  CASE
    WHEN salary > 10000 THEN 'High'
    WHEN salary > 5000 THEN 'Medium'
    ELSE 'Low'
  END AS salary_level
FROM employees;

23. What is a constraint in SQL?

Constraints are rules enforced on data columns. Common ones include:

  • NOT NULL
  • UNIQUE
  • CHECK
  • DEFAULT
  • PRIMARY KEY
  • FOREIGN KEY

24. What is COALESCE() in SQL?

COALESCE returns the first non-null value from the given arguments.

SELECT COALESCE(NULL, NULL, 'fallback', 'another');

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

  • UNION: Combines results and removes duplicates
  • UNION ALL: Combines results with duplicates allowed

These questions cover the basics and some advanced topics you’ll likely face in SQL job interviews. Practice writing queries and understand the logic behind each concept to perform well in interviews.