0 Comments

Introduction

SQL (Structured Query Language) is a must-know skill for data analysts, database administrators, and software developers. Whether you’re a beginner or an experienced professional, SQL interview questions can range from basic queries to complex database optimizations.

To help you prepare, we’ve compiled the top 50 Most asked SQL questions in interviews with well-explained answers. This guide covers:

  • Basic SQL Questions (SELECT, INSERT, UPDATE, DELETE)
  • Intermediate SQL Questions (JOINs, Subqueries, GROUP BY)
  • Advanced SQL Questions (Indexing, Stored Procedures, Optimization)

Let’s dive in!


Basic SQL Interview Questions

1. What is SQL?

SQL (Structured Query Language) is a standard language for managing and manipulating relational databases. It is used for querying, updating, and managing data in systems like MySQL, PostgreSQL, Oracle, and SQL Server.

2. What are the different types of SQL commands?

  • DDL (Data Definition Language): CREATE, ALTER, DROP, TRUNCATE
  • DML (Data Manipulation Language): SELECT, INSERT, UPDATE, DELETE
  • DCL (Data Control Language): GRANT, REVOKE
  • TCL (Transaction Control Language): COMMIT, ROLLBACK, SAVEPOINT

3. What is a Primary Key?

Primary Key is a unique identifier for a row in a table. It cannot be NULL and must be unique.

4. What is a Foreign Key?

Foreign Key is a field in one table that refers to the Primary Key of another table, establishing a relationship between them.

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

CommandDescriptionRollback Possible?
DELETERemoves rows one by oneYes (with transaction)
TRUNCATERemoves all rows at onceNo
DROPDeletes the entire tableNo

Intermediate SQL Interview Questions

6. What are SQL JOINs? Explain types.

JOINs combine rows from two or more tables based on related columns.

  • INNER JOIN: Returns matching rows from both tables.
  • LEFT JOIN: Returns all rows from the left table + matched rows from the right.
  • RIGHT JOIN: Returns all rows from the right table + matched rows from the left.
  • FULL JOIN: Returns all rows when there’s a match in either table.

7. What is the difference between WHERE and HAVING?

  • WHERE filters rows before grouping.
  • HAVING filters groups after the GROUP BY clause.

8. What is a Subquery?

A subquery is a query nested inside another query (SELECT, INSERT, UPDATE, DELETE).

Example:

sql

Copy

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

9. What is a Self Join?

Self Join is a join where a table is joined with itself.

Example:

sql

Copy

SELECT A.name, B.name AS manager  
FROM employees A, employees B  
WHERE A.manager_id = B.id;  

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

  • UNION: Combines results and removes duplicates.
  • UNION ALL: Combines results without removing duplicates (faster).

Advanced SQL Interview Questions

11. What are Indexes? How do they improve performance?

Indexes are database structures that speed up data retrieval. They work like a book index, allowing faster searches.

12. What is Normalization? Explain 1NF, 2NF, 3NF.

  • 1NF (First Normal Form): No repeating groups, each column has atomic values.
  • 2NF (Second Normal Form): No partial dependency (all non-key columns depend on the full primary key).
  • 3NF (Third Normal Form): No transitive dependency (non-key columns don’t depend on other non-key columns).

13. What is a Stored Procedure?

Stored Procedure is a precompiled SQL query stored in the database for reuse.

Example:

sql

Copy

CREATE PROCEDURE GetEmployees AS  
SELECT * FROM employees;  

14. What is a Trigger?

Trigger is an automatic action that executes when a specific event (INSERT, UPDATE, DELETE) occurs.

15. How do you optimize a slow SQL query?

  • Use INDEXES on frequently queried columns.
  • Avoid **SELECT *** (fetch only needed columns).
  • Optimize JOINs and WHERE clauses.
  • Use EXPLAIN to analyze query execution.

Conclusion

Mastering these 50 most asked SQL interview questions will give you a strong foundation for cracking technical interviews. Practice writing queries, understand database design principles, and optimize performance to stand out.

Need more SQL practice? Try platforms like LeetCode, HackerRank, and StrataScratch!

🔗 Share this guide with your network to help others prepare!

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts