Top 100 SQL Interview Questions and Answers for Freshers and Experienced (2025 Edition)

SQL Interview Questions

Looking to crack your next SQL interview? Whether you’re a beginner, intermediate, or advanced SQL developer, this comprehensive list of the top 100+ SQL interview questions and answers will help you stand out from the crowd. It covers every key concept in SQLβ€”queries, joins, indexing, transactions, and even real-time performance optimization. Let’s dive in.


πŸ”° What is SQL and Why Is It Important?

SQL (Structured Query Language) is a standard programming language designed specifically for managing and manipulating relational databases. It helps users interact with data by allowing them to create, retrieve, update, and delete records (CRUD operations).

Why is it important?

  • Almost every application relies on databases to store data.
  • SQL is platform-independent and supported by all major relational database systems like MySQL, PostgreSQL, SQL Server, and Oracle.
SQL Interview Questions
SQL Interview Questions

🎯 Top 100 SQL Interview Q&A and SQL Cheat Sheet – Free PDF Download πŸ“˜

✨ Getting it is super simple:

πŸš€ Step 1: Tap the link below πŸ‘‡
πŸ“₯ Step 2: You’ll be redirected to the exact Telegram channel
πŸ”“ Step 3: Access & download the PDF instantly – no hassle!

πŸ“š Crack your next interview with confidence!
From basic to advanced SQL questions – all in one place.

πŸ‘‰ Click below and level up your prep now!
πŸ”— [Link Here]

SQL Interview Questions

πŸ“˜ Basic Level (Beginner)–>SQL Interview Questions

SQL Interview Questions
SQL Interview Questions

These SQL interview questions are perfect for freshers or those new to database concepts.

1. What is SQL?

SQL stands for Structured Query Language. It’s used for managing and manipulating data in relational databases. For example:

SELECT * FROM students;

This retrieves all records from the ‘students’ table.

2. What is a Database?

A database is a structured collection of data that can be easily accessed and managed. It can be as simple as a contact list or as complex as a banking system.

3. What are the types of SQL Commands?

  • DDL (Data Definition Language): Defines structure. Example: CREATE, ALTER, DROP
  • DML (Data Manipulation Language): Manages data. Example: INSERT, UPDATE, DELETE
  • DCL (Data Control Language): Manages permissions. Example: GRANT, REVOKE
  • TCL (Transaction Control Language): Manages transactions. Example: COMMIT, ROLLBACK
  • DQL (Data Query Language): Retrieves data. Example: SELECT

4. What is Primary Key?

A primary key uniquely identifies each row in a table. Example:

CREATE TABLE students (
  id INT PRIMARY KEY,
  name VARCHAR(50)
);

5. What is Foreign Key?

A foreign key links two tables. It enforces referential integrity. Example:

CREATE TABLE orders (
  order_id INT,
  student_id INT,
  FOREIGN KEY (student_id) REFERENCES students(id)
);

6. What is UNIQUE Key?

Ensures all values in a column are different. Example:

CREATE TABLE users (
  email VARCHAR(100) UNIQUE
);

7. Primary Key vs UNIQUE Key

Feature Primary Key UNIQUE Key
NULLs Not Allowed Allowed
Count One per table Multiple allowed

8. What is NOT NULL Constraint?

It ensures that a column must have a value. Example:

CREATE TABLE employees (
  name VARCHAR(50) NOT NULL
);

9. What is Default Constraint?

Provides a default value if no input is given. Example:

CREATE TABLE orders (
  status VARCHAR(20) DEFAULT 'Pending'
);

10. DELETE vs TRUNCATE vs DROP

Operation Description
DELETE Removes specific rows based on condition. WHERE clause is used.
TRUNCATE Removes all rows but keeps table structure. Faster.
DROP Deletes the table entirely.

11. WHERE vs HAVING

  • WHERE is used before GROUP BY to filter rows.
  • HAVING is used after GROUP BY to filter aggregated results.
    Example:
SELECT department, COUNT(*)
FROM employees
GROUP BY department
HAVING COUNT(*) > 5;

12–20. Joins, UNIONs, and Normalization

INNER JOIN

Returns matching rows from both tables.

SELECT * FROM employees e INNER JOIN departments d ON e.dept_id = d.id;

LEFT JOIN

Returns all records from the left table, and matched records from the right.

RIGHT JOIN

Returns all records from the right table, and matched from the left.

FULL JOIN

Returns all records when there’s a match in either left or right table.

UNION vs UNION ALL

  • UNION removes duplicates.
  • UNION ALL includes duplicates.

Normalization vs Denormalization

  • Normalization: Organizes data to reduce redundancy.
  • Denormalization: Combines tables to improve read performance.

πŸ“— Intermediate Level–SQL Interview Questions

SQL Interview Questions
SQL Interview Questions

For developers and analysts with 1–3 years experience.

26. What is a Subquery?

A query inside another query. Example:

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

28. What is a Correlated Subquery?

A subquery that references columns from the outer query.

29. What is GROUP BY in SQL?

Groups rows and allows aggregate functions.

SELECT department, AVG(salary) FROM employees GROUP BY department;

30. GROUP BY vs ORDER BY

  • GROUP BY groups rows.
  • ORDER BY sorts rows.

32. Find Second Highest Salary

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

36–38. Window Functions

Functions like ROW_NUMBER(), RANK(), and DENSE_RANK() allow row-level ranking.


πŸ“™ Advanced Level–SQL Interview Questions

Advanced SQL topics for experienced professionals.

41. What is Indexing in SQL?

Improves performance by speeding up data retrieval.

  • Clustered Index: Alters table structure.
  • Non-clustered Index: Separate from table data.

45. What is a View?

A virtual table based on the result of an SQL statement.

CREATE VIEW high_salary AS
SELECT * FROM employees WHERE salary > 50000;

47. What is a Stored Procedure?

A precompiled collection of SQL statements.

CREATE PROCEDURE GetAllEmployees AS
SELECT * FROM employees;

49. What is a Trigger?

Automatically executes when an event like INSERT, UPDATE occurs.

50. What is a Cursor?

Iterates row by row in a result set.

58. RANK() vs DENSE_RANK()

  • RANK(): Skips rankings on ties.
  • DENSE_RANK(): No skipped rankings.

🧠 Real-Time Scenarios–SQL Interview Questions

66. Calculate Age from DOB

SELECT name, DATEDIFF(year, dob, GETDATE()) AS Age FROM employees;

67. What is a Recursive Query?

A query that calls itself. Useful for hierarchical data (like employee-manager trees).

70. What is JSON in SQL?

Modern databases like PostgreSQL and MySQL support JSON for storing semi-structured data.

72. Handling NULL Values

Use IS NULL, COALESCE(), or IFNULL().

SELECT name, COALESCE(phone, 'N/A') FROM contacts;

75. Employees Who Earn More Than Their Manager

SELECT e.name
FROM employees e
JOIN employees m ON e.manager_id = m.id
WHERE e.salary > m.salary;

πŸ“Š Performance & Optimization–SQL Interview Questions

91. How to Optimize SQL Queries?

  • Use indexes
  • Avoid SELECT *
  • Write efficient JOINs
  • Use LIMIT

92. What is Query Execution Plan?

Shows how a query is executed. Use EXPLAIN in MySQL or SHOWPLAN in SQL Server.

95. What is Table Partitioning?

Splits large tables for performance.

100. OLTP vs OLAP

  • OLTP: Used for real-time operations (insert/update/delete).
  • OLAP: Used for analytics and reporting.

🌐 External Resources for SQL Mastery

SQL Interview Questions
SQL Interview Questions

🎯 Final Words

These 100+ SQL Interview Questions and Answers are designed not only to help you crack interviews but also to strengthen your foundational and practical knowledge of SQL. Make sure to practice them, share with your peers, and refer back to this guide whenever needed!

βœ… Want this as a downloadable PDF or printable format? Let us know in the comments!

 

FOR ALL 100 questions pdf–>Click here

Leave a Reply

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