Chapter 3 Filtering and Sorting Data Flashcards Preview

70-461 > Chapter 3 Filtering and Sorting Data > Flashcards

Flashcards in Chapter 3 Filtering and Sorting Data Deck (44)
Loading flashcards...
1

What are the 3 query clauses that enable you to filter data based on predicates?

ON, WHERE, and HAVING.

2

What is two-valued logic?

When NULLs are not possible in the data, the predicate can evaluate to true or false.

3

What is three-valued logic?

When NULLs are possible in the data, the predicate can evaluate to true, false, or unknown. You get an unknown when at least one operand is NULL.

4

Are two NULLs considered equal, e.g. NULL = NULL?

No. NULL = NULL is unknown - not true.

5

What is the purpose of IS NULL and IS NOT NULL?

IS NULL returns true when the tested operand is NULL. IS NOT NULL returns true when the tested operand is not NULL.

6

What is SARG (Search Argument)?

Search Argument describes a form that a predicate needs to be in for SQL Server to use indexes efficiently without requiring a full scan of the table. Predicates such as col1 = 10 and col1 > 10 are search argument form. Basically, applying manipulation to the filtered column prevents the predicate from being search argument, e.g. F(col1) = 10, WHERE COALESCE(shippeddate, '19000101') = COALESCE(@dt, '19000101'), col1 -1 <= @n + 1.

7

What is the correct way to handle NULL inputs in a predicate?

Don't use ISNULL or COALESCE because this breaks the search argument form; instead, use something like WHERE region <> 'WA' OR region IS NULL;

8

What is the order of precedence for logical operators?

Parenthesis then NOT then AND then OR, e.g. WHERE col1 = 'w' AND col2 = 'x' OR col3 = 'y' AND col4 = 'z' evaluates to (col1='w' AND col2='x') OR (col3='y' AND col4='y')

9

In what order are the expressions in a WHERE clause evaluated?

Due to the all at once concept in the language, the expression will not be evaluated in left to right order. SQL Server could decide for cost related reasons, to start with the right expression, e.g. WHERE propertyType = 'INT' AND CAST(propertyValue AS INT) > 10. To make this query work, you can use TRY_CAST instead.

10

Can implicit conversion in the WHERE clause hurt performance?

Yes. For example, when comparing against literals, be sure to distinguish between unicode and non-unicode correctly, e.g. WHERE lastName = N'Davis'

11

What is the LIKE function?

Used to filter character string data (regular and unicode) based on pattern matching, e.g. LIKE . Supports: % = any string including empty string, _ single character, [ABC] single character from a set, [A-C] single character from a range, [^ABC] single character not in the list or range.

12

What is the ESCAPE keyword used for in the context of LIKE?

Used to help search for a character that is considered a wildcard. You must designate your escape character with the ESCAPE keyword, e.g. col1 LIKE '!_%' ESCAPE '!' searches for strings that start with an underscore using the ! as the escape character.

13

What are some of the performance concerns with LIKE?

When LIKE is used with a known prefix (col LIKE 'ABC%', SQL Server can efficiently use an index on the filtered column. When the pattern starts with a wildcard (col LIKE '%ABC%'), SQL cannot rely on index ordering any more.

14

How does SQL Server interpret a date literal when used in a where clause such as WHERE orderdate='02/12/07'?

Each logon has a default language associated with it and the default language sets various session options on the logon's behalf, including one called DATEFORMAT. A logon with us_english will have a DATEFORMAT set to mdy, British to dmy, and Japenese to ymd.

15

What is the language neutral date format?

ymd, e.g. 20070212. is language neutral for all date and time data types.

16

What are the performance benefits of the WHERE clause?

(1) You reduce network traffic by filtering in the database server instead of the client, (2) potentially use indexes to avoid full scans of the tables involved thus minimizing I/O.

17

What is the form of a filter predicate that can rely on index ordering?

A search argument.

18

Assuming that no ORDER BY is specified, does a given query have a guaranteed order?

No. A table in T-SQL is supposed to represent a relation; a relation is a set, and a set has no order to it's elements. Unless, you explicitly instruct the query otherwise, the results have no guaranteed order. The ordering is said to be arbitrary - it's optimization dependent..

19

What is the only way to truly guarantee that the rows are returned from a query in a certain order?

Add an ORDER BY clause.

20

If you don't indicate a direction (ASC, DESC) with the ORDER BY clause, which is the default?

ASC

21

Assuming that an ORDER BY is specified, but that the ordered column is not unique, do the duplicate rows have a guaranteed order?

No. The ordering of the rows with the same value is not guaranteed. You can specify additional expressions in the ORDER BY list to serve as a tie-breaker.

22

Can you sort by the ordinal position of columns, e.g. ORDER BY 4, 1? Why is this a bad practice?

Yes, but it is considered a bad practice because (1) It's non-relational (2) If the SELECT list ever changes, you must remember to change the ordinal indices (otherwise, they may not sync up)

23

Can you sort by columns that you're not returning?

Yes, but it depends. The rules is, you can order the results by elements that are not part of the SELECT list, as long as the result rows would have normally been allowed there. However, if SELECT DISTINCT is used, the source and result rows don't map up one to one and you can only sort by elements in the SELECT list.

24

Are column aliases assigned in the SELECT clause visible to the ORDER BY clause?

Yes. ORDER BY gets evaluated conceptually after SELECT - unlike most other clauses. As a result, the column aliases assigned in SELECT are visible to ORDER BY.

25

How does SQL handle NULL's during sorting?

Standard SQL says that NULLs should sort together, but leaves it to the implementation to decide if they should be before or after non-NULL values. In SQL Server, they are sorted BEFORE non-NULLs when ASC is used.

26

What is a result with an ORDER BY called? What is a result without an ORDER BY called?

A query with an ORDER BY returns a cursor. A query without an ORDER BY returns a relation (relational).

27

How can you achieve completely deterministic ordering?

The ORDER BY list must be unique.

28

What is the TOP option and how do you use it?

You specify the TOP option in the SELECT clause followed by the requested number of rows in parenthesis (BIGINT); you can also specify a percent of rows to filter instead of a number (FLOAT value 0 - 100). The PERCENT option puts a ceiling on the resulting number of rows. TOP (1) PERCENT of 830 rows => 8.3 => ceiling => 9 rows, e.g. SELECT TOP(1) col1, col2, col3 or SELECT TOP(1) PERCENT col1, col2, col3.

29

Does T-SQL support specifying the number of rows to filter using the TOP option without parenthesis?

Yes, but only for backwards compatibility reasons. The correct syntax is with parenthesis.

30

Is the TOP option limited to a constant?

The TOP option is not limited to a constant input. You can also specify a self contained expression such as a parameter/variable, e.g. DECLARE @n AS BIGINT = 5;
SELECT TOP (@n) col1, col2, etc.