SQL from Learning SQL = Ch3 Flashcards

(15 cards)

1
Q

What is the difference between WHERE and HAVING clauses?

A

WHERE filters rows before grouping; HAVING filters after GROUP BY.

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

Why might SELECT DISTINCT produce unexpected results when used with multiple columns?

A

Because DISTINCT applies to the entire row, not individual columns.

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

How does SQL determine which rows to return when multiple tables are listed in the FROM clause without a JOIN condition?

A

It returns a Cartesian product of all combinations of rows from the listed tables.

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

In what order are SQL clauses logically evaluated in a SELECT statement?

A

FROM, WHERE, GROUP BY, HAVING, SELECT, ORDER BY

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

How can you alias a column so that the alias can be used in the ORDER BY clause but not in WHERE?

A

By defining it in the SELECT clause and referencing it in ORDER BY; WHERE is evaluated earlier.

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

Why can’t you use an alias defined in the SELECT clause within the WHERE clause?

A

Because WHERE is evaluated before SELECT.

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

How would SQL handle this query: SELECT name, price FROM products WHERE price = NULL?

A

It would return no rows because comparisons with NULL must use IS NULL.

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

What is the behavior of the BETWEEN operator when used with NULL values?

A

It returns NULL (treated as unknown) if either boundary or the test value is NULL.

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

Why is SELECT * considered dangerous in production code?

A

Because it can return more data than needed, break applications if schema changes, and reduce performance.

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

What happens if you alias a column without using the AS keyword?

A

It still works in most databases, but readability suffers.

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

What’s the difference between WHERE col != NULL and WHERE col IS NOT NULL?

A

The first always returns false; the second is correct for filtering out NULLs.

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

How would you select the second highest salary from an employees table?

A

SELECT MAX(salary) FROM employees WHERE salary < (SELECT MAX(salary) FROM employees);

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

How does SQL treat an ORDER BY clause with a column index rather than a name?

A

It sorts by the specified column position in the SELECT list.

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

How can using functions in a WHERE clause impact performance?

A

It may prevent the use of indexes, leading to full table scans.

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

What’s the implication of using SELECT DISTINCT with ORDER BY on large datasets?

A

It may require SQL to sort and compare all rows, increasing processing time.

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