T SQL, short for Transact-SQL, is an extension of SQL that includes procedural programming, local variables and supports functions for processing strings, dates, and mathematics. As the cornerstone of Microsoft SQL Server, mastering T SQL is vital for database professionals to effectively interact with and manage data. In tech interviews, questions about T SQL assess a candidate’s proficiency in database management, data manipulation, and their ability to write efficient and dynamic SQL queries.
T-SQL Fundamentals
- 1.
What is T-SQL and how is it different from standard SQL?
Answer:Transact-SQL (T-SQL) is an extension of SQL that’s specific to Microsoft SQL Server. It includes functionalities such as procedural programming, local variables, and exception handling through
TRY...CATCHblocks. These features are not found in standard SQL.Key T-SQL Features
-
Stored Procedures: T-SQL supports server-side scripts, known as stored procedures, for better security, performance, and encapsulation.
-
User Defined Functions (UDFs): These custom, reusable functions can help in tasks not directly supported by built-in SQL functions.
-
Common Table Expressions (CTEs): With the
WITHclause, T-SQL offers an efficient way to define temporary result sets. -
Triggers: T-SQL can be used to define triggers that automatically execute in response to certain database events.
-
Table Variables: These are variable collections, especially useful for temporary data storage during complex queries.
-
Transaction Control: T-SQL allows finer-grained control over transactions with commands like
BEGIN TRAN,ROLLBACK, andCOMMIT.
Code Example: TRY-CATCH Block in T-SQL
Here is the T-SQL code:
BEGIN TRY -- Generate a divide by zero error intentionally DECLARE @num1 INT = 10, @num2 INT = 0; SELECT @num1 / @num2; END TRY BEGIN CATCH -- Provides details of the error PRINT 'Error Number: ' + CAST(ERROR_NUMBER() AS VARCHAR); PRINT 'Error Severity: ' + CAST(ERROR_SEVERITY() AS VARCHAR); PRINT 'Error State: ' + CAST(ERROR_STATE() AS VARCHAR); PRINT 'Error Line: ' + CAST(ERROR_LINE() AS VARCHAR); PRINT 'Error Message: ' + ERROR_MESSAGE(); END CATCH; -
- 2.
Explain the use of the SELECT statement in T-SQL.
Answer: - 3.
What are the basic components of a T-SQL query?
Answer: - 4.
How do you write a T-SQL query to filter data using the WHERE clause?
Answer: - 5.
Describe how to sort data using the ORDER BY clause in T-SQL.
Answer: - 6.
What are JOINs in T-SQL and can you explain the different types?
Answer: - 7.
How do you implement paging in T-SQL queries?
Answer: - 8.
What is the difference between UNION and UNION ALL?
Answer: - 9.
How are aliases used in T-SQL queries?
Answer: - 10.
Can you explain the GROUP BY and HAVING clauses in T-SQL?
Answer:
Data Manipulation and Conversion
- 11.
What are the T-SQL commands for inserting, updating, and deleting data?
Answer: - 12.
How do you perform a conditional update in T-SQL?
Answer: - 13.
What is the purpose of the COALESCE function?
Answer: - 14.
Explain how to convert data types in T-SQL.
Answer: - 15.
How do you handle NULL values in T-SQL?
Answer: