SQL Server Interview Questions and Answers
Basic to Moderate SQL Server Interview Questions
Q: What is SQL Server?
A: SQL Server is a relational database management system (RDBMS) developed by Microsoft.
Q: What is a database?
A: A structured set of data stored and accessed electronically.
Q: What is a table?
A: A collection of rows and columns used to store data.
Q: What is a primary key?
A: A column or set of columns that uniquely identifies each row in a table.
Q: What is a foreign key?
A: A key used to link two tables together by referencing a primary key in another table.
Q: What is a constraint?
A: Rules enforced on data columns to ensure data integrity.
Q: What is normalization?
A: Process of organizing data to reduce redundancy and improve data integrity.
Q: What is denormalization?
A: Combining tables to reduce complexity and increase read performance.
Q: What is an index?
A: An object that improves the speed of data retrieval from a table.
Q: What is a view?
A: A virtual table based on a SQL query.
Q: What are joins?
A: Joins combine rows from two or more tables based on related columns.
Q: What is an inner join?
A: Returns rows when there is a match in both tables.
Q: What is a left join?
A: Returns all rows from the left table, and matched rows from the right table.
SQL Server Interview Questions and Answers
Q: What is a right join?
A: Returns all rows from the right table, and matched rows from the left table.
Q: What is a full outer join?
A: Returns rows when there is a match in either left or right table.
Q: What is a cross join?
A: Returns the Cartesian product of the two tables.
Q: What is a self join?
A: A self join is a regular join but the table is joined with itself.
Q: What is a stored procedure?
A: A precompiled collection of SQL statements that can be reused.
Q: What is a function in SQL Server?
A: A stored program that returns a single value or table.
Q: What is a trigger?
A: A special type of stored procedure that automatically executes when an event occurs.
Q: What is the difference between `DELETE` and `TRUNCATE`?
A: `DELETE` removes rows with logging and conditions; `TRUNCATE` removes all rows quickly without logging.
Q: What is a transaction?
A: A sequence of operations performed as a single logical unit of work.
Q: What is ACID in SQL Server?
A: Atomicity, Consistency, Isolation, Durability - properties of a transaction.
Q: What are DDL commands?
A: `CREATE`, `ALTER`, `DROP`, etc. that define database schema.
Q: What are DML commands?
A: `SELECT`, `INSERT`, `UPDATE`, `DELETE` used to manipulate data.
Q: What are DCL commands?
A: `GRANT`, `REVOKE` - used for access control.
Q: What is the use of `GROUP BY`?
A: Groups rows that have the same values into summary rows.
SQL Server Interview Questions and Answers
Q: What is the use of `ORDER BY`?
A: Sorts the result set in ascending or descending order.
Q: What is a subquery?
A: A query nested inside another query.
Q: What is a correlated subquery?
A: A subquery that references a column from the outer query.
Q: What are temporary tables?
A: Tables created using `#` that exist temporarily during the session.
Q: What is a table variable?
A: A variable that holds table data and exists for the batch duration.
Q: Difference between temp table and table variable?
A: Temp tables support indexing/stats; table variables are faster for small sets.
Q: What is a schema?
A: A logical container for database objects like tables and views.
Q: What is a synonym?
A: An alias or alternate name for a database object.
Q: What is a CTE?
A: A Common Table Expression is a temporary result set used in a SELECT, INSERT, etc.
Q: What is the difference between `ISNULL` and `COALESCE`?
A: `ISNULL` returns a specified value for NULL, `COALESCE` returns the first non-NULL among arguments.
Q: What is the difference between `HAVING` and `WHERE`?
A: `WHERE` filters rows before grouping; `HAVING` filters groups after aggregation.
Q: What are aggregate functions?
A: `SUM`, `AVG`, `MAX`, `MIN`, `COUNT` - used to calculate values from sets of rows.
Q: What is indexing?
A: Technique to optimize performance of queries by minimizing disk I/O.
Q: What are types of indexes?
A: Clustered, Non-clustered, Unique, Full-text, XML Indexes.
SQL Server Interview Questions and Answers
Q: What is a clustered index?
A: Sorts and stores table data rows in the order of the key.
Q: What is a non-clustered index?
A: A structure separate from the data rows that contains pointers to the data.
Q: What is the maximum number of clustered indexes per table?
A: One clustered index per table.
Q: What is the use of `SET NOCOUNT ON`?
A: Prevents the message indicating the number of rows affected from being returned.
Q: What is SQL Server Management Studio (SSMS)?
A: A tool for configuring, managing, and administering all components within SQL Server.
Q: How do you retrieve the current date and time?
A: Use `GETDATE()` function.
Q: How to find the number of rows in a table?
A: Use `SELECT COUNT(*) FROM TableName`.
Q: What is a unique key?
A: Ensures all values in a column are different and allows one NULL.
Q: What is a composite key?
A: A primary key consisting of two or more columns.