SQL Topics
SQL Interview Questions Advanced
title: SQL Interview Questions Advanced
This section covers questions typically asked for:
Senior SQL Developer
Backend Engineer
Database Engineer
Data Engineer
DBA
Software EngineerAdvanced interviews focus on:
Performance
Optimization
Architecture
Concurrency
Transactions
Execution Plans
Scalabilityrather than simple SQL syntax.
Q1. What is the difference between Clustered and Non-Clustered Indexes?
Clustered Index
Determines physical order of data.
Only one clustered index per table.
Non-Clustered Index
Separate structure pointing to table rows.
Multiple non-clustered indexes allowed.
| Feature | Clustered | Non-Clustered |
|---|---|---|
| Physical Sorting | Yes | No |
| Count Per Table | One | Multiple |
| Storage | Data + Index | Index Only |
Q2. What is a Covering Index?
A covering index contains all columns required by a query.
Example:
SELECT EmployeeID,
EmployeeName
FROM Employees
WHERE DepartmentID = 1;Index:
CREATE INDEX IX_Covering
ON Employees
(
DepartmentID,
EmployeeID,
EmployeeName
);No table lookup needed.
Q3. What is the Leftmost Prefix Rule?
For index:
(DepartmentID, Salary)SQL can efficiently use:
WHERE DepartmentID = 1and
WHERE DepartmentID = 1
AND Salary > 50000Not always:
WHERE Salary > 50000alone.
Q4. What is Index Fragmentation?
Over time indexes become inefficient due to:
INSERTS
UPDATES
DELETES
Page SplitsResult:
Slower Queries
More I/OQ5. Difference Between Rebuild and Reorganize Index?
REBUILD
Recreates entire index.
REORGANIZE
Defragments existing index.
| Feature | REBUILD | REORGANIZE |
|---|---|---|
| Faster Recovery | Yes | No |
| Resource Usage | Higher | Lower |
| Complete Rebuild | Yes | No |
Q6. What is a Query Execution Plan?
A roadmap showing how SQL executes a query.
Displays:
Scans
Seeks
Joins
Sorts
CostsQ7. What is the difference between Index Scan and Index Seek?
Index Scan
Reads entire index.
Index Seek
Directly locates required rows.
Index Seek is usually faster.
Q8. What is a Table Scan?
Reading every row in a table.
Usually caused by:
Missing Indexes
Poor Query DesignQ9. What is Cardinality?
Cardinality refers to the uniqueness of values in a column.
High Cardinality:
Email
EmployeeIDLow Cardinality:
Gender
StatusQ10. Why Are High Cardinality Columns Better for Indexing?
Because indexes become more selective.
Example:
Emailis better than:
GenderQ11. What is Query Optimization?
Improving performance while reducing:
CPU
Memory
Disk I/OQ12. What is Cost-Based Optimization?
The optimizer evaluates multiple execution plans and chooses the lowest estimated cost.
Q13. What is a Hash Join?
Join strategy that builds a hash table and matches rows.
Best for:
Large Tables
No Useful IndexesQ14. What is a Nested Loop Join?
For each row in one table:
Find Matching Rowin another table.
Best for:
Small Datasets
Indexed LookupsQ15. What is a Merge Join?
Combines already sorted datasets.
Best for:
Indexed Data
Sorted DataQ16. What is Partitioning?
Dividing a large table into smaller partitions.
Benefits:
Performance
Scalability
MaintenanceQ17. What is Partition Pruning?
Reading only relevant partitions during query execution.
Example:
WHERE TransactionDate
BETWEEN ...Q18. Difference Between Partitioning and Indexing?
| Feature | Partitioning | Indexing |
|---|---|---|
| Splits Data | Yes | No |
| Improves Lookup | Indirectly | Directly |
| Large Tables | Excellent | Good |
Q19. What is a Materialized View?
A view whose results are physically stored.
Advantages:
Fast ReadsDisadvantages:
Consumes StorageQ20. What is a CTE?
Common Table Expression.
Temporary result set used within a query.
Q21. What is a Recursive CTE?
A CTE that references itself.
Used for:
Hierarchy
Organization Charts
Folder TreesQ22. What is a Window Function?
Performs calculations across related rows without collapsing results.
Example:
AVG(Salary)
OVER()Q23. Difference Between RANK() and ROW_NUMBER()?
ROW_NUMBER()
Always unique.
RANK()
Duplicate values receive same rank.
Example:
ROW_NUMBER:
1 2 3 4
RANK:
1 2 2 4Q24. What is DENSE_RANK()?
Like RANK but without gaps.
Example:
1
2
2
3Q25. What is a Deadlock?
Two transactions waiting for each other indefinitely.
Example:
A waits for B
B waits for AQ26. How Can Deadlocks Be Prevented?
Use:
Consistent Lock Order
Short Transactions
Proper IndexesQ27. What are Isolation Levels?
Control visibility between transactions.
Levels:
Read Uncommitted
Read Committed
Repeatable Read
SerializableQ28. What is Phantom Read?
Rows appearing or disappearing between reads within the same transaction.
Q29. What is Dirty Read?
Reading uncommitted data from another transaction.
Q30. What is Repeatable Read?
Ensures same row returns same values during a transaction.
Q31. What is Serializable Isolation?
Highest isolation level.
Prevents:
Dirty Reads
Non-Repeatable Reads
Phantom ReadsQ32. What is SQL Injection?
Malicious SQL inserted into user input.
Example:
' OR 1=1 --Q33. How to Prevent SQL Injection?
Q34. What is Dynamic SQL?
SQL generated at runtime.
Example:
EXEC(@SQL);Q35. Difference Between EXISTS and IN?
Large datasets often perform better with:
EXISTSbecause it stops after finding a match.
Q36. Difference Between OLTP and OLAP?
| Feature | OLTP | OLAP |
|---|---|---|
| Transactions | High | Low |
| Queries | Short | Complex |
| Data | Current | Historical |
Q37. What is a Star Schema?
Common Data Warehouse structure.
Components:
Fact Table
Dimension TablesQ38. What is a Snowflake Schema?
Normalized version of Star Schema.
Benefits:
Reduced RedundancyQ39. What is CAP Theorem?
Distributed systems can guarantee only two of:
Consistency
Availability
Partition Tolerancesimultaneously.
Q40. What is Database Sharding?
Splitting data across multiple servers.
Example:
Server 1 → Customers A-M
Server 2 → Customers N-ZBenefits:
Horizontal ScalingMost Asked Advanced SQL Questions
Execution Plans
Indexing Strategies
Deadlocks
Partitioning
Isolation Levels
Window Functions
Materialized Views
Query Optimization
Sharding
CAP TheoremAdvanced Interview Quick Revision
✓ Execution Plans
✓ Index Seek vs Scan
✓ Partitioning
✓ Deadlocks
✓ Isolation Levels
✓ Window Functions
✓ Query Optimization
✓ Materialized Views
✓ Sharding
✓ CAP TheoremSummary
Advanced SQL interviews focus heavily on performance tuning, indexing, transactions, execution plans, scalability, and database architecture. Understanding these concepts demonstrates the ability to work with large-scale production databases and enterprise systems.
In this lesson, you learned:
- 40 Advanced SQL Interview Questions
- Query Optimization
- Execution Plans
- Indexing
- Transactions
- Isolation Levels
- Window Functions
- Data Warehousing
- Sharding
- Distributed Systems
Next Step
Continue to the next lesson:
SQL Scenario-Based Interview Questions →
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for SQL Interview Questions Advanced.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this SQL topic.
Search Terms
sql, sql complete guide, sql tutorial, sql notes, complete, guide, interview, questions
Related SQL Topics