Developer Interview Questions
Developer Interview Questions
SQL Server supports several types of joins including INNER JOIN, LEFT
JOIN (or LEFT OUTER JOIN), RIGHT JOIN (or RIGHT OUTER JOIN), and FULL
JOIN.
2. Explain the differences between CHAR, VARCHAR, and NVARCHAR data
types in SQL Server.
CHAR is a fixed-length character data type, while VARCHAR is a variable-
length character data type. NVARCHAR is used to store Unicode character
data.
CHAR and VARCHAR are limited to non-Unicode characters, whereas
NVARCHAR can store both Unicode and non-Unicode characters.
3. What is a clustered index and how does it differ from a non-clustered index?
A clustered index determines the physical order of rows in a table based
on the indexed column(s). Each table can have only one clustered index.
A non-clustered index is a separate structure from the table and contains a
sorted list of references to the table's rows. Multiple non-clustered indexes
can exist on a table.
4. Explain the purpose of the GROUP BY clause in SQL Server.
The GROUP BY clause is used to group rows that have the same values
into summary rows, typically to perform aggregate functions (e.g., SUM,
COUNT, AVG) on each group.
5. What is the purpose of the HAVING clause in SQL Server?
The HAVING clause is used in conjunction with the GROUP BY clause to
filter groups based on specified conditions after the grouping has been
done.
6. What is a stored procedure in SQL Server?
A stored procedure is a precompiled collection of SQL statements and
procedural logic that is stored in the database and can be executed
multiple times.
7. How do you handle errors in stored procedures in SQL Server?
Errors in stored procedures can be handled using TRY...CATCH blocks to
catch and handle exceptions gracefully, allowing for rollback of
transactions if necessary.
8. Explain the purpose of the @@IDENTITY and SCOPE_IDENTITY() functions in
SQL Server.
@@IDENTITY returns the last identity value generated for any table in the
current session, while SCOPE_IDENTITY() returns the last identity value
generated within the current scope (e.g., within the current stored
procedure, trigger, or function).
9. What is a Common Table Expression (CTE) in SQL Server?
A Common Table Expression (CTE) is a temporary result set that can be
referenced within a SELECT, INSERT, UPDATE, or DELETE statement. It helps
in simplifying complex queries and improving readability.
10. Explain the purpose of the MERGE statement in SQL Server.
The MERGE statement allows you to perform INSERT, UPDATE, and DELETE
operations in a single statement based on the results of a join between a
target table and a source table.
11. What is the purpose of the INDEX hint in SQL Server?
The INDEX hint allows you to specify which index should be used by the
query optimizer when executing a query, providing more control over
query execution plans.
12. What is the purpose of the NOLOCK hint in SQL Server?
The NOLOCK hint, also known as READUNCOMMITTED, allows a query to
read data without acquiring locks on the underlying tables, potentially
improving query performance at the risk of reading uncommitted data.
13. What is the difference between UNION and UNION ALL in SQL Server?
UNION removes duplicate rows from the result set, while UNION ALL
includes all rows, including duplicates, in the result set.
14. Explain the purpose of the ROW_NUMBER() function in SQL Server.
The ROW_NUMBER() function assigns a unique sequential integer to each
row in the result set based on the specified ordering. It is commonly used
for pagination or ranking purposes.
15. What is the purpose of the PIVOT and UNPIVOT operators in SQL Server?
PIVOT rotates rows into columns based on specified column values, while
UNPIVOT performs the opposite operation, rotating columns into rows.
16. What is the purpose of the EXISTS and NOT EXISTS operators in SQL Server?
EXISTS is used to check for the existence of rows returned by a subquery,
while NOT EXISTS is used to check for the absence of rows returned by a
subquery.
17. What is SQL injection and how can it be prevented in SQL Server?
SQL injection is a security vulnerability that occurs when untrusted input is
inserted into SQL queries, allowing attackers to execute malicious SQL
code.
It can be prevented by using parameterized queries, stored procedures,
input validation, and proper user permissions.
18. Explain the purpose of the XML data type in SQL Server.
The XML data type allows you to store XML data in a structured format
within SQL Server, enabling efficient storage, retrieval, and manipulation of
XML data.
19. What is the purpose of the TRY_PARSE function in SQL Server?
The TRY_PARSE function attempts to convert a string representation of a
value to the specified data type, returning NULL if the conversion fails
instead of throwing an error.
20. How do you optimize query performance in SQL Server?
Query performance can be optimized by creating appropriate indexes,
minimizing table scans, avoiding unnecessary joins, using parameterized
queries, updating statistics, and optimizing query execution plans.
22. Explain the difference between the TRUNCATE TABLE and DELETE
statements in SQL Server.
TRUNCATE TABLE removes all rows from a table, but it does not log
individual row deletions, making it faster and less resource-intensive than
DELETE, which removes rows one by one and logs each deletion.
23. What is the purpose of the ROWLOCK and PAGELOCK hints in SQL Server?
ROWLOCK hint instructs the SQL Server query optimizer to use row-level
locking, whereas PAGELOCK hint instructs it to use page-level locking.
These hints can be used to control the granularity of locks acquired during
query execution.
24. Explain the purpose of the XML PATH() function in SQL Server.
The XML PATH() function is used to concatenate values from multiple rows
into a single string, formatted as XML. It is commonly used for string
aggregation in SQL Server.
25. What is the purpose of the SET NOCOUNT ON statement in SQL Server?
SET NOCOUNT ON statement prevents the message indicating the
number of rows affected by a Transact-SQL statement from being returned
as part of the result set. This can reduce network traffic and improve
performance, especially in environments with high transaction rates.
27. What is the purpose of the ROLLUP and CUBE operators in SQL Server?
ROLLUP generates subtotals and grand totals for a group of rows, while
CUBE generates subtotals and grand totals for all possible combinations of
grouping columns. These operators are used with the GROUP BY clause for
multi-level aggregation.
30. Explain the purpose of the LEAD and LAG functions in SQL Server.
The LEAD function is used to access data from subsequent rows within the
same result set, while the LAG function is used to access data from
preceding rows. These functions are often used for time-series analysis
and to compare values across adjacent rows.
31. Explain the purpose of the PERSISTED keyword in SQL Server.
33. What is the purpose of the CROSS APPLY and OUTER APPLY operators in SQL
Server?
CROSS APPLY and OUTER APPLY operators are used to invoke a table-valued
function for each row returned by the outer query. CROSS APPLY returns only
the rows for which there is a match between the left and right table expressions,
while OUTER APPLY returns all rows from the left table expression and NULLs
for unmatched rows.
34. Explain the purpose of the SQL Server Agent and how it is used.
35. What are the different recovery models available in SQL Server, and how do they
differ?
SQL Server supports three recovery models: Simple, Full, and Bulk-Logged.
Simple recovery model logs minimal information, allowing for easy management
of transaction logs but providing limited recovery options.
Full recovery model logs all changes to the database, enabling point-in-time
recovery but requiring regular transaction log backups.
Bulk-Logged recovery model is similar to Full recovery model but minimally logs
bulk operations, reducing log space usage during bulk operations.
36. Explain the purpose of the SQL Server Profiler tool.
SQL Server Profiler is a graphical tool used to monitor and capture events and
activities occurring in SQL Server, such as queries, stored procedure calls, and
errors. It helps database administrators and developers analyze database
performance, diagnose issues, and optimize queries.
37. What is the purpose of the SQL Server Reporting Services (SSRS)?
38. Explain the purpose of the SQL Server Integration Services (SSIS).
39. What is the purpose of the SQL Server Analysis Services (SSAS)?
SQL Server Analysis Services (SSAS) is a multidimensional and data mining tool
used for online analytical processing (OLAP) and data mining tasks. It allows
users to design and deploy analytical models, create cubes and data mining
structures, and perform advanced analytics on large datasets.
40. Explain the purpose of the SQL Server Always On Availability Groups feature.