SQL Interview Questions

Basic SQL Interview Questions

1. What is SQL?
SQL (Structured Query Language) is used to store, retrieve, and manage data in relational databases.


2. What is a database?
A database is an organized collection of data that can be easily accessed, managed, and updated.


3. What are tables and rows?

  • Table: A structure that stores data in rows and columns
  • Row: A single record in a table

4. What is the difference between SQL and MySQL?
SQL is a language, while MySQL is a database management system that uses SQL.


5. What is a primary key?
A column (or set of columns) that uniquely identifies each row in a table.


6. What is a foreign key?
A column that creates a relationship between two tables.


🟡 Intermediate SQL Questions

7. What is a JOIN?
A JOIN combines rows from two or more tables based on a related column.


8. Types of JOINs

-- INNER JOIN
SELECT * FROM A
INNER JOIN B ON A.id = B.id;-- LEFT JOIN
SELECT * FROM A
LEFT JOIN B ON A.id = B.id;-- RIGHT JOIN
SELECT * FROM A
RIGHT JOIN B ON A.id = B.id;

9. What is GROUP BY?
It groups rows with the same values into summary rows.

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

10. What is HAVING?
Used to filter grouped data (after GROUP BY).

SELECT department, COUNT(*) 
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

11. Difference between WHERE and HAVING

  • WHERE filters rows before grouping
  • HAVING filters after grouping

12. What is a subquery?
A query inside another query.

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

🔵 Advanced SQL Interview Questions

13. What is a window function?
Performs calculations across a set of rows related to the current row.

SELECT name, salary,
RANK() OVER (ORDER BY salary DESC) AS rank
FROM employees;

14. What is a CTE (Common Table Expression)?

WITH HighSalary AS (
SELECT * FROM employees WHERE salary > 50000
)
SELECT * FROM HighSalary;

15. What is indexing?
Indexes improve query performance by allowing faster data retrieval.


16. What is normalization?
The process of organizing data to reduce redundancy.


17. What is denormalization?
Adding redundancy to improve read performance.


🟣 Scenario-Based SQL Questions

18. Find the second highest salary

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

19. Find duplicate records

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

20. Delete duplicate records

DELETE FROM employees
WHERE id NOT IN (
SELECT MIN(id)
FROM employees
GROUP BY name
);

21. Get top 3 highest salaries

SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 3;

22. Find employees with no department

SELECT e.name
FROM employees e
LEFT JOIN departments d ON e.dept_id = d.id
WHERE d.id IS NULL;

🧠 Conceptual Questions

23. What is ACID property?

  • Atomicity
  • Consistency
  • Isolation
  • Durability

24. What is a transaction?
A sequence of operations performed as a single unit.


25. What is a view?
A virtual table based on a SQL query.


26. What is a stored procedure?
A precompiled SQL code stored in the database.


27. What is a trigger?
Automatically executes when a specific event occurs.