Intermediate SQL - Sorting and Grouping Flashcards

This final chapter teaches you how to sort and group data. These skills will take your analyses to a new level by helping you uncover critical business insights and identify trends and performance. You'll get hands-on experience to determine which films performed the best and how movie durations and budgets changed over time.

1
Q

Which keyword is used to sort results of one or more fields?

A

ORDER BY

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

How does the ORDER BY keyword sort by default?

A

Ascending (smallest to biggest or A-Z)

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

How can we change the sort of the ORDER BY keyword?

A

We can add the DESC keyword to sort in ascending order

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

True/False - With ORDER BY, you have to select the field you want to sort on.

A

False - we don’t have to select the field we are sorting on,, but its a good idea to mention it in the select for clarity

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

If there are two fields in an ORDER BY separated by a comma - when will the second field be thought of as a tie-breaker?

A

When the first field is not decisive in telling the order

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

If we are using ORDER BY on two fields and are getting a tie we can break that tie by adding what?

A

A second sorting field

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

If we are using ORDER BY on two fields and are getting a tie we can break that tie by adding what?

A

A second sorting field

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

True/False - We can select a different order for each field we are sorting when using an ORDER BY on multiple fields.

A

True

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

What is the correct order of execution for this query?

A

FROM
WHERE
SELECT
ORDER BY
LIMIT

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

What keyword is used to group data in SQL?

A

GROUP BY

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

GROUP BY is often used alongside [blank] to provide summary statistics, particularly when only grouping a single field and selecting multiple fields.

A

aggregate functions

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

Why is GROUP BY often used with aggregate functions?

A

the aggregate functions will reduce the non-grouped field to one record only, which will need to correspond to one group

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

SQL will return an error if we try to SELECT a field that is not in our GROUP BY clause. We’ll need to correct this by doing what?

A

adding an aggregate function around the field that is not in our GROUP BY clause

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

We can use GROUP BY on multiple fields similar to ORDER BY, but the order we write the fields affects what about the data?

A

affects how the data is grouped

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

True/False - We can combine GROUP BY with ORDER BY to group our results, make a calculation, and then order our results.

A

True - Here is an example query without ORDER BY, and this is the same query with ordering added.

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

Should ORDER BY be written before or after GROUP BY?

A

ORDER BY is always written after GROUP BY

15
Q

You can reference an alias later in a query as long as the operator you first assigned the alias in is what?

A

executed first

16
Q

What is the correct order of execution for this query?

A

FROM
GROUP BY
SELECT / ALIAS
ORDER BY
LIMIT

17
Q

True/False - In SQL, we can filter aggregate functions with WHERE clauses.

A

False - groups have their own special filtering word

18
Q

What filtering keyword is used to filter based on the results of an aggregate function?

A

HAVING

19
Q

Why do groups have their own keyword for filtering?

A

order of execution

20
Q

What is the order of execution for this query?

A

FROM
WHERE
GROUP BY
HAVING
SELECT
ORDER BY
LIMIT

21
Q

What type of records does WHERE filter?

A

individual records

22
Q

What type of records does HAVING filter?

A

grouped records