Skip to main content

Mid MS SQL Interview Questions

Curated Mid-level MS SQL interview questions for developers targeting mid positions. 20 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

20 questions
Q1:

Explain in detail how SQL Server processes different types of JOINs internally.

Mid

Answer

SQL Server evaluates JOINs using physical operators such as Nested Loops, Merge Join, and Hash Join. INNER, LEFT, RIGHT, and FULL JOINs all use these strategies depending on data size, indexes, and sorting.

INNER JOIN: Returns matching rows only.

LEFT/RIGHT JOIN: Returns matching rows plus NULL for non-matching.

FULL JOIN: Returns all matches + unmatched from both sides.

CROSS JOIN: Cartesian product; typically expensive.

Proper indexing affects which join operator SQL Server chooses.

Quick Summary: SQL Server supports Nested Loops (small outer table, indexed inner), Merge Join (both inputs sorted — often the fastest), and Hash Match (large unsorted inputs — memory intensive). The optimizer picks based on data size and index availability. Nested Loops is cheapest for small datasets; Hash Match is a fallback for large ones.
Q2:

Explain Nested Loops, Merge Join, and Hash Join with when each is chosen.

Mid

Answer

Nested Loops: Best for small outer input and indexed inner table. Great for OLTP random lookups.

Merge Join: Requires sorted inputs. Very fast for large, sorted datasets.

Hash Join: Best for large, unsorted sets. Spills to TempDB if memory is insufficient.

Quick Summary: Nested Loops: iterate outer rows, seek matching inner rows via index — best when outer set is small. Merge Join: both inputs must be pre-sorted; scan both simultaneously — fast when indexes provide sort order. Hash Match: build a hash table from smaller input, probe with larger — chosen when sorts aren't available and data is large.
Q3:

What is a Transaction? Explain ACID with real SQL Server implications.

Mid

Answer

A transaction is a unit of work ensured by ACID properties:

  • Atomicity: All-or-nothing behavior.
  • Consistency: Constraints must remain valid.
  • Isolation: Controls concurrency.
  • Durability: Committed data survives crashes via logging.
Quick Summary: A transaction groups multiple operations into one atomic unit. In SQL Server: Atomicity means the transaction commits fully or rolls back entirely. Consistency ensures constraints hold. Isolation controls what concurrent transactions can see. Durability means committed data survives crashes via the transaction log.
Q4:

Explain SQL Server Transaction Isolation Levels with practical use-cases.

Mid

Answer

Isolation levels include:

READ UNCOMMITTED: Allows dirty reads; used in analytics.

READ COMMITTED: Default; prevents dirty reads.

RCSI: Uses version store; avoids blocking.

REPEATABLE READ: Prevents non-repeatable reads.

SERIALIZABLE: Highest isolation; heavy locking.

SNAPSHOT: Uses row versioning; avoids shared locks.

Quick Summary: Read Uncommitted: sees dirty (uncommitted) data — fastest but risky. Read Committed: default, only reads committed data but allows non-repeatable reads. Repeatable Read: locks read rows, prevents changes. Serializable: full isolation, range locks prevent phantom reads. Snapshot: uses row versions instead of locks — concurrent and clean.
Q5:

Explain Pessimistic vs Optimistic Concurrency in SQL Server.

Mid

Answer

Pessimistic: Uses locks to avoid conflicts; good for heavy-write systems.

Optimistic: Uses row versioning; detects conflicts at commit.

Quick Summary: Pessimistic: lock the data before you use it — nobody else can change it until you're done. Low concurrency but zero conflict risk. Optimistic: don't lock on read; check at commit time if data changed. Higher concurrency but requires retry logic on conflict. SQL Server's Snapshot Isolation is an optimistic approach.
Q6:

What is Deadlock? How does SQL Server detect and resolve it?

Mid

Answer

A deadlock occurs when sessions wait on each other indefinitely. SQL Server detects deadlocks every 5 seconds and selects a victim to kill.

Prevention includes using consistent resource order, short transactions, and proper indexes.

Quick Summary: A deadlock forms when transaction A holds lock 1 and wants lock 2, while transaction B holds lock 2 and wants lock 1. Neither can proceed. SQL Server's deadlock monitor runs every 5 seconds, detects the cycle, picks the cheapest victim, rolls it back, and lets the other complete. Victims get error 1205.
Q7:

Explain Lock Types (Shared, Update, Exclusive, Intent).

Mid

Answer

Shared (S): For reading.

Exclusive (X): For writing.

Update (U): Prevents deadlocks during read-to-write transitions.

Intent Locks: Used to manage lock hierarchy efficiently.

Quick Summary: Shared (S): read lock, multiple transactions can hold simultaneously. Update (U): intent to update — prevents two transactions from upgrading to exclusive at the same time. Exclusive (X): write lock, blocks all other access. Intent locks (IS, IX, SIX): signal at a higher level (table) that lower-level locks exist.
Q8:

What are Lock Waits and Blocking Chains? How do you debug them?

Mid

Answer

Blocking occurs when a session holds a lock required by another. Debugging tools include:

  • sp_whoisactive
  • sys.dm_exec_requests
  • Extended events
  • Activity Monitor
Quick Summary: Lock waits happen when one transaction needs a lock already held by another. A blocking chain is multiple transactions queued behind the head blocker. Debug using sys.dm_exec_requests, sys.dm_os_waiting_tasks, or the blocking report in Query Store. The head blocker is always the root cause — fix that transaction first.
Q9:

Explain TempDB usage in detail.

Mid

Answer

TempDB stores temp tables, table variables, hash join work tables, version store, spills, and cursor data. Best practices include multiple equal-sized files and SSD storage.

Quick Summary: TempDB stores: temp tables (#temp), table variables (@table), internal work tables for sorts and hash joins, row version store for snapshot isolation, DBCC operations, and Service Broker. It's recreated fresh every SQL Server restart. All user sessions share it — heavy TempDB use is a common performance bottleneck.
Q10:

Explain the Write-Ahead Logging (WAL) mechanism.

Mid

Answer

Before modifying a data page, SQL writes the log record first. This ensures durability and crash recovery.

Quick Summary: WAL guarantees every change is written to the transaction log (WAL) before it's written to data pages. On crash, SQL Server replays committed log records (redo) and undoes uncommitted ones (undo). This makes the database consistent after a crash without data loss. Durability in ACID depends entirely on WAL.
Q11:

Describe Transaction Log Architecture. Why does the log grow?

Mid

Answer

The log consists of Virtual Log Files (VLFs). Log grows due to long-running transactions, no log backups, replication delays, or index rebuilds inside transactions.

Quick Summary: The transaction log records every change in sequence — each log record has an LSN (Log Sequence Number). The log grows when: long-running transactions hold the log open, log backups aren't taken (Full recovery model), or replication hasn't consumed the log. Regular log backups keep it manageable.
Q12:

Explain CHECKPOINT and why it is important.

Mid

Answer

CHECKPOINT flushes dirty pages to disk to reduce crash recovery time and manage buffer pressure.

Quick Summary: CHECKPOINT flushes dirty pages (modified but not yet written) from the buffer pool to disk. It reduces crash recovery time — SQL Server only needs to replay changes since the last checkpoint instead of replaying the whole log. Automatic checkpoints run periodically; you can also force one manually.
Q13:

What is the difference between Full, Differential, and Log Backups?

Mid

Answer

Full: Entire database.

Differential: Changes since last full.

Log: Captures all log records since last log backup.

Quick Summary: Full backup: entire database, self-contained restore point. Differential backup: only pages changed since the last full backup — faster than full, smaller file. Log backup: transaction log since last log backup — enables point-in-time recovery. Together they form a restore chain: full + diff + logs.
Q14:

Explain Recovery Models (Simple, Full, Bulk-Logged).

Mid

Answer

Simple: Auto-truncates log; no PIT recovery.

Full: Requires log backups; supports PIT recovery.

Bulk-Logged: Minimally logs operations.

Quick Summary: Simple: log is truncated at each checkpoint — no point-in-time recovery, just restore to last full or diff backup. Full: log is kept until backed up — allows point-in-time restore, required for AlwaysOn. Bulk-Logged: like Full but bulk operations are minimally logged — faster loads but can't do point-in-time around them.
Q15:

What is Tail Log Backup?

Mid

Answer

A backup taken before restoring a damaged DB to prevent data loss. Captures last active log records.

Quick Summary: A tail-log backup captures the end of the transaction log after a failure but before restore begins. Without it, you lose all transactions between the last log backup and the crash. It's the last piece of the restore chain. Required when recovering a Full-model database to the point of failure.
Q16:

Explain the Restore Sequence in SQL Server.

Mid

Answer

Restore order: Full ? Differential ? Logs. Use NORECOVERY until final restore, then RECOVERY.

Quick Summary: Restore sequence: 1) Restore the full backup (NORECOVERY), 2) Apply each differential/log backup in order (NORECOVERY), 3) Apply final log backup with RECOVERY to bring the database online. NORECOVERY keeps the database in restoring state so you can apply more backups; RECOVERY completes the restore.
Q17:

What is AlwaysOn Availability Groups? Explain components.

Mid

Answer

AG consists of primary replica, secondary replicas, listener, and modes (sync/async). Provides HA, DR, and readable secondaries.

Quick Summary: AlwaysOn AG replicates databases from a primary replica to one or more secondary replicas using log shipping internally. Supports synchronous (zero data loss, needed for automatic failover) and asynchronous (lower latency for distant replicas). Secondaries can serve reads, backups, and reporting to offload the primary.
Q18:

Explain Log Shipping.

Mid

Answer

Log Shipping copies log backups to a secondary server for restore. Simple, reliable, but no automatic failover.

Quick Summary: Log shipping automatically copies and restores transaction log backups from a primary to one or more secondary servers. Simple and cheap — just a SQL Agent job. Secondaries are in restoring state (read-only via STANDBY). No automatic failover; manual intervention required. Predecessor to AlwaysOn AG.
Q19:

Explain Replication Types (Snapshot, Transactional, Merge).

Mid

Answer

Snapshot: Full copy; simple.

Transactional: Near real-time; best for reporting.

Merge: Bidirectional sync for disconnected systems.

Quick Summary: Snapshot replication copies a full snapshot of data to subscribers periodically — good for infrequently changed data. Transactional replication streams individual changes in near real-time — good for read scale-out. Merge replication allows both sides to make changes and merges them — complex, used for offline scenarios.
Q20:

What is Database Mirroring? Why is it deprecated?

Mid

Answer

Mirroring uses principal + mirror + witness for automatic failover. Deprecated in favor of Availability Groups.

Quick Summary: Database Mirroring sends transaction log blocks from principal to mirror in real-time. High-Safety mode (synchronous) = automatic failover with a witness. High-Performance mode (asynchronous) = potential data loss. Deprecated since SQL Server 2012 in favor of AlwaysOn AGs, which are more flexible and feature-rich.

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