This collection of 30 multiple-choice questions (MCQs) focuses on advanced SQL Server topics such as Dynamic SQL, FOR XML/JSON queries, PIVOT/UNPIVOT operators, and advanced window functions. These MCQs are designed for SQL Server professionals aiming to enhance their query optimization skills and database management techniques.
Creating Dynamic SQL
Which of the following is used to execute a string of SQL code dynamically in SQL Server?
a) EXEC
b) EXECUTE
c) sp_executesql
d) All of the above
What is the main purpose of dynamic SQL in SQL Server?
a) To improve query execution time
b) To create and execute SQL code at runtime
c) To fetch multiple result sets
d) To optimize stored procedures
Which of the following is true about the sp_executesql stored procedure in dynamic SQL?
a) It only executes SELECT queries
b) It supports parameterized queries
c) It doesn’t allow variable substitution
d) It cannot return result sets
What should you use to prevent SQL injection in dynamic SQL queries?
a) SELECT statements
b) sp_executesql with parameters
c) Using EXEC without parameters
d) Dynamic SQL with open cursors
Which of the following SQL commands cannot be used with dynamic SQL?
a) INSERT
b) DELETE
c) CREATE
d) ALTER DATABASE
FOR XML and FOR JSON Queries
Which clause in SQL Server is used to convert query results into XML format?
a) FOR XML AUTO
b) FOR XML RAW
c) FOR XML PATH
d) All of the above
What is the default root element for a FOR XML PATH query?
a) root
b) xmlroot
c) data
d) None of the above
Which option would you use in FOR XML to generate XML with each row as a separate element?
a) FOR XML AUTO
b) FOR XML PATH
c) FOR XML RAW
d) FOR XML EXPLICIT
Which SQL clause converts query results into JSON format in SQL Server?
a) FOR JSON PATH
b) FOR JSON AUTO
c) JSON QUERY
d) Both a and b
What is the purpose of the INCLUDE_NULL_VALUES option in FOR JSON queries?
a) It eliminates NULL values from the JSON output.
b) It includes NULL values in the JSON output.
c) It formats JSON for compatibility.
d) None of the above
PIVOT and UNPIVOT Operators
What does the PIVOT operator do in SQL Server?
a) Converts rows to columns
b) Converts columns to rows
c) Aggregates data into groups
d) None of the above
Which function is often used in conjunction with PIVOT to aggregate data in SQL Server?
a) SUM
b) COUNT
c) AVG
d) All of the above
What is the purpose of the UNPIVOT operator in SQL Server?
a) Converts rows into columns
b) Converts columns into rows
c) Removes duplicates from the result set
d) Combines multiple rows into one
Which of the following is the correct syntax for using PIVOT in SQL Server?
a) SELECT * FROM table PIVOT (SUM(column) FOR column IN (val1, val2)) AS pivot_table
b) SELECT * FROM table PIVOT (SUM(column) FOR column BY (val1, val2)) AS pivot_table
c) SELECT * FROM table UNPIVOT (column FOR column IN (val1, val2)) AS unpivot_table
d) SELECT * FROM table PIVOT (AVG(column) FOR column IN (val1, val2)) AS pivot_table
Which of the following is true about the UNPIVOT operator?