Advanced SQL and Optimization Interview Questions SQL (original) (raw)

Last Updated : 2 Sep, 2025

Advanced SQL and Optimization covers performance tuning and complex features that make SQL powerful. Topics include stored procedures, triggers, window functions, CTEs, indexing, and query execution plans. Interview questions often explore how to optimize queries, manage transactions and locks, and use advanced functions for analytics and scalability in real-world applications.

1. What are the differences between SQL and NoSQL databases?

**SQL Databases:

**NoSQL Databases:

2. How is data integrity maintained in SQL databases?

Data integrity refers to the accuracy, consistency, and reliability of the data stored in the database. SQL databases maintain data integrity through several mechanisms:

3. What are the advantages of using stored procedures?

4. What is the difference between an index and a key in SQL?

**1. Index

2. **Key

5. What is the difference between a local and a global temporary table?

**Local Temporary Table:

**Global Temporary Table:

**Example:

CREATE TABLE #LocalTemp (ID INT);
CREATE TABLE ##GlobalTemp (ID INT);

6. What are partitioned tables, and when should we use them?

Partitioned tables divide data into smaller, more manageable segments based on a column’s value (e.g., date or region). Each partition is stored separately, making queries that target a specific partition more efficient. It is used when

7. What are the ACID properties of a transaction?

ACID is an acronym that stands for Atomicity, Consistency, Isolation, and Durability, four key properties that ensure database transactions are processed reliably.

8. What are the differences between isolation levels in SQL?

Isolation levels define the extent to which the operations in one transaction are isolated from those in other transactions. They are critical for managing concurrency and ensuring data integrity. Common isolation levels include:

**1. Read Uncommitted:

**2. Read Committed:

**3. Repeatable Read:

**4. Serializable:

9. What is the purpose of the WITH (NOLOCK) hint in SQL Server?

**Example:

SELECT *
FROM Orders WITH (NOLOCK);

This query fetches data from the Orders table without waiting for other transactions to release their locks.

10. How do you handle deadlocks in SQL databases?

Deadlocks occur when two or more transactions hold resources that the other transactions need, resulting in a cycle of dependency that prevents progress. Strategies to handle deadlocks include:

11. What is a database snapshot, and how is it used?

A database snapshot is a read-only, static view of a database at a specific point in time.

**Example:

CREATE DATABASE MySnapshot ON
(
NAME = MyDatabase_Data,
FILENAME = 'C:\Snapshots\MyDatabase_Snapshot.ss'
)
AS SNAPSHOT OF MyDatabase;

12. What are the differences between OLTP and OLAP systems?

**1. OLTP (Online Transaction Processing)

**2. OLAP (Online Analytical Processing)

13. What is a live lock, and how does it differ from a deadlock?

**1. Live Lock

2. **Deadlock

14. How do you implement dynamic SQL, and what are its advantages and risks?

Dynamic SQL is SQL code that is constructed and executed at runtime rather than being fully defined and static. In SQL Server: Use sp_executesql or EXEC. In other databases: Concatenate query strings and execute them using the respective command for the database platform.

**Syntax:

DECLARE @sql NVARCHAR(MAX)
SET @sql = 'SELECT * FROM ' + @TableName
EXEC sp_executesql @sql;

**Advantages:

**Risks:

15. What is the difference between horizontal and vertical partitioning?

Partitioning is a database technique used to divide data into smaller, more manageable pieces.

**Horizontal Partitioning:

**Vertical Partitioning:

**Key Difference:

16. What are the considerations for indexing very large tables?

Indexing large tables requires a careful approach to ensure that performance gains from faster queries outweigh the costs of increased storage and maintenance effort.

17. What is the difference between database sharding and partitioning?

**1. Sharding

**2. Partitioning

18. What are the best practices for writing optimized SQL queries?

19. How can you monitor query performance in a production database?

**SQL Server: Use Query Store, DMVs (Dynamic Management Views), and performance dashboards.
**MySQL: Use EXPLAIN, SHOW PROFILE, and the Performance Schema.
**PostgreSQL: Use EXPLAIN (ANALYZE), pg_stat_statements, and log-based monitoring.

20. What are the trade-offs of using indexing versus denormalization?

**1. Indexing

**Advantages:

**Disadvantages:

**2. Denormalization

**Advantages:

**Disadvantages:

**21. How does SQL handle recursive queries?

SQL handles recursive queries using Common Table Expressions (CTEs). A recursive CTE repeatedly references itself to process hierarchical or tree-structured data.

**Key Components:

**Example:

WITH RecursiveCTE (ID, ParentID, Depth) AS (
SELECT ID, ParentID, 1 AS Depth
FROM Categories
WHERE ParentID IS NULL
UNION ALL
SELECT c.ID, c.ParentID, r.Depth + 1
FROM Categories c
INNER JOIN RecursiveCTE r
ON c.ParentID = r.ID
)
SELECT * FROM RecursiveCTE;

**22. What are the differences between transactional and analytical queries?

**1. Transactional Queries:

**2. Analytical Queries:

**Key Differences:

Transactional queries support day-to-day operations and maintain data integrity.
Analytical queries support decision-making by providing insights from large datasets

**23. How can you ensure data consistency across distributed databases?

24. What is the purpose of the SQL PIVOT operator?

The PIVOT operator transforms rows into columns, making it easier to summarize or rearrange data for reporting.

**Example: Converting a dataset that lists monthly sales into a format that displays each month as a separate column.

SELECT ProductID, [2021], [2022]
FROM (
SELECT ProductID, YEAR(SaleDate) AS SaleYear, Amount
FROM Sales
) AS Source
PIVOT (
SUM(Amount)
FOR SaleYear IN ([2021], [2022])
) AS PivotTable;