MS SQL Interview Questions and Answers

Find 100+ MS SQL interview questions and answers to assess candidates’ skills in queries, indexing, stored procedures, performance tuning, and database administration.
By
WeCP Team

As organizations rely on relational databases for transactional systems, reporting, and analytics, recruiters must identify MS SQL Server professionals who can design, manage, and optimize high-performance database environments. Microsoft SQL Server is widely used in enterprise applications, BI systems, and mission-critical workloads.

This resource, "100+ MS SQL Interview Questions and Answers," is tailored for recruiters to simplify the evaluation process. It covers a wide range of topics—from SQL Server fundamentals to advanced database administration and performance tuning, including T-SQL, indexing, and security.

Whether you're hiring SQL Developers, Database Administrators (DBAs), Data Engineers, or BI Professionals, this guide enables you to assess a candidate’s:

  • Core MS SQL Knowledge: Tables, views, constraints, indexes, joins, subqueries, and basic T-SQL syntax.
  • Advanced Skills: Stored procedures, functions, triggers, execution plans, indexing strategies, transactions, locking, and query optimization.
  • Real-World Proficiency: Designing efficient schemas, optimizing slow queries, managing backups and restores, ensuring data security, and supporting production databases.

For a streamlined assessment process, consider platforms like WeCP, which allow you to:

  • Create customized MS SQL assessments tailored to development, administration, or analytics roles.
  • Include hands-on tasks such as writing complex queries, optimizing execution plans, or debugging performance issues.
  • Proctor exams remotely while ensuring integrity.
  • Evaluate results with AI-driven analysis for faster, more accurate decision-making.

Save time, enhance your hiring process, and confidently hire MS SQL professionals who can build, manage, and optimize reliable database systems from day one.

MS SQL Interview Questions

MS SQL – Beginner (1–40)

  1. What is MS SQL Server?
  2. What are the main components of SQL Server?
  3. What is a database?
  4. What is a table in SQL Server?
  5. What is a row and a column?
  6. What is a primary key?
  7. What is a foreign key?
  8. What is the difference between primary key and unique key?
  9. What is NULL in SQL Server?
  10. What are SQL constraints?
  11. What is the use of the SELECT statement?
  12. What is the WHERE clause used for?
  13. What is the ORDER BY clause?
  14. What is GROUP BY?
  15. What is HAVING clause?
  16. What is the difference between DELETE and TRUNCATE?
  17. What is the difference between DELETE and DROP?
  18. What are aggregate functions in SQL Server?
  19. What is COUNT(), SUM(), AVG(), MIN(), and MAX()?
  20. What is a database schema?
  21. What is normalization?
  22. What are the different normal forms?
  23. What is denormalization?
  24. What is an index?
  25. What is a clustered index?
  26. What is a non-clustered index?
  27. What is the difference between clustered and non-clustered index?
  28. What is a view?
  29. What is a stored procedure?
  30. What is a function in SQL Server?
  31. What is the difference between function and stored procedure?
  32. What is a transaction?
  33. What are ACID properties?
  34. What is COMMIT and ROLLBACK?
  35. What is IDENTITY column?
  36. What is a default constraint?
  37. What is CHECK constraint?
  38. What is NOT NULL constraint?
  39. What is the use of DISTINCT keyword?
  40. What is the difference between CHAR and VARCHAR?

MS SQL – Intermediate (1–40)

  1. What are joins in SQL Server?
  2. Explain INNER JOIN.
  3. Explain LEFT JOIN.
  4. Explain RIGHT JOIN.
  5. Explain FULL OUTER JOIN.
  6. What is a SELF JOIN?
  7. What is a CROSS JOIN?
  8. What is a subquery?
  9. What is a correlated subquery?
  10. What is the difference between UNION and UNION ALL?
  11. What are temporary tables?
  12. Difference between local and global temporary tables?
  13. What are table variables?
  14. Table variable vs temporary table?
  15. What is a Common Table Expression (CTE)?
  16. What are window functions?
  17. Explain ROW_NUMBER(), RANK(), and DENSE_RANK().
  18. What is partitioning in window functions?
  19. What is indexing strategy?
  20. What is a composite index?
  21. What is index fragmentation?
  22. How do you rebuild and reorganize indexes?
  23. What is execution plan?
  24. Difference between estimated and actual execution plan?
  25. What is query optimization?
  26. What is parameter sniffing?
  27. What is implicit and explicit conversion?
  28. What is TRY…CATCH in SQL Server?
  29. What is error handling in T-SQL?
  30. What is a cursor?
  31. Why cursors should be avoided?
  32. What is dynamic SQL?
  33. What is sp_executesql?
  34. What is SET NOCOUNT ON?
  35. What is the use of EXISTS?
  36. EXISTS vs IN?
  37. What is a trigger?
  38. Types of triggers in SQL Server?
  39. What is an AFTER trigger?
  40. What is an INSTEAD OF trigger?

MS SQL – Experienced (1–40)

  1. Explain SQL Server architecture in detail.
  2. What are system databases and their roles?
  3. Explain TempDB internals.
  4. How does SQL Server store data internally?
  5. What are pages and extents?
  6. Explain heap vs clustered table internals.
  7. What is latch vs lock?
  8. Types of locks in SQL Server?
  9. What is deadlock and how do you resolve it?
  10. What is isolation level?
  11. Explain all transaction isolation levels.
  12. What is snapshot isolation?
  13. What is row versioning?
  14. What is blocking and how do you troubleshoot it?
  15. Explain wait stats and wait types.
  16. How do you identify performance bottlenecks?
  17. What is Query Store?
  18. How does Query Store help in performance tuning?
  19. What is plan cache?
  20. What causes plan cache bloat?
  21. What is recompilation and why does it happen?
  22. Explain index seek vs index scan.
  23. What is covering index?
  24. How do you design indexes for OLTP systems?
  25. How do you design indexes for OLAP systems?
  26. What is partitioned table?
  27. How does table partitioning work internally?
  28. What is SQL Server Agent?
  29. Explain SQL Server backup types.
  30. Difference between full, differential, and log backup?
  31. What is point-in-time recovery?
  32. What is high availability vs disaster recovery?
  33. Explain Always On Availability Groups.
  34. What is database mirroring?
  35. What is replication and its types?
  36. What is log shipping?
  37. What is security architecture in SQL Server?
  38. Explain roles, logins, and users.
  39. How do you secure sensitive data in SQL Server?
  40. How do you troubleshoot slow-running queries in production?

MS SQL Interview Questions and Answers

Beginner (Q&A)

1. What is MS SQL Server?

MS SQL Server is a relational database management system (RDBMS) developed by Microsoft that is used to store, manage, retrieve, and manipulate structured data efficiently. It is designed to handle large volumes of data while ensuring data integrity, security, high availability, and performance.

SQL Server uses T-SQL (Transact-SQL) as its primary query language, which extends standard SQL with additional features such as variables, error handling, procedural logic, and system functions. It is widely used in enterprise applications, web applications, data warehousing, reporting systems, and business intelligence solutions.

Key strengths of MS SQL Server include:

  • Strong transaction management using ACID properties
  • Built-in security features like authentication, authorization, and encryption
  • Scalability and performance tuning capabilities
  • Integration with Microsoft tools such as Azure, Power BI, SSIS, SSRS, and SSAS

In simple terms, MS SQL Server acts as a central system where applications store and retrieve data reliably and securely.

2. What are the main components of SQL Server?

SQL Server consists of several core components that work together to manage data and support applications:

  1. Database Engine
    This is the core component responsible for data storage, query processing, indexing, transactions, locking, and concurrency control. It ensures data consistency and performance.
  2. SQL Server Management Studio (SSMS)
    A graphical tool used by developers and DBAs to write queries, manage databases, configure security, monitor performance, and perform administrative tasks.
  3. SQL Server Integration Services (SSIS)
    Used for ETL (Extract, Transform, Load) operations, data migration, and data integration from multiple sources.
  4. SQL Server Reporting Services (SSRS)
    Used to create, manage, and deliver reports in different formats like PDF, Excel, and web-based reports.
  5. SQL Server Analysis Services (SSAS)
    Supports OLAP and data modeling, enabling advanced analytics and business intelligence solutions.
  6. SQL Server Agent
    Used for job scheduling, automation, backups, and maintenance tasks.

Together, these components make SQL Server a complete data platform, not just a database.

3. What is a database?

A database is an organized collection of structured data stored electronically in a way that allows efficient access, management, and updating. In SQL Server, a database serves as a container that holds tables, views, stored procedures, functions, indexes, and other database objects.

Each database in SQL Server has:

  • Data files (.mdf, .ndf) where actual data is stored
  • Log files (.ldf) that record transactions for recovery and rollback

Databases help in:

  • Maintaining data integrity
  • Supporting transactions
  • Enforcing security and access control
  • Enabling backup and recovery

Examples include system databases (master, model, tempdb, msdb) and user-created databases for applications.

4. What is a table in SQL Server?

A table is a database object used to store data in a structured format of rows and columns. Each table represents a specific entity, such as customers, orders, or products.

A table consists of:

  • Columns: Define the data type and structure (e.g., name, age, salary)
  • Rows: Contain actual data records

Tables are fundamental to relational databases because:

  • They allow data organization
  • They support relationships with other tables using keys
  • They enable efficient querying and indexing

Tables can also have constraints, indexes, triggers, and relationships to maintain data accuracy and performance.

5. What is a row and a column?

A row represents a single record in a table. Each row contains data values for all columns defined in that table. For example, one row may represent one customer.

A column represents an attribute or field of the data. Each column has:

  • A name
  • A data type (INT, VARCHAR, DATE, etc.)
  • Optional constraints

Example:

Columns define the structure, while rows hold the actual data.

6. What is a primary key?

A primary key is a constraint that uniquely identifies each row in a table. It ensures that:

  • Each value is unique
  • No value is NULL

Primary keys are critical for:

  • Enforcing entity integrity
  • Creating relationships between tables
  • Improving query performance through indexing

Characteristics:

  • Only one primary key per table
  • Can be single-column or composite
  • Automatically creates a unique clustered index by default (unless specified otherwise)

Example: CustomerID as a primary key uniquely identifies each customer.

7. What is a foreign key?

A foreign key is a constraint that creates a relationship between two tables by referencing the primary key (or unique key) of another table.

Purpose of a foreign key:

  • Enforces referential integrity
  • Prevents invalid data insertion
  • Maintains consistency between related tables

Example:

  • Orders table has CustomerID as a foreign key
  • CustomerID references Customer table’s primary key

Foreign keys ensure that:

  • You cannot insert orphan records
  • You cannot delete referenced data without handling dependencies

8. What is the difference between primary key and unique key?

Both primary key and unique key enforce uniqueness, but they differ in important ways:

  • A primary key uniquely identifies records and does not allow NULL values
  • A unique key ensures uniqueness but allows one NULL value

Key differences:

  • Only one primary key per table
  • Multiple unique keys allowed
  • Primary key is used for entity identification
  • Unique key is used for business rule enforcement

Primary keys are essential, while unique keys are optional based on design needs.

9. What is NULL in SQL Server?

NULL represents the absence of a value. It does not mean zero, empty string, or false—it means unknown or not applicable.

Important characteristics:

  • NULL is not equal to anything, even another NULL
  • Comparisons using = do not work with NULL
  • Requires IS NULL or IS NOT NULL

Example:

SELECT * FROM Employees WHERE MiddleName IS NULL;

NULL helps represent missing or optional data but should be handled carefully to avoid incorrect query results.

10. What are SQL constraints?

SQL constraints are rules applied to columns or tables to enforce data integrity and consistency.

Common SQL Server constraints include:

  • PRIMARY KEY – Uniquely identifies records
  • FOREIGN KEY – Maintains referential integrity
  • UNIQUE – Ensures unique values
  • NOT NULL – Prevents NULL values
  • CHECK – Enforces logical conditions
  • DEFAULT – Assigns default values

Constraints ensure:

  • Invalid data cannot be inserted
  • Business rules are enforced at the database level
  • Data remains accurate and reliable

They act as the first line of defense for data quality in SQL Server.

11. What is the use of the SELECT statement?

The SELECT statement is the most fundamental SQL command used to retrieve data from one or more tables or views in SQL Server. It allows users to specify what data they want to see and how it should be presented.

Using SELECT, you can:

  • Fetch specific columns or all columns (SELECT *)
  • Filter rows using conditions
  • Sort results
  • Perform calculations
  • Aggregate data
  • Join multiple tables

Example:

SELECT CustomerName, City FROM Customers;

Key points:

  • It does not modify data
  • It works with tables, views, CTEs, and subqueries
  • It is the foundation for reporting, analytics, and application data access

In short, the SELECT statement is used to read and analyze data stored in SQL Server.

12. What is the WHERE clause used for?

The WHERE clause is used to filter rows based on specified conditions. It ensures that only relevant records are returned, updated, or deleted.

Without WHERE, SQL Server processes all rows in a table.

Example:

SELECT * FROM Employees WHERE Department = 'IT';

The WHERE clause supports:

  • Comparison operators (=, >, <, >=, <=)
  • Logical operators (AND, OR, NOT)
  • Pattern matching (LIKE)
  • Ranges (BETWEEN)
  • Sets (IN)
  • NULL checks (IS NULL)

It is essential for:

  • Performance optimization
  • Data accuracy
  • Implementing business logic

13. What is the ORDER BY clause?

The ORDER BY clause is used to sort query results in either ascending (ASC) or descending (DESC) order.

Example:

SELECT Name, Salary FROM Employees ORDER BY Salary DESC;

Key characteristics:

  • Default sorting order is ascending
  • Can sort by one or multiple columns
  • Can use column names, aliases, or column positions

Example with multiple columns:

ORDER BY Department ASC, Salary DESC;

ORDER BY improves:

  • Readability of result sets
  • Reporting and presentation
  • Data analysis clarity

14. What is GROUP BY?

The GROUP BY clause is used to group rows that have the same values in specified columns and then apply aggregate functions to each group.

Example:

SELECT Department, COUNT(*) 
FROM Employees 
GROUP BY Department;

How it works:

  • Rows are grouped based on column values
  • Aggregate functions calculate results per group

Rules:

  • All non-aggregated columns in SELECT must appear in GROUP BY
  • Commonly used with COUNT, SUM, AVG, MIN, MAX

GROUP BY is widely used for:

  • Reporting
  • Data summarization
  • Business analytics

15. What is the HAVING clause?

The HAVING clause is used to filter grouped data after aggregation. While WHERE filters rows before grouping, HAVING filters after grouping.

Example:

SELECT Department, COUNT(*) AS EmpCount
FROM Employees
GROUP BY Department
HAVING COUNT(*) > 10;

Key differences:

  • WHERE → works on individual rows
  • HAVING → works on aggregated results

Use cases:

  • Filtering totals
  • Applying conditions on aggregate values
  • Business rule enforcement on summary data

16. What is the difference between DELETE and TRUNCATE?

DELETE and TRUNCATE both remove data from a table, but they work very differently.

DELETE

  • Removes rows one by one
  • Can use WHERE clause
  • Fully logged (slower)
  • Can be rolled back
  • Triggers are fired

TRUNCATE

  • Removes all rows at once
  • Cannot use WHERE
  • Minimally logged (faster)
  • Cannot be rolled back
  • Resets identity values

Use DELETE for controlled removal and TRUNCATE for fast cleanup of entire tables.

17. What is the difference between DELETE and DROP?

DELETE removes data, while DROP removes the entire object.

DELETE

  • Removes rows from a table
  • Table structure remains
  • Can be rolled back
  • Triggers are fired

DROP

  • Removes the table completely
  • Structure and data are deleted
  • Cannot be rolled back
  • Dependent objects are affected

Example:

DROP TABLE Employees;

Use DELETE when you want to keep the table, and DROP when the table is no longer needed.

18. What are aggregate functions in SQL Server?

Aggregate functions perform calculations on a set of values and return a single summarized result.

They are commonly used with GROUP BY.

Common aggregate functions:

  • COUNT
  • SUM
  • AVG
  • MIN
  • MAX

Example:

SELECT AVG(Salary) FROM Employees;

Use cases:

  • Reports
  • Dashboards
  • Data analysis
  • Business intelligence

Aggregate functions ignore NULL values (except COUNT(*)).

19. What is COUNT(), SUM(), AVG(), MIN(), and MAX()?

These are the most commonly used aggregate functions:

  • COUNT() – Returns the number of rows
  • SUM() – Returns the total of numeric values
  • AVG() – Returns the average of numeric values
  • MIN() – Returns the smallest value
  • MAX() – Returns the largest value

Examples:

SELECT 
  COUNT(*) AS TotalEmployees,
  SUM(Salary) AS TotalSalary,
  AVG(Salary) AS AvgSalary,
  MIN(Salary) AS MinSalary,
  MAX(Salary) AS MaxSalary
FROM Employees;

They are essential for summary reports and decision-making.

20. What is a database schema?

A database schema is a logical container that organizes database objects such as tables, views, procedures, and functions.

Example:

SELECT * FROM Sales.Orders;

Benefits of schemas:

  • Logical organization
  • Security management
  • Namespace separation
  • Easier maintenance

Common schemas:

  • dbo (default)
  • sales
  • hr
  • finance

Schemas help structure large databases and improve security, clarity, and manageability.

21. What is normalization?

Normalization is a database design technique used to organize data efficiently by reducing data redundancy and improving data integrity. The main goal of normalization is to store data logically so that each piece of information is stored only once.

Normalization achieves this by:

  • Dividing large tables into smaller, related tables
  • Defining relationships using keys
  • Eliminating duplicate and inconsistent data

Benefits of normalization:

  • Reduces data duplication
  • Improves data consistency
  • Makes updates easier and safer
  • Saves storage space
  • Prevents update, insert, and delete anomalies

Normalization is most commonly applied in OLTP systems where data integrity is critical.

22. What are the different normal forms?

Normal forms are rules or guidelines that define levels of database normalization. Each normal form builds on the previous one.

  1. First Normal Form (1NF)
    • No repeating groups
    • Atomic column values
    • Unique rows
  2. Second Normal Form (2NF)
    • Must be in 1NF
    • No partial dependency on a composite key
  3. Third Normal Form (3NF)
    • Must be in 2NF
    • No transitive dependency
  4. Boyce–Codd Normal Form (BCNF)
    • Stronger version of 3NF
    • Every determinant is a candidate key
  5. Fourth Normal Form (4NF)
    • No multi-valued dependencies
  6. Fifth Normal Form (5NF)
    • Eliminates join dependencies

In real-world systems, most databases are normalized up to 3NF.

23. What is denormalization?

Denormalization is the intentional process of introducing redundancy into a database design to improve read performance.

Instead of splitting data into many tables, denormalization:

  • Combines related data into fewer tables
  • Reduces the need for complex joins
  • Improves query speed

Trade-offs:

  • Faster SELECT queries
  • Increased data redundancy
  • Higher risk of data inconsistency
  • More complex INSERT and UPDATE operations

Denormalization is commonly used in:

  • Reporting systems
  • Data warehouses
  • Read-heavy applications

24. What is an index?

An index is a database object that improves the speed of data retrieval operations on a table.

Indexes work similarly to a book index, allowing SQL Server to find data quickly without scanning the entire table.

Key points:

  • Improves SELECT query performance
  • Can slow down INSERT, UPDATE, DELETE
  • Uses additional storage
  • Created on one or more columns

Example:

CREATE INDEX idx_emp_name ON Employees(Name);

Indexes are essential for performance tuning in SQL Server.

25. What is a clustered index?

A clustered index determines the physical order of data in a table. The table’s data rows are stored in the same order as the clustered index key.

Key characteristics:

  • Only one clustered index per table
  • Often created on the primary key
  • Data pages are sorted based on index key
  • Faster for range queries

Example:

CREATE CLUSTERED INDEX idx_emp_id ON Employees(EmployeeID);

If a table has no clustered index, it is called a heap.

26. What is a non-clustered index?

A non-clustered index is a separate structure from the table that stores index keys and pointers to the actual data rows.

Key characteristics:

  • Multiple non-clustered indexes allowed per table
  • Does not change physical data order
  • Uses row locators to fetch data
  • Useful for selective queries

Example:

CREATE NONCLUSTERED INDEX idx_emp_email ON Employees(Email);

Non-clustered indexes are ideal for frequently searched columns.

27. What is the difference between clustered and non-clustered index?

FeatureClustered IndexNon-Clustered IndexData orderPhysical order of dataLogical order onlyCountOne per tableMultiple allowedStorageData stored in indexSeparate index structureSpeedFaster for range queriesFaster for lookupsDefaultCreated with primary keyCreated manually

In short, clustered index defines how data is stored, while non-clustered index defines how data is searched.

28. What is a view?

A view is a virtual table created using a SQL query. It does not store data itself but displays data dynamically from one or more tables.

Example:

CREATE VIEW vw_EmployeeDetails AS
SELECT Name, Department FROM Employees;

Benefits:

  • Simplifies complex queries
  • Enhances security
  • Improves code reuse
  • Provides abstraction

Views are commonly used in reporting and application development.

29. What is a stored procedure?

A stored procedure is a precompiled collection of SQL statements stored in the database and executed as a single unit.

Key advantages:

  • Improved performance
  • Code reuse
  • Enhanced security
  • Easier maintenance

Example:

CREATE PROCEDURE GetEmployees
AS
SELECT * FROM Employees;

Stored procedures can accept parameters and contain business logic.

30. What is a function in SQL Server?

A function is a database object that returns a value and can be used inside SQL statements.

Types of functions:

  • Scalar functions
  • Inline table-valued functions
  • Multi-statement table-valued functions

Example:

CREATE FUNCTION GetTax(@Salary INT)
RETURNS INT
AS
BEGIN
  RETURN @Salary * 0.1
END

Functions are used for calculations, validations, and reusable logic.

31. What is the difference between function and stored procedure?

A function and a stored procedure are both reusable database objects, but they serve different purposes and have different capabilities.

A function:

  • Must return a value (scalar or table)
  • Can be used inside SELECT, WHERE, JOIN, etc.
  • Cannot modify database state (no INSERT/UPDATE/DELETE directly)
  • Cannot use transactions
  • Is mainly used for calculations and data transformations

A stored procedure:

  • May or may not return a value
  • Cannot be used directly in a SELECT statement
  • Can modify data
  • Supports transactions and error handling
  • Used to implement business logic and workflows

In summary, functions are for computation, while stored procedures are for processing and operations.

32. What is a transaction?

A transaction is a logical unit of work that consists of one or more SQL statements that must be executed together as a single operation.

Example:

BEGIN TRANSACTION
UPDATE Accounts SET Balance = Balance - 100 WHERE ID = 1;
UPDATE Accounts SET Balance = Balance + 100 WHERE ID = 2;
COMMIT;

Transactions ensure:

  • Data consistency
  • Safe execution of multiple related operations
  • Protection from partial updates

If any statement fails, the transaction can be rolled back to maintain correctness.

33. What are ACID properties?

ACID properties define the reliability guarantees of transactions in SQL Server.

  1. Atomicity – All operations succeed or none do
  2. Consistency – Database moves from one valid state to another
  3. Isolation – Transactions do not interfere with each other
  4. Durability – Committed changes persist even after failures

These properties ensure data integrity, especially in multi-user environments.

34. What is COMMIT and ROLLBACK?

COMMIT and ROLLBACK are transaction control commands.

  • COMMIT
    • Saves all changes permanently
    • Ends the transaction successfully
  • ROLLBACK
    • Reverts all changes since the last commit
    • Used when errors occur

Example:

ROLLBACK;

They allow safe execution of critical operations and recovery from failures.

35. What is IDENTITY column?

An IDENTITY column is used to automatically generate unique numeric values for a column, commonly used as primary keys.

Syntax:

EmployeeID INT IDENTITY(1,1)

Key points:

  • Auto-incremented
  • Ensures uniqueness
  • Reduces manual input errors
  • Identity values may have gaps

It is widely used to uniquely identify records.

36. What is a default constraint?

A default constraint assigns a default value to a column when no value is provided during insertion.

Example:

Status VARCHAR(20) DEFAULT 'Active'

Benefits:

  • Ensures consistent data
  • Reduces need for null handling
  • Enforces business rules automatically

Default constraints help maintain data completeness.

37. What is CHECK constraint?

A CHECK constraint enforces a logical condition on column values.

Example:

CHECK (Age >= 18)

Key features:

  • Validates data before insertion
  • Prevents invalid values
  • Enforces business rules at database level

CHECK constraints improve data quality and reliability.

38. What is NOT NULL constraint?

The NOT NULL constraint ensures that a column cannot contain NULL values.

Example:

Name VARCHAR(50) NOT NULL

Purpose:

  • Forces mandatory data entry
  • Prevents missing critical information
  • Improves data integrity

Columns with NOT NULL must always have valid data.

39. What is the use of DISTINCT keyword?

The DISTINCT keyword is used to remove duplicate values from query results.

Example:

SELECT DISTINCT Department FROM Employees;

Key points:

  • Applied to entire row
  • Improves result clarity
  • Can impact performance on large datasets

It is commonly used in reporting and data analysis.

40. What is the difference between CHAR and VARCHAR?

Both CHAR and VARCHAR store character data, but they differ in storage behavior.

CHAR

  • Fixed-length
  • Pads extra spaces
  • Faster for fixed-size data

VARCHAR

  • Variable-length
  • Saves storage space
  • Better for variable-size text

Example:

Name CHAR(10)
Email VARCHAR(100)

Use CHAR for fixed-length values and VARCHAR for variable-length values.

Intermediate (Q&A)

1. What are joins in SQL Server?

Joins in SQL Server are used to combine rows from two or more tables based on a related column between them. Joins allow you to retrieve meaningful data that is spread across multiple tables in a relational database.

Why joins are needed:

  • Data is stored in normalized form across multiple tables
  • Joins help reconstruct related information
  • Enable complex queries across entities

Common join conditions are based on:

  • Primary key and foreign key relationships
  • Matching column values

Types of joins include:

  • INNER JOIN
  • LEFT JOIN
  • RIGHT JOIN
  • FULL OUTER JOIN
  • SELF JOIN
  • CROSS JOIN

Joins are fundamental for data retrieval, reporting, and analytics in SQL Server.

2. Explain INNER JOIN.

An INNER JOIN returns only the rows that have matching values in both tables involved in the join. Rows that do not have a matching record in the other table are excluded from the result.

Example:

SELECT e.Name, d.DepartmentName
FROM Employees e
INNER JOIN Departments d
ON e.DepartmentID = d.DepartmentID;

Key characteristics:

  • Returns intersection of two tables
  • Most commonly used join
  • Filters out unmatched records automatically

INNER JOIN is ideal when only related data is required.

3. Explain LEFT JOIN.

A LEFT JOIN (or LEFT OUTER JOIN) returns all rows from the left table and the matching rows from the right table. If no match exists, NULL values are returned for right table columns.

Example:

SELECT e.Name, d.DepartmentName
FROM Employees e
LEFT JOIN Departments d
ON e.DepartmentID = d.DepartmentID;

Key characteristics:

  • Always returns all rows from left table
  • Unmatched right table values appear as NULL
  • Useful for identifying missing relationships

LEFT JOIN is often used for data completeness analysis.

4. Explain RIGHT JOIN.

A RIGHT JOIN (or RIGHT OUTER JOIN) returns all rows from the right table and the matching rows from the left table. If no match exists, NULL values appear for left table columns.

Example:

SELECT e.Name, d.DepartmentName
FROM Employees e
RIGHT JOIN Departments d
ON e.DepartmentID = d.DepartmentID;

Key characteristics:

  • Opposite of LEFT JOIN
  • Less commonly used
  • Can often be rewritten as LEFT JOIN

RIGHT JOIN is used when the right table is the primary focus.

5. Explain FULL OUTER JOIN.

A FULL OUTER JOIN returns all rows from both tables, including matched and unmatched rows. When no match exists, NULL values are returned for missing columns.

Example:

SELECT e.Name, d.DepartmentName
FROM Employees e
FULL OUTER JOIN Departments d
ON e.DepartmentID = d.DepartmentID;

Key characteristics:

  • Combines LEFT and RIGHT JOIN results
  • Shows complete data from both tables
  • Useful for data reconciliation

FULL OUTER JOIN is ideal for comparison and audit scenarios.

6. What is a SELF JOIN?

A SELF JOIN is a join where a table is joined to itself. It is used when rows in the same table are related to each other.

Example:

SELECT e.Name AS Employee, m.Name AS Manager
FROM Employees e
LEFT JOIN Employees m
ON e.ManagerID = m.EmployeeID;

Key characteristics:

  • Requires table aliases
  • Commonly used for hierarchical data
  • Does not create a new table

SELF JOIN is useful for organizational hierarchies and recursive relationships.

7. What is a CROSS JOIN?

A CROSS JOIN returns the Cartesian product of two tables, meaning every row from the first table is combined with every row from the second table.

Example:

SELECT a.Name, b.Product
FROM Customers a
CROSS JOIN Products b;

Key characteristics:

  • No join condition
  • Result size = rows in table A × rows in table B
  • Can generate very large result sets

CROSS JOIN is used for combinations, permutations, and testing scenarios.

8. What is a subquery?

A subquery is a query nested inside another query. It can appear in SELECT, WHERE, FROM, or HAVING clauses.

Example:

SELECT Name
FROM Employees
WHERE Salary > (SELECT AVG(Salary) FROM Employees);

Key points:

  • Executes first or per row
  • Can return scalar, row, or table
  • Improves query readability

Subqueries help solve complex logic in a structured way.

9. What is a correlated subquery?

A correlated subquery is a subquery that depends on values from the outer query and is executed once for each row processed by the outer query.

Example:

SELECT e.Name
FROM Employees e
WHERE Salary > (
  SELECT AVG(Salary)
  FROM Employees
  WHERE DepartmentID = e.DepartmentID
);

Key characteristics:

  • References outer query columns
  • Slower than non-correlated subqueries
  • Often replaced by JOINs or CTEs for performance

Correlated subqueries are powerful but should be used carefully.

10. What is the difference between UNION and UNION ALL?

Both UNION and UNION ALL are used to combine result sets of multiple SELECT statements.

UNION

  • Removes duplicate rows
  • Performs sorting
  • Slower performance

UNION ALL

  • Includes duplicates
  • No sorting
  • Faster performance

Example:

SELECT City FROM Customers
UNION ALL
SELECT City FROM Suppliers;

Use UNION when duplicates must be removed, and UNION ALL when performance matters and duplicates are acceptable.

11. What are temporary tables?

Temporary tables are special tables used to store temporary data during a SQL Server session or procedure execution. They are mainly used for intermediate result storage, complex transformations, and breaking large queries into manageable steps.

Types of temporary tables:

  • Local temporary tables (#TempTable)
  • Global temporary tables (##TempTable)

Key characteristics:

  • Stored in the TempDB database
  • Automatically dropped when no longer needed
  • Support indexes, constraints, and statistics
  • Improve readability and performance of complex queries

Temporary tables are widely used in ETL, reporting, and data processing scenarios.

12. Difference between local and global temporary tables?

Local and global temporary tables differ mainly in scope and visibility.

Local Temporary Table (#Temp)

  • Visible only to the current session
  • Automatically dropped when session ends
  • Safe for concurrent users

Global Temporary Table (##Temp)

  • Visible to all sessions
  • Dropped only when last session using it ends
  • Requires careful usage to avoid conflicts

Example:

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

Local temp tables are preferred in most cases due to session isolation.

13. What are table variables?

Table variables are in-memory table-like structures declared using the DECLARE statement. They are primarily used for small datasets and short-lived operations.

Example:

DECLARE @Employees TABLE (
  ID INT,
  Name VARCHAR(50)
);

Key characteristics:

  • Scope limited to batch, function, or stored procedure
  • Stored in TempDB but treated like variables
  • Cannot use ALTER TABLE
  • Limited indexing options

Table variables are lightweight and easy to use for small result sets.

14. Table variable vs temporary table?

FeatureTable VariableTemporary TableDeclarationDECLARECREATE TABLEScopeBatch / procedureSessionIndexingLimitedFull indexingStatisticsNo (limited)YesPerformanceBetter for small dataBetter for large dataRollbackNot affectedTransaction-aware

Use table variables for small datasets and temporary tables for large, complex operations.

15. What is a Common Table Expression (CTE)?

A Common Table Expression (CTE) is a temporary named result set defined using the WITH keyword. It exists only for the duration of a single query.

Example:

WITH EmployeeCTE AS (
  SELECT ID, Name, Salary
  FROM Employees
)
SELECT * FROM EmployeeCTE;

Benefits:

  • Improves query readability
  • Supports recursive queries
  • Eliminates need for temp tables in simple cases

CTEs are ideal for hierarchical data and complex logic.

16. What are window functions?

Window functions perform calculations across a set of rows related to the current row without collapsing rows like aggregate functions.

Example:

SELECT Name, Salary,
AVG(Salary) OVER (PARTITION BY Department)
FROM Employees;

Key features:

  • Use OVER() clause
  • Preserve row-level detail
  • Ideal for analytics

Common window functions include:

  • ROW_NUMBER()
  • RANK()
  • DENSE_RANK()
  • SUM() OVER()

17. Explain ROW_NUMBER(), RANK(), and DENSE_RANK().

These are ranking window functions used to assign numbers to rows based on sorting.

  • ROW_NUMBER()
    • Assigns unique sequential numbers
    • No duplicates
  • RANK()
    • Same rank for ties
    • Skips next ranks
  • DENSE_RANK()
    • Same rank for ties
    • Does not skip ranks

Example:

SELECT Name, Salary,
ROW_NUMBER() OVER (ORDER BY Salary DESC) AS RowNum,
RANK() OVER (ORDER BY Salary DESC) AS RankNum,
DENSE_RANK() OVER (ORDER BY Salary DESC) AS DenseRankNum
FROM Employees;

These functions are widely used in reporting and pagination.

18. What is partitioning in window functions?

Partitioning divides data into logical groups within a window function using PARTITION BY.

Example:

SELECT Name, Department, Salary,
ROW_NUMBER() OVER (PARTITION BY Department ORDER BY Salary DESC)
FROM Employees;

Key points:

  • Acts like GROUP BY but keeps row-level data
  • Resets calculations for each partition
  • Useful for per-group analysis

Partitioning is essential for advanced analytical queries.

19. What is indexing strategy?

Indexing strategy is the planned approach to creating and managing indexes to balance performance, storage, and maintenance.

Key considerations:

  • Query patterns
  • Read vs write workload
  • Index selectivity
  • Maintenance overhead

Good indexing strategy:

  • Improves query performance
  • Avoids over-indexing
  • Supports critical queries

Indexing strategy is a core responsibility of DBAs and senior developers.

20. What is a composite index?

A composite index is an index created on multiple columns of a table.

Example:

CREATE INDEX idx_Emp_Dep_Sal
ON Employees (DepartmentID, Salary);

Key characteristics:

  • Order of columns matters
  • Supports queries using leading columns
  • Improves multi-column search performance

Composite indexes are ideal for frequently used multi-column filters.

21. What is index fragmentation?

Index fragmentation occurs when the logical order of index pages does not match the physical order in which the data is stored on disk. Over time, frequent INSERT, UPDATE, and DELETE operations cause pages to split and data to become scattered.

Types of fragmentation:

  • Internal fragmentation – wasted space within index pages
  • External fragmentation – pages stored out of logical order

Impact:

  • Slower query performance
  • Increased I/O operations
  • Inefficient use of storage

Fragmentation is commonly monitored using system views like sys.dm_db_index_physical_stats.

22. How do you rebuild and reorganize indexes?

Index maintenance is done using REBUILD and REORGANIZE operations.

Index REORGANIZE

  • Online operation
  • Defragments leaf-level pages
  • Recommended when fragmentation is between 5% and 30%
  • Lightweight and less resource-intensive

Index REBUILD

  • Drops and recreates index
  • Can be offline (or online in Enterprise Edition)
  • Recommended when fragmentation is above 30%
  • Updates statistics

Example:

ALTER INDEX ALL ON Employees REBUILD;
ALTER INDEX ALL ON Employees REORGANIZE;

Choosing the right method helps maintain optimal query performance.

23. What is an execution plan?

An execution plan is a roadmap created by the SQL Server query optimizer that shows how a query will be executed.

It includes:

  • Access methods (index seek/scan)
  • Join algorithms (nested loop, hash join)
  • Estimated cost of operations
  • Order of execution

Types:

  • Graphical execution plan
  • Text execution plan

Execution plans help identify performance bottlenecks and inefficient queries.

24. Difference between estimated and actual execution plan?

FeatureEstimated Execution PlanActual Execution PlanDataUses estimated statisticsUses real runtime dataExecutionQuery is not executedQuery is executedAccuracyApproximatePrecisePerformance impactNo impactMay affect performance

Estimated plans are useful for safe analysis, while actual plans provide real-world insights.

25. What is query optimization?

Query optimization is the process of improving query performance by helping SQL Server choose the most efficient execution plan.

Optimization techniques include:

  • Proper indexing
  • Avoiding SELECT *
  • Using appropriate joins
  • Updating statistics
  • Query rewriting

The SQL Server Query Optimizer automatically optimizes queries, but developers can guide it through better design and indexing.

26. What is parameter sniffing?

Parameter sniffing occurs when SQL Server creates an execution plan based on the first parameter values passed to a stored procedure and reuses it for subsequent executions.

Problem:

  • Works well for some parameters
  • Performs poorly for others

Solutions:

  • OPTION (RECOMPILE)
  • Optimize for hints
  • Local variables
  • Plan guides

Parameter sniffing is a common cause of intermittent performance issues.

27. What is implicit and explicit conversion?

Implicit conversion

  • Automatically performed by SQL Server
  • Happens when data types are compatible
  • Can cause performance issues if used on indexed columns

Explicit conversion

  • Developer specifies conversion
  • Uses CAST or CONVERT functions
  • More predictable and controlled

Example:

CAST(Salary AS VARCHAR)
CONVERT(INT, '123')

Explicit conversion is preferred for performance and clarity.

28. What is TRY…CATCH in SQL Server?

TRY…CATCH is a structured error handling mechanism in T-SQL that captures runtime errors.

Example:

BEGIN TRY
  INSERT INTO Employees VALUES (1, 'Aman');
END TRY
BEGIN CATCH
  PRINT ERROR_MESSAGE();
END CATCH;

Benefits:

  • Graceful error handling
  • Custom logging
  • Better debugging

TRY…CATCH improves robustness and reliability of SQL code.

29. What is error handling in T-SQL?

Error handling in T-SQL ensures that errors are detected, logged, and managed without crashing applications.

Techniques include:

  • TRY…CATCH blocks
  • @@ERROR
  • THROW
  • RAISERROR
  • Transaction rollback

Effective error handling:

  • Prevents data corruption
  • Improves user experience
  • Simplifies troubleshooting

It is essential in production-grade SQL code.

30. What is a cursor?

A cursor is a database object that allows row-by-row processing of query results.

Example:

DECLARE emp_cursor CURSOR FOR
SELECT Name FROM Employees;

Key characteristics:

  • Processes one row at a time
  • Slower than set-based operations
  • Uses more resources

Cursors should be avoided when possible and replaced with set-based queries, but they are useful in complex row-level logic scenarios.

31. Why cursors should be avoided?

Cursors should generally be avoided because they process data row by row, whereas SQL Server is optimized for set-based operations.

Disadvantages of cursors:

  • Poor performance on large datasets
  • High memory and CPU usage
  • Increased locking and blocking
  • Complex code that is hard to maintain

Cursors are slower because:

  • Each row is fetched, processed, and closed individually
  • Context switching occurs between rows

Best practice:

  • Replace cursors with set-based queries, joins, or window functions

Cursors should only be used when row-by-row logic is unavoidable.

32. What is dynamic SQL?

Dynamic SQL is SQL code that is constructed and executed at runtime instead of being statically defined.

Example:

DECLARE @sql NVARCHAR(MAX);
SET @sql = 'SELECT * FROM Employees WHERE DepartmentID = 1';
EXEC(@sql);

Use cases:

  • Dynamic table or column names
  • Flexible filtering logic
  • Dynamic reports

Risks:

  • SQL injection
  • Harder debugging
  • Performance issues

Dynamic SQL should be used carefully and securely.

33. What is sp_executesql?

sp_executesql is a system stored procedure used to execute dynamic SQL with parameters.

Example:

DECLARE @sql NVARCHAR(MAX);
SET @sql = 'SELECT * FROM Employees WHERE DepartmentID = @DeptId';

EXEC sp_executesql 
  @sql,
  N'@DeptId INT',
  @DeptId = 2;

Advantages over EXEC:

  • Supports parameterization
  • Reduces SQL injection risk
  • Enables execution plan reuse
  • Better performance

sp_executesql is the recommended approach for executing dynamic SQL.

34. What is SET NOCOUNT ON?

SET NOCOUNT ON disables the message that reports the number of rows affected by a T-SQL statement.

Example:

SET NOCOUNT ON;

Benefits:

  • Reduces network traffic
  • Improves performance
  • Prevents issues in client applications

It is commonly used in:

  • Stored procedures
  • Triggers
  • Batch scripts

Using SET NOCOUNT ON is a best practice in production code.

35. What is the use of EXISTS?

The EXISTS operator checks for the existence of rows returned by a subquery.

Example:

SELECT Name
FROM Employees e
WHERE EXISTS (
  SELECT 1 FROM Orders o WHERE o.EmployeeID = e.ID
);

Key points:

  • Returns TRUE or FALSE
  • Stops processing once a match is found
  • Efficient for large datasets

EXISTS is often preferred for existence checks.

36. EXISTS vs IN?

FeatureEXISTSINEvaluationStops at first matchEvaluates full listPerformanceBetter for large subqueriesSlower for large listsNULL handlingHandles NULL safelyCan behave unexpectedlyUse caseCorrelated subqueriesSmall static lists

Use EXISTS for large or correlated datasets and IN for small, fixed lists.

37. What is a trigger?

A trigger is a special type of stored procedure that automatically executes in response to DML or DDL events on a table or database.

Common trigger events:

  • INSERT
  • UPDATE
  • DELETE

Triggers are used to:

  • Enforce business rules
  • Maintain audit logs
  • Validate data
  • Synchronize tables

Triggers operate implicitly, without user invocation.

38. Types of triggers in SQL Server?

SQL Server supports several types of triggers:

  1. DML Triggers
    • AFTER triggers
    • INSTEAD OF triggers
  2. DDL Triggers
    • Respond to CREATE, ALTER, DROP events
  3. LOGON Triggers
    • Fire on user login events

Triggers should be used carefully due to performance and complexity concerns.

39. What is an AFTER trigger?

An AFTER trigger executes after the triggering DML operation has completed successfully.

Example:

CREATE TRIGGER trg_AfterInsert
ON Employees
AFTER INSERT
AS
BEGIN
  INSERT INTO AuditLog VALUES ('Insert occurred');
END;

Key characteristics:

  • Fires after INSERT, UPDATE, DELETE
  • Can roll back transactions
  • Most commonly used trigger type

AFTER triggers are ideal for auditing and validation.

40. What is an INSTEAD OF trigger?

An INSTEAD OF trigger executes in place of the triggering operation, overriding the default behavior.

Example:

CREATE TRIGGER trg_InsteadOfDelete
ON Employees
INSTEAD OF DELETE
AS
BEGIN
  UPDATE Employees SET IsActive = 0 WHERE ID IN (SELECT ID FROM deleted);
END;

Use cases:

  • Implementing soft deletes
  • Validating complex business logic
  • Handling views with multiple tables

INSTEAD OF triggers provide full control over DML behavior.

Experienced (Q&A)

1. Explain SQL Server architecture in detail.

SQL Server architecture is a layered architecture designed to efficiently process queries, manage memory, ensure concurrency, and guarantee data durability.

At a high level, SQL Server consists of two major layers:

1. Relational Engine (Query Processor)

This layer is responsible for query parsing, optimization, and execution.

Key components:

  • Parser – Validates T-SQL syntax and semantics
  • Algebrizer – Resolves object names, permissions, and data types
  • Query Optimizer – Generates multiple execution plans and chooses the most cost-effective one
  • Execution Engine – Executes the chosen plan and retrieves data

2. Storage Engine

This layer manages data storage, retrieval, transactions, and recovery.

Key components:

  • Buffer Manager – Manages data pages in memory
  • Transaction Manager – Handles ACID compliance
  • Lock Manager – Manages concurrency and locking
  • Log Manager – Ensures durability using transaction logs
  • Access Methods – Reads and writes data pages

Together, these layers ensure performance, reliability, scalability, and data integrity.

2. What are system databases and their roles?

System databases are core databases required for SQL Server to function.

master

  • Stores server-level metadata
  • Logins, endpoints, linked servers
  • Configuration settings
  • Required for SQL Server startup

model

  • Template for new databases
  • Objects added here appear in newly created databases

msdb

  • Used by SQL Server Agent
  • Stores job schedules, alerts, backup history, SSIS packages

tempdb

  • Stores temporary objects
  • Used for sorting, hashing, versioning
  • Recreated on every SQL Server restart

Each system database plays a critical operational role, and corruption can impact the entire instance.

3. Explain TempDB internals.

TempDB is a shared system database used by all sessions and workloads.

Internally, TempDB stores:

  • Temporary tables and table variables
  • Intermediate query results (hash joins, sorts)
  • Row versioning data (snapshot isolation)
  • Cursors and internal objects

Key internal behaviors:

  • Uses allocation maps (PFS, GAM, SGAM)
  • Heavy contention can occur on metadata pages
  • Uses mixed extents for small objects

Best practices:

  • Multiple TempDB data files
  • Uniform file sizes
  • Fast disk subsystem

TempDB performance is often a bottleneck in high-concurrency systems.

4. How does SQL Server store data internally?

SQL Server stores data in data files (.mdf, .ndf) and log files (.ldf).

Storage hierarchy:

  • Database
  • Filegroup
  • Data files
  • Extents
  • Pages
  • Rows

Data is stored in 8 KB pages, and pages are grouped into extents. SQL Server reads and writes data only in page units, not rows.

Internally:

  • Fixed-length data stored inline
  • Variable-length data stored with offsets
  • Large objects (LOB) stored off-row when needed

This design enables efficient I/O and memory management.

5. What are pages and extents?

Pages

  • Smallest unit of data storage
  • Size: 8 KB
  • Types include:
    • Data pages
    • Index pages
    • IAM pages
    • PFS, GAM, SGAM pages

Extents

  • Group of 8 contiguous pages
  • Total size: 64 KB

Types of extents:

  • Uniform extents – owned by one object
  • Mixed extents – shared by multiple objects

Pages and extents form the foundation of SQL Server’s storage engine.

6. Explain heap vs clustered table internals.

Heap

  • Table without a clustered index
  • Rows stored unordered
  • Uses RID (Row Identifier) to locate rows
  • Inserts are fast
  • Reads are slower due to scans
  • Susceptible to forwarded records

Clustered Table

  • Data stored physically ordered by clustered index key
  • Leaf level = actual data
  • Faster range queries
  • Slightly slower inserts due to page splits

Internally, clustered tables provide better read performance, while heaps are sometimes used for staging tables.

7. What is latch vs lock?

Although often confused, latches and locks serve different purposes.

Locks

  • Logical concurrency mechanism
  • Protect data consistency
  • Managed by lock manager
  • Visible to users
  • Affected by isolation levels

Latches

  • Physical memory synchronization
  • Protect internal structures
  • Very short-lived
  • Not user-controlled

In short:

  • Locks protect data
  • Latches protect memory structures

8. Types of locks in SQL Server?

SQL Server supports multiple lock types to control concurrency:

Common lock modes

  • Shared (S) – Read operations
  • Exclusive (X) – Write operations
  • Update (U) – Prevents deadlocks
  • Intent locks (IS, IX, SIX) – Hierarchical locking
  • Schema locks (Sch-S, Sch-M) – Schema stability/modification

Lock granularity

  • Row
  • Page
  • Key
  • Table
  • Database

Locks are dynamically escalated to balance performance and concurrency.

9. What is deadlock and how do you resolve it?

A deadlock occurs when two or more sessions block each other indefinitely, each holding resources the others need.

Example:

  • Session A locks Table 1, waits for Table 2
  • Session B locks Table 2, waits for Table 1

SQL Server automatically:

  • Detects deadlock
  • Chooses a victim
  • Rolls back victim transaction

Resolution strategies:

  • Access objects in consistent order
  • Keep transactions short
  • Use proper indexing
  • Reduce lock escalation
  • Enable snapshot isolation

Deadlocks indicate design or concurrency issues.

10. What is isolation level?

Isolation level defines how transaction visibility and concurrency are handled.

It controls:

  • Dirty reads
  • Non-repeatable reads
  • Phantom reads

Common isolation levels:

  • READ UNCOMMITTED
  • READ COMMITTED (default)
  • REPEATABLE READ
  • SERIALIZABLE
  • SNAPSHOT

Each level balances data accuracy vs performance. Choosing the right isolation level is critical for high-concurrency systems.

11. Explain all transaction isolation levels.

Transaction isolation levels control how and when changes made by one transaction become visible to other transactions. They balance data consistency vs concurrency.

SQL Server supports the following isolation levels:

  1. READ UNCOMMITTED
    • Allows dirty reads
    • No shared locks are taken
    • Fastest but least reliable
    • Used mainly for reporting where accuracy is not critical
  2. READ COMMITTED (Default)
    • Prevents dirty reads
    • Uses shared locks during read
    • Allows non-repeatable reads and phantom reads
    • Good balance for most OLTP workloads
  3. REPEATABLE READ
    • Prevents dirty and non-repeatable reads
    • Shared locks held until transaction completes
    • Phantom reads still possible
    • Can cause blocking
  4. SERIALIZABLE
    • Highest isolation level
    • Prevents dirty, non-repeatable, and phantom reads
    • Locks entire key ranges
    • Lowest concurrency, highest consistency
  5. SNAPSHOT
    • Uses row versioning instead of locks
    • Reads a consistent snapshot of data
    • No blocking between readers and writers

Choosing the correct isolation level is critical for system scalability and correctness.

12. What is snapshot isolation?

Snapshot isolation is an isolation level that provides transaction-level consistency using row versioning instead of locking.

How it works:

  • Each transaction reads data as it existed at the start of the transaction
  • Uses versions stored in TempDB
  • Writers do not block readers and vice versa

Key characteristics:

  • Prevents dirty reads, non-repeatable reads, and phantom reads
  • Reduces blocking and deadlocks
  • Requires ALLOW_SNAPSHOT_ISOLATION to be enabled

Limitations:

  • Higher TempDB usage
  • Possible update conflicts

Snapshot isolation is ideal for high-concurrency read-heavy systems.

13. What is row versioning?

Row versioning is a concurrency control mechanism where SQL Server stores previous versions of modified rows in TempDB.

Used by:

  • Snapshot Isolation
  • Read Committed Snapshot (RCSI)
  • Online index operations

How it works:

  • When a row is updated, the old version is copied to TempDB
  • Readers access the version instead of blocking writers

Benefits:

  • Reduces blocking
  • Improves concurrency
  • Enables consistent reads

Trade-offs:

  • Increased TempDB usage
  • Additional I/O overhead

Row versioning is a cornerstone of modern SQL Server concurrency management.

14. What is blocking and how do you troubleshoot it?

Blocking occurs when one session holds a lock that another session needs, causing the second session to wait.

Common causes:

  • Long-running transactions
  • Poor indexing
  • Large batch updates
  • High isolation levels

Troubleshooting steps:

  1. Identify blocking sessions using DMVs (sys.dm_exec_requests)
  2. Find blocking chains
  3. Analyze queries and indexes
  4. Reduce transaction scope
  5. Enable snapshot isolation where appropriate

Blocking is expected in transactional systems, but excessive blocking indicates design issues.

15. Explain wait stats and wait types.

Wait stats represent the time SQL Server spends waiting for resources rather than doing useful work.

Categories of wait types:

  • CPU waits (SOS_SCHEDULER_YIELD)
  • I/O waits (PAGEIOLATCH_*)
  • Lock waits (LCK_*)
  • Memory waits (RESOURCE_SEMAPHORE)
  • Network waits (ASYNC_NETWORK_IO)

Why wait stats matter:

  • Identify bottlenecks
  • Guide performance tuning
  • Separate symptoms from root causes

Wait stats are one of the most reliable performance diagnostic tools in SQL Server.

16. How do you identify performance bottlenecks?

Performance bottlenecks are identified using a systematic approach, not guesswork.

Key tools and techniques:

  • Wait stats analysis
  • Execution plan analysis
  • Query Store insights
  • Index usage statistics
  • CPU, memory, and I/O metrics
  • Blocking and deadlock reports

Approach:

  1. Determine if bottleneck is CPU, memory, disk, or locking
  2. Identify top resource-consuming queries
  3. Tune queries or indexes
  4. Validate improvements

Effective troubleshooting focuses on root cause, not symptoms.

17. What is Query Store?

Query Store is a built-in SQL Server feature that captures query execution history, plans, and runtime statistics over time.

It stores:

  • Query text
  • Execution plans
  • Runtime metrics
  • Regressions and improvements

Benefits:

  • Persistent performance history
  • Detects plan changes
  • Survives restarts

Query Store is essential for modern performance monitoring and tuning.

18. How does Query Store help in performance tuning?

Query Store helps by providing historical visibility into query performance.

Key advantages:

  • Identify regressed queries
  • Compare execution plans
  • Force a stable plan
  • Monitor workload trends

Use cases:

  • Performance regression after deployment
  • Parameter sniffing issues
  • Plan instability

Query Store transforms tuning from reactive troubleshooting to proactive optimization.

19. What is plan cache?

Plan cache stores compiled execution plans so SQL Server can reuse them without recompiling queries.

Benefits:

  • Reduces CPU overhead
  • Improves response time
  • Enables scalability

Plans are cached for:

  • Ad-hoc queries
  • Stored procedures
  • Prepared statements

Plan reuse is critical for high-throughput systems.

20. What causes plan cache bloat?

Plan cache bloat occurs when the cache is filled with too many single-use or inefficient plans.

Common causes:

  • Ad-hoc queries with literals
  • Excessive dynamic SQL without parameters
  • Frequent recompilations
  • Lack of plan reuse

Consequences:

  • Memory pressure
  • Reduced cache efficiency
  • Increased CPU usage

Mitigation strategies:

  • Use parameterized queries
  • Enable Optimize for Ad Hoc Workloads
  • Use sp_executesql
  • Monitor cache usage regularly

Plan cache health is crucial for predictable SQL Server performance.

21. What is recompilation and why does it happen?

Recompilation occurs when SQL Server discards an existing execution plan and generates a new plan for a query or stored procedure. While compilation is expensive, recompilation is sometimes necessary to ensure optimal performance.

Common reasons for recompilation:

  • Schema changes (ALTER TABLE, CREATE/DROP INDEX)
  • Statistics updates
  • Significant data changes
  • Use of temporary tables
  • OPTION (RECOMPILE)
  • SET option changes
  • Parameter sniffing mitigation techniques

Impact:

  • Increased CPU usage
  • Possible performance instability

Recompilation is a trade-off between plan accuracy and CPU cost. It should be controlled, not eliminated entirely.

22. Explain index seek vs index scan.

An index seek occurs when SQL Server can efficiently navigate an index to retrieve specific rows.

  • Uses B-tree traversal
  • Highly selective
  • Very fast

An index scan occurs when SQL Server reads all or most of the index.

  • Used when filters are not selective
  • Higher I/O cost
  • Still better than table scan in many cases

Index seeks are preferred, but index scans are not always bad—especially for analytical queries.

23. What is a covering index?

A covering index is a non-clustered index that includes all columns needed by a query, allowing SQL Server to satisfy the query without accessing the base table.

Example:

CREATE NONCLUSTERED INDEX idx_cover
ON Orders (CustomerID)
INCLUDE (OrderDate, Amount);

Benefits:

  • Eliminates key lookups
  • Reduces I/O
  • Improves query performance

Covering indexes are especially valuable in high-frequency OLTP queries.

24. How do you design indexes for OLTP systems?

OLTP systems focus on high-volume, short transactions.

Index design principles:

  • Index frequently used WHERE and JOIN columns
  • Keep indexes narrow
  • Avoid over-indexing
  • Use covering indexes selectively
  • Monitor write overhead

Focus:

  • Fast point lookups
  • Low latency
  • Minimal locking

Index design for OLTP is about balance between read speed and write cost.

25. How do you design indexes for OLAP systems?

OLAP systems focus on large-scale reporting and analysis.

Index design principles:

  • Use fewer but broader indexes
  • Favor index scans over seeks
  • Use columnstore indexes
  • Partition large tables
  • Use filtered indexes where applicable

Focus:

  • Large aggregations
  • Sequential reads
  • Query throughput over latency

OLAP indexing prioritizes analytical efficiency over transactional speed.

26. What is a partitioned table?

A partitioned table divides a large table into smaller, manageable partitions based on a partition key, while still appearing as a single logical table.

Benefits:

  • Improved manageability
  • Faster maintenance
  • Query performance improvements for partition-aligned queries

Partitioning is commonly used for very large tables.

27. How does table partitioning work internally?

Internally, table partitioning uses:

  • Partition Function – defines value ranges
  • Partition Scheme – maps partitions to filegroups
  • Partitioned Indexes – align data with partitions

Execution behavior:

  • SQL Server performs partition elimination
  • Only relevant partitions are scanned
  • Improves query performance and maintenance efficiency

Partitioning does not automatically improve performance—it must be used strategically.

28. What is SQL Server Agent?

SQL Server Agent is a job scheduling and automation service.

Used for:

  • Scheduled backups
  • Index maintenance
  • ETL jobs
  • Alerts and notifications

Components:

  • Jobs
  • Schedules
  • Alerts
  • Operators

SQL Server Agent is essential for automated database operations in production environments.

29. Explain SQL Server backup types.

SQL Server supports several backup types to protect data:

  • Full backup – Complete database
  • Differential backup – Changes since last full backup
  • Transaction log backup – Changes since last log backup
  • File/Filegroup backup – Selected files
  • Copy-only backup – Does not affect backup chain

A proper backup strategy balances recovery time and storage usage.

30. Difference between full, differential, and log backup?

Backup TypeWhat it ContainsRecovery UsageFullEntire databaseBaseline restoreDifferentialChanges since last fullFaster restoreLogAll transactions since last log backupPoint-in-time recovery

Restore sequence:

  1. Full backup
  2. Latest differential (optional)
  3. All log backups

A well-designed backup strategy ensures minimal data loss and fast recovery.

31. What is point-in-time recovery?

Point-in-time recovery is a data recovery technique that allows a database to be restored to a specific moment in time, typically just before a failure, data corruption, or accidental data modification occurred.

How it works:

  • Requires FULL recovery model
  • Uses a sequence of full backup → differential (optional) → transaction log backups
  • Transaction logs capture every change with timestamps (LSNs)

Use cases:

  • Accidental DELETE or UPDATE
  • Application bugs
  • Data corruption

Example workflow:

  1. Restore full backup with NORECOVERY
  2. Restore latest differential (optional)
  3. Restore log backups up to desired time
  4. Recover database

Point-in-time recovery minimizes data loss (RPO) and is critical for mission-critical systems.

32. What is high availability vs disaster recovery?

High Availability (HA) and Disaster Recovery (DR) are related but serve different purposes.

High Availability

  • Focus: Minimize downtime
  • Handles local failures (server crash, hardware failure)
  • Fast failover
  • Examples: Always On AGs, Failover Clustering

Disaster Recovery

  • Focus: Recover from catastrophic events
  • Handles site-level failures (data center outage)
  • Usually longer recovery time
  • Examples: Log shipping, replication, geo-replication

Key difference:

  • HA = stay online
  • DR = recover after disaster

A complete strategy often includes both HA and DR.

33. Explain Always On Availability Groups.

Always On Availability Groups (AGs) provide high availability and disaster recovery at the database level.

Key concepts:

  • Primary replica (read-write)
  • Secondary replicas (read-only or failover)
  • Synchronous and asynchronous commit modes
  • Automatic or manual failover

Features:

  • No shared storage required
  • Supports multiple databases
  • Read-only replicas for reporting
  • Integrated health detection

AGs are widely used for enterprise-grade availability and scalability.

34. What is database mirroring?

Database mirroring is a deprecated high availability feature that maintains a hot standby copy of a database.

Components:

  • Principal server
  • Mirror server
  • Witness (optional)

Modes:

  • High safety (synchronous)
  • High performance (asynchronous)

Limitations:

  • Database-level only
  • No read access on mirror
  • Deprecated in favor of Always On AGs

Mirroring is still seen in legacy systems.

35. What is replication and its types?

Replication is a data distribution technology that copies data from one database to another.

Main types:

  1. Snapshot Replication
    • Copies entire dataset periodically
    • Simple but heavy
  2. Transactional Replication
    • Near real-time data replication
    • High throughput
    • Minimal latency
  3. Merge Replication
    • Two-way replication
    • Conflict resolution
    • Suitable for disconnected systems

Replication is used for data distribution, reporting, and scale-out scenarios.

36. What is log shipping?

Log shipping is a disaster recovery solution that involves automatically backing up transaction logs from a primary database and restoring them on a secondary server.

Components:

  • Primary server
  • Secondary server
  • Monitor server (optional)

Process:

  • Take log backups
  • Copy logs to secondary
  • Restore logs in sequence

Characteristics:

  • Manual failover
  • Read-only access on secondary (optional)
  • Simple and reliable

Log shipping is ideal for cost-effective DR solutions.

37. What is security architecture in SQL Server?

SQL Server security follows a layered architecture:

  1. Authentication
    • Windows authentication
    • SQL authentication
  2. Authorization
    • Logins
    • Users
    • Roles
    • Permissions
  3. Encryption
    • TDE
    • Column-level encryption
    • Always Encrypted
  4. Auditing
    • SQL Audit
    • Extended Events

Security is enforced at server, database, and object levels.

38. Explain roles, logins, and users.

  • Login
    • Server-level identity
    • Allows connection to SQL Server
  • User
    • Database-level identity
    • Maps to a login
  • Roles
    • Collection of permissions
    • Simplifies security management

Types of roles:

  • Fixed server roles
  • Fixed database roles
  • User-defined roles

This separation ensures least-privilege access and scalability.

39. How do you secure sensitive data in SQL Server?

Sensitive data is protected using multiple techniques:

  • Always Encrypted
  • Transparent Data Encryption (TDE)
  • Column-level encryption
  • Dynamic Data Masking
  • Row-Level Security
  • Auditing and monitoring
  • Strong authentication and role management

Security should be implemented defense-in-depth, not relying on a single feature.

40. How do you troubleshoot slow-running queries in production?

Production troubleshooting requires a methodical, low-impact approach.

Steps:

  1. Identify slow queries (Query Store, DMVs)
  2. Analyze execution plans
  3. Check waits and blocking
  4. Review indexes and statistics
  5. Validate parameter sniffing issues
  6. Test fixes in non-production
  7. Monitor after deployment

Key principles:

  • Avoid guesswork
  • Use real metrics
  • Fix root causes, not symptoms

Effective troubleshooting is a core skill of senior SQL Server professionals.

WeCP Team
Team @WeCP
WeCP is a leading talent assessment platform that helps companies streamline their recruitment and L&D process by evaluating candidates' skills through tailored assessments