Top 50 SQL Interview Questions and Answers
Practical & Job‑Oriented SQL Preparation
Basic SQL Questions
1. What is SQL?
SQL (Structured Query Language) is used to store, retrieve, update, and manage data in relational databases.
2. What are the types of SQL commands?
DDL, DML, DCL, TCL, and DQL.
3. What is a primary key?
A primary key uniquely identifies each record in a table and cannot be NULL.
4. What is a foreign key?
A foreign key links one table to another by referencing the primary key.
5. What is the difference between DELETE and TRUNCATE?
DELETE removes specific rows and can be rolled back; TRUNCATE removes all rows and cannot be rolled back.
6. What is NULL?
NULL represents missing or unknown data.
7. What is a UNIQUE constraint?
Ensures all values in a column are different.
8. What is a table?
A structured collection of rows and columns storing data.
9. What is a database?
An organized collection of structured data.
10. What is the use of SELECT?
Used to retrieve data from a database.
Intermediate SQL Questions
11. What is JOIN?
JOIN combines rows from multiple tables based on a related column.
12. Types of JOIN?
INNER, LEFT, RIGHT, FULL.
13. What is GROUP BY?
Groups rows that have the same values into summary rows.
14. Difference between WHERE and HAVING?
WHERE filters rows; HAVING filters groups.
15. What is ORDER BY?
Sorts the result set in ascending or descending order.
16. What is a subquery?
A query inside another query.
17. What is COUNT()?
Returns the number of rows.
18. What is DISTINCT?
Removes duplicate records.
19. What is BETWEEN?
Filters values within a specified range.
20. What is LIKE?
Used for pattern matching.
Advanced SQL Questions
21. What is an index?
Improves query performance by speeding up data retrieval.
22. What is a view?
A virtual table created using a SELECT query.
23. What is normalization?
Process of organizing data to reduce redundancy.
24. What is denormalization?
Adding redundancy to improve performance.
25. What is a stored procedure?
A reusable SQL code block stored in the database.
26. What is a trigger?
Automatically executes SQL code in response to events.
27. What is a CTE?
Common Table Expression used for temporary result sets.
28. What are window functions?
Functions that perform calculations across a set of rows.
29. What is RANK()?
Assigns rank with gaps for duplicates.
30. What is ROW_NUMBER()?
Assigns a unique sequential number.
Practical & Interview SQL Questions
31. Find second highest salary.
SELECT MAX(salary) FROM emp WHERE salary < (SELECT MAX(salary) FROM emp);
32. Difference between UNION and UNION ALL?
UNION removes duplicates; UNION ALL does not.
33. What is ACID?
Atomicity, Consistency, Isolation, Durability.
34. What is a transaction?
A sequence of SQL operations treated as a unit.
35. What is COMMIT?
Saves transaction changes permanently.
36. What is ROLLBACK?
Reverts changes made in a transaction.
37. What is a cursor?
Processes rows one at a time.
38. Difference between CHAR and VARCHAR?
CHAR is fixed length; VARCHAR is variable length.
39. What is SQL injection?
A security attack using malicious SQL queries.
40. How to prevent SQL injection?
Use prepared statements and parameterized queries.
41. What is EXPLAIN?
Shows query execution plan.
42. What is COALESCE?
Returns first non‑NULL value.
43. What is IFNULL?
Replaces NULL with a specified value.
44. What is LIMIT?
Restricts number of rows returned.
45. Difference between EXISTS and IN?
EXISTS checks row existence; IN compares values.
46. What is schema?
Logical container for database objects.
47. What is surrogate key?
System‑generated unique identifier.
48. What is composite key?
Primary key made of multiple columns.
49. What is data integrity?
Accuracy and consistency of data.
50. Why SQL is important for IT jobs?
SQL is the foundation for data analysis, reporting, backend systems, and enterprise applications.