1. SQL Flashcards

1
Q

tables, rows, columns - synonyms

A

relations, tuples, attributes

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

What is the default order of the rows in SQL?

(i.e. if there is no ORDER BY in SELECT)

A

The order of the rows is nondeterministic. So any order would be correct, and we can’t make any assumptions about it.

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

NULL values - a general meaning

A

NULL represents an “unknown” or “missing” value

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

How NULL values interact with Boolean operators

A

NULL short-circuits with boolean operators. That means a boolean expression involving NULL will evaluate to:

– TRUE, if it’d evaluate to TRUE regardless of whether the NULL value is really TRUE or FALSE.

– FALSE, if it’d evaluate to FALSE regardless of whether the NULL value is really TRUE or FALSE.

– Or NULL, if it depends on the NULL value.

Examples:

NULL or TRUE => TRUE

NULL and FALSE => FALSE

NULL or FALSE => NULL

NULL and TRUE => NULL

i.e. exactly matches the “unknown” meaning

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

How NULL acts in WHERE clauses?

A

WHERE NULL is just like WHERE FALSE. The row in question does not get included.

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

How NULL acts in expressions?

A

If you do anything with NULL, you’ll just get NULL. For instance if x is NULL, then x > 3, 1 = x, and x + 4 all evaluate to NULL. Even x = NULL would evaluate to NULL; if you want to check whether x is NULL, then write x IS NULL or x IS NOT NULL instead.

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

How NULL acts in aggregates? (1 exception)

A

Every aggregate ignores NULL values except for COUNT(*). (So COUNT() returns the number of non-NULL values in the specified column, whereas COUNT(*) returns the number of rows in the table overall.)

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

Difference between WHERE and HAVING

A

• WHERE occurs before grouping. It filters out uninteresting rows. • HAVING occurs after grouping. It filters out uninteresting groups.

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

What’s wrong with this query?

SELECT age, name

FROM Person

GROUP BY age;

A

If you’re going to do any grouping/aggregation at all, then you must only SELECT grouped/aggregated columns.

In this example:

Each age group can include many names.

DBMS can’t decide which one to choose.

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