SQL - Aggregate Functions Flashcards

1
Q

count the number of rows

A

COUNT()

SELECT COUNT(*)
FROM table_name;

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

the sum of the values in a column

A

SUM()

SELECT SUM(downloads)
FROM fake_apps;

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

the largest/smallest value

A

MAX(), MIN()

SELECT MAX(downloads)
FROM fake_apps;

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

the average of the values in a column

A

AVG()

SELECT AVG(downloads)
FROM fake_apps;

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

round the values in the column

A

ROUND()

SELECT ROUND(price, 0)
FROM fake_apps;

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

_______ is a clause used with aggregate functions to combine data from one or more columns

A

GROUP BY

SELECT year,
AVG(imdb_rating)
FROM movies
GROUP BY year
ORDER BY year;

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

_______ limit the results of a query based on an aggregate property.

A

HAVING

SELECT year,
genre,
COUNT(name)
FROM movies
GROUP BY 1, 2
HAVING COUNT(name) > 10;

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

Aggregate functions

A

Aggregate functions combine multiple rows together to form a single value of more meaningful information.

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

Find the error in this code:

SELECT COUNT(*)
FROM songs
HAVING plays > 100;

A

It should be WHERE instead of HAVING.

It should be WHERE plays > 100 because WHERE filter rows and HAVING filter groups.

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

Which function takes a column and returns the total sum of the numeric values in that column?

A

SUM()

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

What does the following query do?

SELECT price,
COUNT(*)
FROM menu
WHERE orders > 50
GROUP BY price;

A

It calculates the total number of menu items that have been ordered more than 50 times – for each price.

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

How would you calculate the minimum number of stops from the train table?

A

SELECT MIN(stops)
FROM train;

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

What does the ROUND function take as argument(s)?

A

The column name, and the number of decimal places to round the values in the column to.

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

The WHERE clause filters rows, whereas the HAVING clause filter groups. T/F

A

T

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

What does the COUNT() function take as argument(s)?

A

The name of a column or *.

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

What does the following query do?

SELECT neighborhood,
AVG(price)
FROM apartments
GROUP BY neighborhood;

A

It calculates the average price of apartments in each neighborhood.

17
Q

What does the following query do?

SELECT genre,
SUM(downloads)
FROM kindle
GROUP BY genre;

A

It returns the total amount of downloads – for each genre.