15 MS SQL Interview Questions and Answers
Prepare for your interview with our comprehensive guide on MS SQL, featuring common questions and detailed answers to boost your confidence.
Prepare for your interview with our comprehensive guide on MS SQL, featuring common questions and detailed answers to boost your confidence.
MS SQL, or Microsoft SQL Server, is a robust relational database management system widely used for storing and retrieving data as requested by other software applications. Known for its scalability, security features, and integration capabilities, MS SQL is a critical tool for managing large volumes of data efficiently. Its comprehensive suite of tools and services makes it a preferred choice for businesses of all sizes, from small startups to large enterprises.
This article aims to prepare you for your upcoming interview by providing a curated list of MS SQL questions and answers. By familiarizing yourself with these questions, you will gain a deeper understanding of key concepts and practical applications, enhancing your ability to demonstrate your expertise and problem-solving skills during the interview process.
A primary key uniquely identifies a record in a database table, ensuring no duplicate records exist. It can be a single column or a combination of columns (composite key) and is essential for data integrity and establishing relationships between tables.
To define a primary key in a table, use the following SQL syntax:
CREATE TABLE Employees ( EmployeeID INT PRIMARY KEY, FirstName NVARCHAR(50), LastName NVARCHAR(50), Department NVARCHAR(50) );
In this example, EmployeeID
is the primary key for the Employees
table, ensuring uniqueness and non-nullability.
You can also define a composite primary key:
CREATE TABLE Orders ( OrderID INT, ProductID INT, Quantity INT, PRIMARY KEY (OrderID, ProductID) );
Here, the combination of OrderID
and ProductID
serves as the primary key for the Orders
table.
To retrieve the top 5 highest salaries from an Employee table, use the ORDER BY
clause with the TOP
keyword. This sorts salaries in descending order and limits the result to the top 5 entries.
Example:
SELECT TOP 5 Salary FROM Employee ORDER BY Salary DESC;
This query selects the top 5 salaries by ordering them in descending order.
An INNER JOIN returns rows with matching values in both tables, useful for retrieving records with corresponding entries in both tables.
A LEFT JOIN returns all rows from the left table and matched rows from the right table. If no match exists, the result is NULL on the right side. This is useful for retrieving all records from the left table, regardless of matches in the right table.
Example of INNER JOIN:
SELECT employees.name, departments.department_name FROM employees INNER JOIN departments ON employees.department_id = departments.id;
Example of LEFT JOIN:
SELECT employees.name, departments.department_name FROM employees LEFT JOIN departments ON employees.department_id = departments.id;
Indexes are data structures that speed up row retrieval. They work like a book index, allowing quick data location without scanning the entire table. Types include clustered and non-clustered indexes.
Indexes improve query performance by reducing the data scanned. The database engine uses the index to quickly locate rows matching query conditions.
Normalization structures a database to reduce redundancy and improve data integrity. It divides large tables into smaller ones and defines relationships between them, guided by normal forms.
Common normal forms include:
Normalization enhances data integrity, efficiency, consistency, and scalability.
Optimizing a slow-running query involves several strategies:
Pivoting data transforms it from rows to columns, often for reporting. In MS SQL, the PIVOT operator achieves this transformation.
Example:
Suppose we have a table named Sales:
CREATE TABLE Sales ( Product VARCHAR(50), Year INT, Amount INT ); INSERT INTO Sales (Product, Year, Amount) VALUES ('ProductA', 2020, 100), ('ProductA', 2021, 150), ('ProductB', 2020, 200), ('ProductB', 2021, 250);
To pivot this data:
SELECT Product, [2020] AS Year2020, [2021] AS Year2021 FROM ( SELECT Product, Year, Amount FROM Sales ) AS SourceTable PIVOT ( SUM(Amount) FOR Year IN ([2020], [2021]) ) AS PivotTable;
This query transforms the data into:
Product | Year2020 | Year2021 |
---|---|---|
ProductA | 100 | 150 |
ProductB | 200 | 250 |
Common Table Expressions (CTEs) are temporary result sets referenced within a SELECT, INSERT, UPDATE, or DELETE statement. They simplify complex queries, improve readability, and manage recursive queries.
Example:
WITH Sales_CTE (SalesPerson, TotalSales) AS ( SELECT SalesPerson, SUM(SalesAmount) FROM Sales GROUP BY SalesPerson ) SELECT SalesPerson, TotalSales FROM Sales_CTE WHERE TotalSales > 10000;
In this example, the CTE calculates total sales for each salesperson, and the main query selects those with total sales over 10,000.
ACID properties ensure database transactions are processed reliably:
1. Atomicity: Ensures all operations within a transaction are completed successfully. If any operation fails, the entire transaction is rolled back.
2. Consistency: Ensures a transaction brings the database from one valid state to another, maintaining data validity.
3. Isolation: Ensures transactions are executed in isolation, preventing interference and data anomalies.
4. Durability: Ensures committed transactions remain so, even in the event of a system failure.
A recursive query in MS SQL is implemented using a Common Table Expression (CTE). Recursive CTEs are useful for querying hierarchical data, such as organizational charts.
A recursive CTE consists of:
Example:
WITH EmployeeHierarchy AS ( SELECT EmployeeID, ManagerID, EmployeeName FROM Employees WHERE ManagerID IS NULL UNION ALL SELECT e.EmployeeID, e.ManagerID, e.EmployeeName FROM Employees e INNER JOIN EmployeeHierarchy eh ON e.ManagerID = eh.EmployeeID ) SELECT * FROM EmployeeHierarchy;
This example retrieves all levels of an employee hierarchy.
A clustered index sorts and stores data rows based on key values, determining the physical order of data. Each table can have only one clustered index.
A non-clustered index creates a separate structure with pointers to data rows, without altering their physical order. A table can have multiple non-clustered indexes.
Key differences:
Concurrency issues in SQL Server arise when multiple transactions execute simultaneously, potentially leading to conflicts. SQL Server provides mechanisms to handle these issues:
Transaction isolation levels in SQL Server determine how transaction integrity is maintained and data is accessed concurrently:
1. Read Uncommitted: Allows reading uncommitted data, resulting in dirty reads.
2. Read Committed: Ensures reading only committed data, preventing dirty reads but allowing non-repeatable reads.
3. Repeatable Read: Prevents dirty and non-repeatable reads but allows phantom reads.
4. Serializable: Ensures complete isolation, preventing dirty, non-repeatable, and phantom reads, but can impact performance.
Performance tuning in SQL Server involves strategies to ensure efficient database operation:
SQL Server provides several backup and recovery options to ensure data integrity and availability:
These options provide a comprehensive recovery strategy, enabling data restoration to a specific point in time and minimizing data loss and downtime.