LogIn
I don't have account.

Top SQL Interview Questions for 2025

Code Crafter
20 Views

SQL (Structured Query Language) is a standardized programming language used to manage and manipulate relational databases. It allows users to create, read, update and delete data using commands like SELECT, INSERT, UPDATE, and DELETE. SQL is a core skill for developers, data analysts and backend engineers. Whether you’re preparing for a tech interview at a startup or a FAANG company, mastering SQL interview questions is crucial.

In this guide, we have compiled the top 10 most commonly asked SQL interview questions, complete with explanations and examples.

1. What Is SQL?

SQL stands for Structured Query Language. It’s used to interact with relational databases - performing tasks like querying, inserting, updating and deleting data

2. What Are SQL Dialects?

Variants like MySQL, PostgreSQL, SQL Server, Oracle. They share core syntax but offer different extensions .

3. What is the difference between WHERE and HAVING?

  • WHERE is used to filter rows before aggregation
  • HAVING is used to filter groups after aggregation sql Copy Edit
SELECT department, COUNT(*) 
FROM employees 
WHERE status = 'active'
GROUP BY department
HAVING COUNT(*) > 5;

Tip : Don't use HAVING when WHERE is sufficient. HAVING is more performance-intensive.

4. Explain the types of JOINs in SQL

  • INNER JOIN: Returns records that match in both tables
  • LEFT JOIN: Returns all records from left and matching from right
  • RIGHT JOIN: Returns all from right and matching from left
  • FULL OUTER JOIN: Returns all records from both tables, matching rows where possible. If there’s no match, the result will still include the row, with NULL values for the missing side.
  • CROSS JOIN : Returns the Cartesian product of two tables, i.e. all combinations of rows.
  • SELF JOIN : A table is joined with itself.

5. What is a Primary Key and how is it different from a Unique Key?

A Primary Key is a column (or combination of columns) that uniquely identifies each row in a table and does not allow NULL values. A Unique Key also enforces uniqueness across rows but allows one NULL value and is typically used for enforcing alternate or secondary unique constraints.

CREATE TABLE Employees (
  emp_id INT PRIMARY KEY,          -- uniquely identifies each employee
  email VARCHAR(100) UNIQUE,       -- email must be unique, NULL allowed
  phone VARCHAR(15) UNIQUE         -- phone must be unique, NULL allowed
);

Key differences between Primary Key and Unique Key

Feature Primary Key Unique Key
Uniqueness Must be unique Must be unique
NULL Values Allowed Not allowed Allowed (only one NULL per unique key in most DBMS)
Default Index Automatically creates a clustered index (in many DBMSs like SQL Server) Automatically creates a non-clustered index
Number per Table Only one Primary Key per table Can have multiple Unique Keys
Purpose Main identifier for a row Enforces uniqueness on other columns
Constraint Type Primary Key constraint Unique constraint
Composite Key Support Yes (can be made of multiple columns) Yes (can be made of multiple columns)
Enforces Entity Integrity Yes No
Syntax PRIMARY KEY UNIQUE

6. What is Normalization? Explain 1NF, 2NF and 3NF

Normalization is the process of organizing data in a relational database to minimize redundancy and improve data integrity. It involves dividing large tables into smaller and related tables and defining relationships between them. This ensures efficient data storage and reduces anomalies during insert, update or delete operations.

1NF (First Normal Form)

  • Each column must contain atomic (indivisible) values and each record must be unique.
  • No repeating groups or arrays.
  • Goal: Eliminate multivalued attributes.
Original Table (Not in 1NF)
StudentID Name Courses
1 John Math, Science
1NF Version (Atomic Values)
StudentID Name Course
1 John Math
1 John Science

2NF (Second Normal Form)

  • Rule: Must be in 1NF and no partial dependency (non-key attributes must depend on the whole primary key).
  • Applies to tables with composite primary keys.
Original Table (Violating 2NF)
StudentID CourseID StudentName CourseName
1 101 John Math

StudentName depends only on StudentID → partial dependency

2NF Version (Eliminating Partial Dependency)


Students Table
StudentID StudentName
1 John
Courses Table
CourseID CourseName
101 Math
Enrollment Table
StudentID CourseID
1 101

3NF (Third Normal Form)

  • Rule: Must be in 2NF and have no transitive dependency (non-key attributes should not depend on other non-key attributes).
  • All attributes must depend only on the primary key.
Original Table (Violating 3NF)
StudentID Name Department DeptHead
1 John Science Dr. Smith

DeptHead depends on Department, not directly on StudentID.

3NF Version (Removing Transitive Dependency)
Students Table
StudentID Name Department
1 John Science
Departments Table
Department DeptHead
Science Dr. Smith

7. What are Indexes in SQL?

An index is a data structure used by a database to quickly locate and access data in a table, much like an index in a book. It improves the speed of read (SELECT) operations by avoiding full table scans.

8. How Do Indexes Work?

When you create an index on a column (or multiple columns), the database builds a separate lookup structure (usually a B-tree) that keeps the data in sorted order.

So, instead of going through the entire table row by row, the database uses this structure to jump directly to the relevant data, just like finding a word using a dictionary.

9. What all different types of indexes in SQL

Index Type Key Features Best Use Case
Primary Index Unique, no nulls, 1 per table Main row identifier
Unique Index Ensures no two rows have the same value in the indexed column, allows nulls Email, phone, usernames
Composite Index Multi-column indexing Combined filters like (first_name, last_name)
Clustered Index Sorts and stores data rows physically in order (only one per table) Range or sorted queries
Non-Clustered Index Stores a pointer to the actual row, without affecting physical order Fast lookup without affecting row order
Full-Text Index Optimized for large text search Blog posts, product descriptions
Spatial Index Geolocation-based queries Maps, location-based services
Bitmap Index Bitmaps for low-cardinality columns Analytical/data warehouse queries
JSON/XML Index Indexes inside JSON/XML or computed values APIs, modern web apps

10. What are the Benefits and Downsides of Indexes

✅ Benefits of Indexes
Benefit Description
1. Faster Query Performance Indexes allow the database to quickly locate rows, reducing the need for full table scans.
2. Efficient Searching Ideal for filtering with WHERE, joining tables or using LIKE, IN and BETWEEN.
3. Improved JOIN Performance Indexes on foreign keys or join columns speed up multi-table joins significantly.
4. Faster Sorting and Grouping Queries with ORDER BY, GROUP BY and aggregates benefit from indexed columns.
5. Supports Uniqueness Unique indexes help enforce business rules by preventing duplicate values.
6. Better Performance on Large Tables Especially helpful when dealing with millions of records.
⚠️ Downsides of Indexes
Downside Description
1. Slower Write Operations INSERT, UPDATE and DELETE operations become slower because indexes also need to be updated.
2. Extra Storage Required Each index consumes additional disk space, especially in large tables or with many indexes.
3. Maintenance Overhead Indexes must be maintained by the DB engine, which adds load during data changes.
4. Can Hurt Performance if Misused Too many or improperly used indexes can degrade overall performance.
5. Not Useful for Every Query Indexes don’t help if queries don’t filter, join or sort by the indexed column(s).
6. Risk of Over-indexing Too many indexes can hurt performance more than help it, especially on write-heavy tables.