Skip to main content

Expert MS SQL Interview Questions

Curated Expert-level MS SQL interview questions for developers targeting expert positions. 41 questions available.

Last updated:

MS SQL Interview Questions & Answers

Skip to Questions

Welcome to our comprehensive collection of MS SQL interview questions and answers. This page contains expertly curated interview questions covering all aspects of MS SQL, from fundamental concepts to advanced topics. Whether you're preparing for an entry-level position or a senior role, you'll find questions tailored to your experience level.

Our MS SQL interview questions are designed to help you:

  • Understand core concepts and best practices in MS SQL
  • Prepare for technical interviews at all experience levels
  • Master both theoretical knowledge and practical application
  • Build confidence for your next MS SQL interview

Each question includes detailed answers and explanations to help you understand not just what the answer is, but why it's correct. We cover topics ranging from basic MS SQL concepts to advanced scenarios that you might encounter in senior-level interviews.

Use the filters below to find questions by difficulty level (Entry, Junior, Mid, Senior, Expert) or focus specifically on code challenges. Each question is carefully crafted to reflect real-world interview scenarios you'll encounter at top tech companies, startups, and MNCs.

Questions

41 questions
Q1:

What Is Query Tuning and Why Is It Necessary in SQL Server?

Expert

Answer

Query tuning is the process of optimizing SQL statements so they consume fewer CPU cycles, memory, I/O, and locking resources. Poorly written queries slow down the entire SQL Server instance by overusing limited system resources.

The goal is predictable, scalable performance by reducing logical reads, avoiding unnecessary work, using optimal joins, minimizing spills, and improving concurrency.

Quick Summary: Query tuning is finding why a query is slow and making it faster without changing what it returns. SQL Server's optimizer is good but not perfect — bad statistics, missing indexes, parameter sniffing, and poorly-written SQL all require human intervention. Tuning is reading execution plans, fixing root causes, and measuring the improvement.
Q2:

What is Query Tuning, and why is it necessary in SQL Server?

Expert

Answer

Query tuning is the process of analyzing how SQL Server executes a query and optimizing it to minimize resource usage. Because SQL Server has finite CPU, memory, I/O, and concurrency capacity, an inefficient query can slow down the entire system. Tuning ensures optimal execution paths, fewer logical reads, reduced data movement, faster joins, and short lock durations. The goal is predictable, scalable performance.

Quick Summary: Query tuning improves query performance by identifying inefficiencies in execution plans. Even good databases accumulate slow queries as data grows, schemas change, or statistics drift. Without tuning, a query that ran in 10ms at 10K rows can take 10 seconds at 10M rows with the same code.
Q3:

What is the difference between Estimated and Actual Execution Plans?

Expert

Answer

The estimated plan shows SQL Server's predicted execution strategy based on statistics before running the query. The actual plan shows what really happened: row counts, spills, memory usage, and operator execution. Estimated plans are safe for production; actual plans reveal real bottlenecks like bad estimates, scans, or sorts. Both are essential for diagnosing performance issues.

Quick Summary: Estimated plan uses statistics to guess row counts and show the planned approach. Actual plan captures what really happened — actual rows, actual executions, actual time. The gap between estimated and actual row counts is the first thing to look at. A big gap means bad statistics are misleading the optimizer.
Q4:

Why do row estimation errors cause performance issues?

Expert

Answer

SQL Server uses estimated row counts to choose join types, memory grants, and index strategies. If estimates are inaccurate, SQL Server may choose poor plans. Overestimation causes excessive memory allocation, while underestimation causes spills, nested loops, and excessive lookups. Accurate cardinality estimation is fundamental to stable performance.

Quick Summary: Row estimation errors cause the optimizer to pick wrong strategies. If it thinks a join produces 100 rows but it produces 1 million, it might choose Nested Loops (correct for 100 rows) which is catastrophic at 1 million. Estimation errors cascade — each wrong estimate compounds into a progressively worse plan.
Q5:

What happens when a query spills to TempDB?

Expert

Answer

A query spills when SQL Server lacks sufficient memory for operations like sort or hash. SQL offloads intermediate results to TempDB, causing heavy disk I/O and slow execution. Frequent spills indicate bad estimates, missing indexes, or insufficient memory. Repeated spills degrade overall SQL Server performance.

Quick Summary: When a sort or hash operation needs more memory than its grant, SQL Server writes intermediate data to TempDB. This turns an in-memory operation into disk I/O — often 10-100x slower. You'll see Sort Warnings or Hash Warnings in the execution plan. Fix: update statistics, or use OPTION(MIN_GRANT_PERCENT) hints.
Q6:

What is Parameter Sniffing, and how does it affect performance?

Expert

Answer

Parameter sniffing allows SQL Server to reuse cached execution plans based on the first parameter values supplied. If those values are atypical, the plan may be inefficient for later executions. This leads to performance instability. Solutions include OPTION(RECOMPILE), OPTIMIZE FOR UNKNOWN, local variables, or plan forcing when appropriate.

Quick Summary: SQL Server caches a procedure's plan based on the parameter values from the first compilation. If those values were unusual (e.g., a rare customer ID), the plan is optimized for that case. When typical values run later, they use a plan that's wrong for them. Symptoms: same procedure runs fast sometimes, slow other times.
Q7:

Why is choosing the correct JOIN type critical for performance?

Expert

Answer

Join type determines how SQL Server matches rows. Nested loops are ideal for small sets, merge joins require sorted input, and hash joins work on large, unsorted sets. Incorrect join choices cause excessive I/O, CPU load, and slowdowns. SQL chooses join types based on row estimates and indexes, making accurate statistics essential.

Quick Summary: The wrong JOIN type can transform a millisecond query into a minutes-long one. Nested Loops on a large unsorted table causes repeated random I/O. Hash Match without enough memory spills to TempDB. Choose the join type the optimizer picks naturally with good indexes and statistics — forcing the wrong one manually is rarely right.
Q8:

What causes Hash Match operations, and why can they become bottlenecks?

Expert

Answer

Hash matches occur when SQL Server must build a memory-based hash table for joins or aggregates. They appear when inputs are unsorted or not indexed. They become bottlenecks when memory is insufficient, causing spills to TempDB. Hash operations can be CPU-intensive and degrade concurrency.

Quick Summary: Hash Match builds a hash table in memory and probes it — it's chosen when inputs are large and unsorted with no usable index. Bottlenecks: it needs a significant memory grant, and if that grant is insufficient, it spills to TempDB. Adding the right index often eliminates Hash Match entirely in favor of Nested Loops or Merge Join.
Q9:

How do SARGable expressions influence performance?

Expert

Answer

A SARGable expression allows SQL Server to use indexes efficiently. Non-SARGable predicates (functions on columns, mismatched types) force scans, increasing logical reads and slowing queries. SARGability is foundational for scaling queries on large datasets.

Quick Summary: SARGable expressions allow the optimizer to use index seeks by passing the filter condition directly into the index. Non-SARGable expressions (wrapping the column in a function, CAST, or CONVERT) force a table or index scan. Always filter on the raw column, and ensure data types match without implicit conversion.
Q10:

Why is reducing logical reads more important than reducing elapsed time?

Expert

Answer

Elapsed time varies with workload and server load, but logical reads measure actual data touched. Reducing logical reads consistently reduces CPU, I/O, and cache pressure. Logical reads are the primary, stable metric for performance tuning.

Quick Summary: Elapsed time includes waits — network, I/O, locks. Logical reads measure actual work done by the query engine. A query that takes 5 seconds but does 1 million logical reads has a different problem than one that takes 5 seconds waiting for a lock. Reducing logical reads means the query is fundamentally more efficient.
Q11:

What is the importance of covering indexes in performance tuning?

Expert

Answer

A covering index includes all columns a query needs. This eliminates key lookups and reduces I/O. Covering indexes provide dramatic performance improvements in OLTP systems by minimizing data access and improving plan efficiency.

Quick Summary: A covering index includes all columns a query needs — so SQL Server can answer it entirely from the index without touching the base table. This eliminates key lookups, reduces logical reads, and often dramatically cuts query time. Add SELECT columns via INCLUDE to cover without making the index key unnecessarily wide.
Q12:

When does SQL Server choose Index Seek over Index Scan?

Expert

Answer

SQL Server chooses an index seek when predicates match the index key order and are selective. Otherwise, SQL chooses a scan to avoid expensive random lookups. Seek-friendly index design is critical for optimal performance.

Quick Summary: SQL Server uses Index Seek when the filter is selective (returns a small fraction of rows) and the column is part of an index with a matching leading key. It uses Index Scan when the filter is broad or the index doesn't match well. SARGable predicates are what make seeks possible.
Q13:

What causes Key Lookups, and why are they expensive?

Expert

Answer

Key lookups occur when a non-clustered index lacks required columns. SQL must fetch missing columns from the clustered index for each qualifying row. With many rows, this becomes slow and I/O-heavy. Solutions include covering indexes or query redesign.

Quick Summary: Key lookups happen when a non-clustered index satisfies the WHERE clause, but the SELECT needs columns not in the index. SQL Server fetches those from the clustered index — one random I/O per row. For 100 rows it's fine; for 10,000 rows it's painful. Fix: add INCLUDE columns to the non-clustered index to cover the query.
Q14:

What is the purpose of Statistics in SQL Server performance?

Expert

Answer

Statistics describe column data distribution. SQL Server uses them to estimate row counts and choose efficient plans. Outdated or missing statistics lead to poor estimates and unstable performance. Keeping statistics fresh is essential for reliable query optimization.

Quick Summary: Statistics contain column value histograms used by the optimizer to estimate result set sizes. Good statistics → accurate row estimates → good plan choices. The optimizer doesn't run your query to see how many rows come back — it trusts its statistics. Out-of-date stats from large data loads are a top cause of performance regression.
Q15:

Why do implicit conversions degrade performance?

Expert

Answer

Implicit conversions prevent index usage by forcing SQL Server to convert values at runtime. This leads to scans instead of seeks, higher CPU usage, and slower joins. Matching data types between columns and parameters is vital.

Quick Summary: Implicit conversion happens when two columns being compared have different data types — SQL Server silently converts one. This often makes the comparison non-SARGable, forcing a full scan instead of a seek. Common: comparing nvarchar column to a varchar parameter. Always match data types in joins and WHERE clauses.
Q16:

How does high fragmentation affect performance?

Expert

Answer

Fragmentation scatters index pages, causing more I/O during seeks and reducing read-ahead efficiency. High fragmentation slows down queries and increases disk activity. Regular index maintenance helps restore performance.

Quick Summary: Index fragmentation means leaf pages are out of logical order. When SQL Server does a range scan, it should read pages sequentially — fragmentation turns that into random I/O. Over 30% fragmentation is worth addressing. Reorganize (online, light) for moderate fragmentation; Rebuild (offline or online with Enterprise) for heavy fragmentation.
Q17:

Why is TempDB optimization critical for performance tuning?

Expert

Answer

TempDB handles sorts, hashes, temp tables, versioning, and spill operations. Heavy workloads can cause latch contention and I/O bottlenecks. Optimizing TempDB improves concurrency and stabilizes system-wide performance.

Quick Summary: TempDB is shared by all sessions — temp tables, sorts, hash joins, row versioning all land there. Contention on TempDB allocation pages throttles the whole server. Spill-to-TempDB from undersized memory grants is often the hidden bottleneck in slow queries. Multiple data files and fast NVMe storage are baseline requirements.
Q18:

What are Memory Grants, and why do they impact performance?

Expert

Answer

Memory grants are allocations for joins, sorts, and aggregates. Overestimated grants waste memory; underestimated grants cause spills to TempDB. Accurate row estimation and indexing ensure proper memory usage.

Quick Summary: Memory grants are pre-allocated for sort and hash operations. Underallocated → spill to TempDB. Overallocated → grants lock up RAM, starving other queries. Both situations degrade overall throughput. The root fix is always accurate statistics — the optimizer sizes grants based on estimated row counts.
Q19:

How does excessive recompilation affect query performance?

Expert

Answer

Excessive recompilation forces SQL Server to repeatedly rebuild execution plans, increasing CPU consumption and causing unpredictable performance. Causes include volatile schema, unstable statistics, and widely varying parameters.

Quick Summary: Recompilation forces SQL Server to rebuild an execution plan from scratch. Some recompilation is healthy (catching plan regressions). Excessive recompilation wastes CPU — each compilation takes time. Caused by: schema changes in temp tables, statistics updates, or using OPTION(RECOMPILE) on high-frequency queries.
Q20:

Why do large IN clauses degrade performance?

Expert

Answer

Large IN lists complicate cardinality estimation and increase parsing overhead. SQL Server may misestimate row counts, leading to inefficient join choices. Using temp tables or joins improves accuracy and performance.

Quick Summary: Large IN lists force SQL Server to evaluate many OR conditions or build a large hash structure. For very long lists (1000+ values), performance degrades because the optimizer can't estimate the combined selectivity well. Alternative: load the values into a temp table and JOIN — SQL Server handles joins far more efficiently than giant IN lists.
Q21:

What is the significance of identifying the most expensive operators in execution plans?

Expert

Answer

Most query time is spent in one or two heavy operators like hash joins, sorts, or scans. Identifying these bottlenecks allows focused tuning efforts, resulting in faster optimization and greater performance gains.

Quick Summary: The most expensive operator in an execution plan (highest % cost) is where the most work is happening. Focus tuning there first — it's the bottleneck. A 95% cost table scan means adding an index there will have far more impact than optimizing anything else in the plan. Always fix the biggest problem first.
Q22:

What are the most common root causes of slow queries in production?

Expert

Answer

Slow production queries usually result from:

  • Missing or poorly designed indexes
  • Incorrect cardinality estimates
  • High fragmentation
  • Outdated statistics
  • Parameter sniffing issues
  • Excessive key lookups
  • TempDB contention or spills
  • Implicit conversions
  • Heavy sorts/hashes causing CPU pressure
  • Blocking or deadlocks

Most performance issues are caused by a combination rather than a single factor.

Quick Summary: Common root causes of slow queries: missing or outdated indexes, stale statistics, parameter sniffing, implicit data type conversions, non-SARGable predicates, lock blocking, memory pressure (spills), excessive recompilation, and poorly written loops or cursors instead of set-based operations.
Q23:

How do you identify which query is slowing down the system?

Expert

Answer

To identify slow queries, examine:

  • sys.dm_exec_query_stats – CPU, I/O, execution time
  • sys.dm_exec_requests – currently running queries
  • sys.dm_tran_locks – blocking analysis
  • Extended Events – deep tracing
  • Query Store – historical regressions

This helps pinpoint which query and which operator is causing server slowdown.

Quick Summary: Start with sys.dm_exec_query_stats sorted by total_worker_time or total_elapsed_time to find the costliest queries. Query Store gives historical data with plan regression tracking. Wait stats (sys.dm_os_wait_stats) show whether slowdowns are CPU, I/O, or lock-related. Always measure before tuning — don't guess.
Q24:

How do you detect blocking issues and understand who is blocking whom?

Expert

Answer

Blocking is identified using:

  • sys.dm_exec_requests – blocking_session_id
  • sys.dm_os_waiting_tasks – wait chains
  • Activity Monitor – high-level visualization

Blocking chains often originate from a long-running transaction holding critical locks.

Quick Summary: Use sys.dm_exec_requests to see currently waiting sessions and what they're waiting for. sys.dm_os_waiting_tasks shows the blocking chain — who is waiting on whom. The session at the head of the chain with no wait is the blocker. That's the transaction to investigate — long-running or uncommitted transactions are the usual culprit.
Q25:

What is the difference between blocking and deadlocking?

Expert

Answer

Blocking occurs when one transaction holds a lock and others wait behind it.

Deadlock occurs when two transactions wait on each other, creating a cycle.

SQL Server detects deadlocks and kills one transaction (the victim) to resolve the cycle.

Quick Summary: Blocking: transaction A waits for transaction B to release a lock — eventually resolves when B commits or rolls back. Deadlock: two transactions each hold what the other needs — neither can proceed without intervention. SQL Server resolves deadlocks by killing one victim. Blocking is slow; deadlocks are forced terminations.
Q26:

Which indexing mistakes cause the most issues in high-traffic systems?

Expert

Answer

Common indexing mistakes include:

  • Too many non-clustered indexes
  • Lack of covering indexes for hot queries
  • Poor clustered key choice (wide, GUID, non-sequential)
  • Duplicate or overlapping indexes
  • Not using filtered indexes

These increase I/O, fragmentation, and reduce plan efficiency.

Quick Summary: Top indexing mistakes: too many indexes (slows writes, wastes space), wrong leading column (index unused), missing covering columns (causes key lookups), ignoring fill factor (causes page splits), never maintaining fragmentation, and duplicate indexes that the optimizer ignores while still costing maintenance overhead.
Q27:

Why do large table scans cause severe slowdowns?

Expert

Answer

Large scans:

  • Generate heavy I/O
  • Evict useful pages from buffer pool
  • Increase CPU usage
  • Slow concurrent users

One bad scan can impact dozens of users in a production environment.

Quick Summary: Large table scans read every page in the table — for a 100GB table, that's potentially GBs of I/O. This pushes useful pages out of the buffer pool, increases disk read time, and spikes CPU. In a shared server, one scan-heavy query can degrade performance for all other sessions simultaneously.
Q28:

Which issues cause sudden regression in query performance?

Expert

Answer

Sudden regressions often occur due to:

  • Updated statistics
  • Parameter sniffing plan changes
  • Index changes
  • Fragmentation spikes
  • Failover causing cold cache
  • Data growth affecting plan shapes
Quick Summary: Plan regressions after stats updates (optimizer uses new distribution data), index rebuilds (resets statistics), database compatibility level changes (new CE model), and SQL Server upgrades. Queries that were fast can suddenly get a different plan that's wrong for current data. Query Store is the primary tool to detect and revert regressions.
Q29:

What is the importance of Query Store in high-traffic systems?

Expert

Answer

Query Store provides:

  • Plan history recording
  • Runtime performance metrics
  • Ability to revert to stable plans
  • Insight into parameter sniffing behavior

It acts as a "black box recorder" for SQL Server.

Quick Summary: Query Store tracks every query's execution history — plan, duration, CPU, I/O, logical reads. In high-traffic systems it enables: detecting plan regressions instantly, forcing known-good plans, identifying top resource consumers, and understanding query behavior over time without relying on memory-only DMVs that reset on restart.
Q30:

Why should TempDB be placed on fast storage?

Expert

Answer

TempDB handles:

  • Sorts
  • Hash operations
  • Version store
  • Spills
  • Temporary objects

Slow TempDB I/O becomes a bottleneck for the entire SQL Server instance.

Quick Summary: TempDB is heavily used for sort spills, hash join overflows, and temp table operations. Slow TempDB I/O directly slows every query that spills. NVMe SSDs for TempDB are a standard recommendation. Separating TempDB from data/log files on dedicated drives avoids I/O contention between user queries and TempDB operations.
Q31:

Why are wide clustered keys a long-term performance problem?

Expert

Answer

The clustered key is included in all non-clustered indexes. Wide keys increase:

  • Storage footprint
  • Read/write cost
  • Fragmentation
  • Memory usage

Narrow, sequential keys are ideal.

Quick Summary: Wide clustered keys (GUIDs, composite multi-column keys) are included in every non-clustered index as the row locator. A 16-byte GUID key in a table with 10 non-clustered indexes multiplies key storage significantly. Wide keys also increase page splits and fragmentation over time. An integer identity key keeps everything compact.
Q32:

How do you troubleshoot excessive CPU usage in SQL Server?

Expert

Answer

Troubleshooting steps:

  • Identify high-CPU queries in DMVs
  • Check plans for sorts, hashes, conversions
  • Validate statistics
  • Review index usage
  • Investigate parallelism waits
  • Check server MAXDOP settings
Quick Summary: For CPU issues: check sys.dm_exec_query_stats for queries with high total_worker_time. Look for missing indexes, non-SARGable predicates, or excessive recompilation. Execution plans showing large sorts, hash joins, or scans on big tables are CPU-heavy. Fixing those reduces CPU — indexes shift work from CPU to I/O.
Q33:

What causes excessive memory usage in SQL Server?

Expert

Answer

Causes include:

  • Large memory grants
  • Hash and sort operations
  • Poor cardinality estimates
  • Too many concurrent queries
  • SQL caching large data pages
Quick Summary: Excessive memory usage causes: buffer pool pressure (too many large scans pushing data pages in), oversized memory grants (queries holding memory others need), and plan cache bloat (thousands of single-use plans from non-parameterized queries). Use sys.dm_os_memory_clerks to find which component is consuming the most.
Q34:

Why are CROSS JOINs dangerous in real systems?

Expert

Answer

CROSS JOINs produce Cartesian products. They may:

  • Explode row counts
  • Consume massive CPU
  • Stress memory
  • Cause system freezes

They must be used intentionally and carefully.

Quick Summary: CROSS JOIN produces every combination of rows from two tables — N × M rows. A CROSS JOIN between two 10,000-row tables produces 100 million rows. Usually unintentional (a forgotten JOIN condition). In real systems, accidental CROSS JOINs can bring down a server by overwhelming memory, CPU, and TempDB simultaneously.
Q35:

Which situations require table partitioning?

Expert

Answer

Use partitioning when:

  • Tables contain hundreds of millions of rows
  • Fast archiving is required
  • Hot/cold data separation is needed
  • Maintenance takes too long
Quick Summary: Use partitioning when: tables exceed 50-100GB and queries naturally filter on a date or range column, you need to archive/purge old data efficiently (partition switching is instant), or you want to separate hot and cold data across different storage tiers. Smaller tables rarely benefit enough to justify the complexity.
Q36:

How does SQL Server handle large deletes efficiently?

Expert

Answer

Large deletes cause heavy logging, lock escalation, and blocking. Solutions:

  • Batch deletes
  • Partition switching
  • Mark-and-archive patterns
Quick Summary: Delete large data in batches — DELETE TOP (10000) WHERE ... in a loop — instead of one massive DELETE. A single large delete holds locks for a long time, blocks other sessions, and generates a huge transaction log entry. Batching keeps transactions short, log growth manageable, and blocking minimal.
Q37:

Why do developers use OUTPUT clause for auditing?

Expert

Answer

The OUTPUT clause returns affected rows without re-querying the table. It is efficient for:

  • Auditing changes
  • Logging
  • Data replication
Quick Summary: The OUTPUT clause returns rows affected by INSERT, UPDATE, DELETE, or MERGE — directly from the statement without an extra SELECT. For auditing: INSERT INTO AuditLog SELECT * FROM deleted in a delete's OUTPUT clause captures exactly what was removed, atomically in the same transaction, with no separate read needed.
Q38:

What is the impact of missing foreign keys?

Expert

Answer

Missing foreign keys:

  • Prevent automatic referential integrity
  • Hurt query optimization because relationships are unknown
  • Cause poor join strategy selection
Quick Summary: Missing foreign keys mean the database won't prevent orphaned rows — orders with no customer, invoice lines with no invoice. The application must enforce referential integrity, and if it misses a case, the data silently corrupts. Missing FKs also remove optimizer hints — SQL Server uses FK metadata to simplify join plans.
Q39:

Why minimize the number of columns in non-clustered indexes?

Expert

Answer

Extra columns increase:

  • Index size
  • Maintenance overhead
  • Memory usage

Indexes should be narrow and purpose-specific.

Quick Summary: Every non-clustered index duplicates the clustered key columns as the row locator. Wide index keys = larger index pages = more I/O to scan the index. Narrow indexes fit more entries per page, require less memory in the buffer pool, and are faster to scan. Only index the columns you actually need for your queries.
Q40:

When should filtered indexes be used?

Expert

Answer

Filtered indexes are ideal when:

  • Queries target selective subsets
  • Columns contain sparse values
  • Large tables need optimized predicate performance
Quick Summary: Use filtered indexes when queries consistently target a predictable subset: active records (IsDeleted = 0), a specific status, or a regional subset. The index is smaller, statistics are more accurate for that subset, and seek performance is higher. Particularly effective when the indexed subset is a small percentage of the total rows.
Q41:

What steps do you take before rewriting a slow query?

Expert

Answer

Before rewriting a query:

  • Check execution plan
  • Validate statistics
  • Check indexes
  • Identify costly operators
  • Try small predicate adjustments
  • Confirm rewrite is necessary

Most problems are fixed by plan adjustments, not rewrites.

Quick Summary: Before rewriting: capture the actual execution plan and compare estimated vs actual rows. Check wait stats and blocking. Look at logical reads (SET STATISTICS IO ON). Identify the highest-cost operators. Update statistics and check for missing indexes first — often fixes the query without any rewrite. Rewrite only after understanding the root cause.

Curated Sets for MS SQL

No curated sets yet. Group questions into collections from the admin panel to feature them here.

Ready to level up? Start Practice