Vous êtes sur la page 1sur 2

Learn SQL from Scratch

Aggregate Functions Calculations performed on multiple rows

COUNT()
SELECT COUNT(*)
Calculate how many rows are in the fake_apps table
FROM fake_apps;

SUM()
SELECT SUM(downloads)
Calculate the sum of all the values in the downloads
FROM fake_apps; column

MAX()
SELECT MAX(downloads)
Return the highest value in the downloads column
FROM fake_apps;

MIN()
SELECT MIN(downloads)
Return the lowest value in the downloads column
FROM fake_apps;

AVG()
SELECT AVG(price)
Calculate the average value of the price column
FROM fake_apps;

GROUP BY
SELECT category, SUM (downloads)
Calculate the total number of downloads for each
FROM fake_apps
category
GROUP BY category;

GROUP BY
SELECT category, SUM(downloads)
The reference number 1 refers to the first selected
FROM fake_apps
column (category)
GROUP BY 1;

HAVING
SELECT category, SUM(downloads)
FROM fake_apps WHERE can not be used with aggregate functions
GROUP BY category
HAVING is used to filter groups
HAVING SUM(downloads) > 5;
Learn SQL from Scratch

Review
SELECT category, SUM(downloads)
FROM fake_apps 1.SELECT as many columns as you want

WHERE price = 0 2.FROM indicates which table


GROUP BY 1 3.WHERE filters based on individual rows
HAVING SUM(downloads) > 2 4.GROUP BY tells you how to bucket your data
ORDER BY 2 DESC 5.HAVING filters based on your aggregates
LIMIT 5;
6.ORDER BY sorts the query
7.LIMIT reduces the number of results

Vous aimerez peut-être aussi