BookReviews Flashcards
Lessons review questions
You create an index to support the WHERE clause of a query. However, SQL Server
does not use the index. What are the possible reasons? (Choose all that apply.)
- A - The arguments in the predicate are not searchable.
- B - SQLServer does not consider using an index to support the WHERE clause.
- C - The predicate is not selective enough.
- D - You are in the context of the tempdb database, and SQL Server does not use indexes in this database
Correct answers: A and C
- A - Correct: SQL Server does not use an index to support the WHERE clause if the arguments in the predicate are not searchable.
- B - Incorrect: SQL Server supports the WHERE clause with indexes.
- C - Correct: SQL Server might decide not to use an index to support the WHERE clause if the query is not selective enough.
- D - Incorrect: SQL Server considers using indexes in the context of the tempdb database just like in the context of any other database.
What are the benefits of using a CTE over derived tables? (Choose all that apply.)
A.CTEs are better performing than derived tables.
B.CTEs don’t nest; the code is more modular, making it easier to follow the logic.
C.Unlike with derived tables, you can refer to multiple instances of the same CTE name, avoiding repetition of code.
D.Unlike derived tables, CTEs can be used by all statements in the session, and not just the statement defining them
Correct Answers: B and C
A.Incorrect: All types of table expressions are treated the same in terms of optimization— they get unnested.
B.Correct: If you want to refer to one derived table from another, you need to nest them. With CTEs, you separate those by commas, so the code is more modular and easier to follow.
C.Correct: Because the CTE name is defined before the outer query that uses it, the outer query is allowed to refer to multiple instances of the same CTE name.
D.Incorrect: CTEs are visible only in the scope of the statement that defined them.
Which full-text search elements can you use to prevent indexing noisy words? (Choose all that apply.)
A. Stopwords
B. Thesaurus
C. Stemmer
D. Stoplists
Correct answers: A and D
A.correct: Stopwords include noisy words.
B.incorrect: Thesaurus is used for synonyms.
C.incorrect: Stemmer is used for generating inflectional forms of words.
D.correct: You group stopwords in stoplists
When referring in the OUTPUT clause to columns from the inserted rows, when should you prefix the columns with the keyword inserted?
A. Always
B. Never
C. Only when the statement is UPDATE
D. Only when the statement is MERGE
Correct answer: A
A. correct: When referring to elements from inserted rows, you must always prefix the column with the keyword inserted.
B. incorrect: There are no cases where you can omit the keyword inserted—even if the statement is just an INSERT.
C. incorrect: It’s true that you need to prefix inserted elements in an UPDATE statement with the keyword inserted, but not just in an UPDATE statement.
D. incorrect: It’s true that you need to prefix inserted elements in a MERGE statement with the keyword inserted, but not just in a MERGE statement
What is the advantage of using THROW in a CATCH block?
A. THROW in a CATCH block does not require parameters and so is easier to write.
B. THROW re-throws the original error so that the original error can be handled.
C. THROW causes an error severity of level 16 automatically.
D. The statement before a THROW requires a semicolon.
<strong>Correct Answer: B</strong><br></br><strong>A. incorrect</strong>: Although it is true that THROW does not take parameters in a CATCHblock, that is not necessarily an advantage.<br></br><strong>B. correct</strong>: The THROW statement in a CATCH block can re-throw an error andthereby allow you to report on an error in the TRY block without having to havestored any prior information. This makes it possible to do all error handling in the<br></br>CATCH block.<br></br><strong>C. incorrect:</strong> THROW always results in a severity level of 16, but that is not necessarily an advantage. RAISERROR is more flexible by allowing a range of severity levels.<br></br><strong>D. incorrect</strong>: Requiring a semicolon on the previous T-SQL statement is perhaps agood coding requirement, but it is not a benefit provided by the THROW command.
Which of the following are true about the SET QUOTED_IDENTIFIER statement? (Choose all that apply.)
A. When set to ON, QUOTED_IDENTIFIER allows you to use double quotation marks to delimit T-SQL identifiers such as table and column names.
B. When set to OFF, QUOTED_IDENTIFIER allows you to use double quotation marks to delimit T-SQL identifiers such as table and column names.
C. When set to ON, QUOTED_IDENTIFIER allows you to use double quotation marks to delimit strings.
D. When set to OFF, QUOTED_IDENTIFIER allows you to use double quotation marks to delimit strings
Correct answers: A and D
A. correct: When you set QUOTED_IDENTIFIER to ON, you can use double quotation marks to delimit T-SQL identifiers such as table and column names.
B. incorrect: When you set QUOTED_IDENTIFIER to OFF, you cannot use double quotation marks to delimit T-SQL identifiers such as table and column names.
C. incorrect: When you set QUOTED_IDENTIFIER to ON, you cannot use double quotation marks to delimit strings.
D. correct: When you set QUOTED_IDENTIFIER to OFF, you can use double quotation marks to delimit strings
Which of the following strategies can help reduce blocking and deadlocking by reducing shared locks? (Choose all that apply.)
A. Add the READUNCOMMITTED table hint to queries
B. Use the READ COMMTTED SNAPSHOT option.
C.Use the REPEATABLE READ isolation level
D.Use the SNAPSHOT isolation level
correct answers: A, B, and D
A. correct: Adding a READUNCOMMITTED table hint causes no shared locks to be used by the statement.
B. correct: The READ COMMITTED SNAPSHOT option reads committed data from versions, not by acquiring shared locks.
C. incorrect: The REPEATABLE READ isolation level actually holds shared locks until the end of a transaction, and therefore can actually increase blocking and deadlocking.
D. correct: The SNAPSHOT isolation level also reduces shared locks by reading committed data from committed versions and not by using shared locks, so it also can reduce blocking and deadlocking.
Which WHEN clauses are required in a MERGE statement at minimum?
A. At minimum, the WHEN MATCHED and WHEN NOT MATCHED clauses are required.
B. At minimum, only one clause is required, and it can be any of the WHEN clauses.
C. At minimum, the WHEN MATCHED clause is required.
D. At minimum, the WHEN NOT MATCHED clause is required.
Correct answer: B
A. incorrect: Only one clause is required at minimum.
B. correct: Only one clause is required at minimum, and it can be any of the WHEN clauses.
C. incorrect: There’s no specific WHEN clause that is required; instead, any one clause at minimum is required.
D. incorrect: There’s no specific WHEN clause that is required; instead, any one clause at minimum is required
Which DMO gives you information about index usage?
- A.sys.dm_exec_query_stats
- B.sys.dm_exec_query_text
- C.sys.dm_db_index_usage_stats
- D.sys.indexes
Correct answer: C
- A. incorrect: The sys.dm_exec_query_stats DMO gives you statistics about queries, not indexes
- B. incorrect: The sys.dm_exec_query_text DMO gives you the text of the batches and queries
- C. correct: The sys.dm_db_index_usage_stats DMO gives you information about index usage
- D. incorrect: sys.indexes is a catalog view, not a DMO
Which database do you have to install in order to enable the Semantic Search feature?
A. msdb
B. distribution
C. semanticsdb
D. tempdb
Correct answer: C
A.incorrect: The msdb database is installed by default and is used for SQL Server Agent.
B.incorrect: The distribution database is installed and used by replication.
C.correct: You need the semanticsdb database in order to enable semantic search.
D.incorrect: The tempdb database is installed by default and is used for all temporary objects
How can you use output parameters in T-SQL stored procedures? (Choose all that apply.)
A. You can pass data into a procedure by using an output parameter, but you cannot receive information back from it.
B. You can pass data into a procedure by using an output parameter, and any change made to the parameter will be passed back to the calling routine.
C. You cannot pass data into a procedure by using an output parameter; it is only used for passing data back to the caller.
D. You cannot pass data into a procedure by using an output parameter, nor can you receive data back from a procedure from an output parameter.
Correct answer: b
A. incorrect: You can use an output parameter to receive information back from a stored procedure.
B. correct: You can both pass data into a stored procedure and retrieve information back from it, by using an output parameter.
C. incorrect: An output parameter is not used only for passing data back to the caller of the stored procedure. It is also used to pass data from the caller to a stored procedure.
D. incorrect: You can both pass data into a stored procedure and retrieve information back from it, by using an output parameter.
Why is it important to prefer set-based solutions for querying tasks instead of iterative ones? (Choose all that apply.)
- A. Because set-based solutions are based on the relational model, which is the foundation of T-SQL
- B. Because set-based solutions always provide better performance than iterative solutions
- C. Because set-based solutions usually involve less code than iterative solutions
- D. Because set-based solutions enable you to rely on the order of data
Correct Answer: A and C
- A. Correct: Set-based solutions are based on principles from the relational model, and this model is the foundation of SQL (the standard language) and T-SQL (thedialect in SQL Server).
- B. Incorrect: Although it is not common, sometimes iterative solutions are faster than set-based ones
- C. Correct: Because set-based solutions are declarative and iterative solutions are imperative, set-based solutions tend to involve less code.
- D. incorrect: Set-based solutions cannot make any assumptions regarding the order of the data because sets are unordered
What is the difference between the result of T1 CROSS APPLY T2 and T1 CROSS JOIN T2 (the right table expression isn’t correlated to the left)?
- A.CROSS APPLY filters only rows where the values of columns with the same name are equal; CROSS JOIN just returns all combinations.
- B.If T1 has rows and T2 doesn’t, CROSS APPLY returns an empty set and CROSS JOIN still returns the rows from T1.
- C.If T1 has rows and T2 doesn’t, CROSS APPLY still returns the rows from T1 and CROSS join returns an empty set.
- D.There is no difference.
Correct answer: D
- A. incorrect: Both return all combinations.
- B. incorrect: Both return an empty set.
- C. incorrect: Both return an empty set.
- D. correct: Both return the same result when there’s no correlation because CROSS APPLY applies all rows from T2 to each row from T1
Which of the following is only possible when using the MERGE statement in regard to the OUTPUT clause?
A. Referring to columns from the source table
B. Referring to both the keywords deleted and inserted
C. Assigning aliases to output columns
D. Using composable DML
Correct answer: A
A. correct: Only in a MERGE statement’s OUTPUT clause can you refer to elements from the source table.
B. incorrect: This can be done in an UPDATE statement too.
C. incorrect: Aliasing of target columns in the OUTPUT clause is allowed in all statements.
D. incorrect: Composable DML supports all statements
You are tasked with implementing a trigger. As part of the trigger’s code in specific conditions, you need to roll back the transaction. However, you need to copy the data from the inserted and deleted tables in the trigger into audit tables to keep track of what was supposed to be changed. How can you achieve this?
- A. Roll back the transaction, and then copy the data from the inserted and deleted tables into the audit tables
- B. Copy the data from the inserted and deleted tables into the audit tables and then roll back the transaction
- C. Copy the rows from the inserted and deleted tables into temporary tables, roll back the transaction, and then copy the data from the temporary tables into the audit tables
- D. Copy the rows from the inserted and deleted tables into table variables, roll back the transaction, and then copy the data from the table variables into the audit tables.
Correct Answer: D
- A. Incorrect: After you roll back the transaction in the trigger, the inserted and deleted tables are emptied
- B. Incorrect: The rollback causes the copying to the audit tables to be undone
- C.Incorrect: Changes against temporary tables are undone after you roll back a transaction
- D. Correct: Changes against table variables aren’t undone if you roll back a transaction, so this solution works correctly