Chapter 3: Filtering and Sorting Data Flashcards

1
Q

What is a two-valued logic?

A

When a predicate return as response “TRUE” or “FALSE”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What is a three-valued logic?

A

When a predicate returns as response “TRUE”, “FALSE” or “UNKNOWN”. It returns “UNKNOWN” when one of operands, or may both, is NULL.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

NULL = NULL ?

A

Returns “UNKNOWN”.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a Search Argument (SARG)?

A

When the predicate is in the form “column operator value” or “value operator column”. Using the predicate in this way, SQL Server can use indexes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Why we can’t apply a function to the filtered column?

A

Because it doesn’t allow SQL Server to use indexes. We also cannot manipulate the filtered column.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is the precedence of logical operators, like NOT, AND, OR, and so on?

A

NOT precedes the AND and OR operators. AND precedes the OR operator. Remember the operations that are in parentheses has more precedences than NOT, AND and OR.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Note for this chapter

A

Review the table for how to use LIKE wildcards.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the standard way to specify a date?

A

Like this: ‘YYYYMMDD’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Is guaranteed the order of a query?

A

No, it isn’t. You guarantee the order of a query when you specifiy a ORDER BY clause.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the default direction of sorting?

A

Ascendent.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

When the ordering is deterministic?

A

When you guarantee the order of the results. For sample, it the values of City columns aren’t unique, that means the order is not deterministic and is not guaranteed. But if you order by city and employeerId, you guarantee the order because employeerId is unique.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

When you order a result by a column which allows NULL, how SQL Server treats this?

A

If you specify the order to be ascedent, then NULLs value will be in the beginning of the result. Otherwise, if you specifiy the order to be descendent, the NULLs value will be on the bottom.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What is the difference between the result of a query with and one without an ORDER BY clause?

A

Without an ORDER BY clause, the result is relational (from an ordering perspective);
with an ORDER BY clause, the result is conceptually what the standard
calls a cursor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

What’s the TOP clause?

A

With the TOP option, you can filter a requested number or percent of rows from the query
result based on indicated ordering. You specify the TOP option in the SELECT clause followed
by the requested number of rows in parentheses (BIGINT data type). The ordering specification
of the TOP filter is based on the same ORDER BY clause that is normally used for presentation
ordering.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the syntax to specify a percent of rows to be filtered?

A
n = input
SELECT TOP (@n) PERCENT ...
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What’s the difference between TOP clause and OFFSET clause?

A

Both filters rows based in how many rows we want in our result. But TOP isn’t a standard, is just only for T-SQL, whereas OFFSET clause is a standard.

17
Q

How OFFSET clause works?

A

You specify how many rows you want to skip in the result, and then specify how many rows you want to fetch (get) in the result.

18
Q

What is the difference between NEXT and FIRST?

A

It’s more intuitive to use NEXT when you’re skipping rows (when you specifiy a number greater than 0 in the OFFSET clause), and it’s more intuitive to use FIRST when you’re not skipping rows (when you specify 0 in the OFFSET clause).

19
Q

How I can issue a OFFSET-FETCH clause without specifying a order presentation in the result?

A

Just adding in the ORDER BY clause the expression: “SELECT NULL”. For example: ORDER BY (SELECT NULL).

20
Q

How do you guarantee deterministic results with TOP?

A

By defining unique ordering to break ties.

21
Q

What’s is Sargable?

A

In RDBMS, sargable means when a query can improve his perfomance with an index. Sargable stands for Search Argument Able.

22
Q

When a query isn’t sargable?

A

When a function is applied to an indexed column example in the WHERE or ON clause. For example: SELECT * FROM TABLE WHERE Function(IndexedColumn) = Value;

23
Q

How I can implement paging in a database?

A

Using the clause: OFFSET … ROWS FETCH NEXT … ROWS ONLY. The crearly way to achieve this is: OFFSET (page - 1 ) * m ROWS FETCH FIRST n ROWS ONLY, where page = 1 (first page) and m = n. The first clause skip (page - 1) * m rows, and the second clause indicates it’ll get the first n rows (next can be used there instead of first).