第三單元、AGGREGATE FUNCTIONS
第二十三課、Functions
AggregateFunctions
We've learned how to write queries to retrieve information fromthe database. In this lesson we are going to learn how to performcalculations using SQL.
Aggregate functions compute a single result froma set of input values. For instance, when we need the sum or average of a particularcolumn, we can use aggregate functions to quickly compute it for us. We will belearning about different aggregate functions in this lesson.
For this lesson we have givenyou a table named fake_apps
which
is made up of data forfake mobile applications.
Instructions
1.Take a look at the data in thefake_apps table. In the code editor type
SELECT * FROM fake_apps;
2.Count how many apps are in thedatabase. In the code editor replace the previous statement with
SELECT COUNT(*) FROM fake_apps;
-
SELECT * FROM fake_apps;
DatabaseSchema
fake_apps 200 rows |
|
id |
INTEGER |
name |
TEXT |
category |
TEXT |
downloads |
INTEGER |
price |
REAL |
QueryResults
id |
name |
category |
downloads |
price |
3 |
siliconphase |
Productivity |
17193 |
0.0 |
6 |
Donzolab |
Education |
4259 |
0.99 |
10 |
Ittechi |
Reference |
3874 |
0.0 |
13 |
Subdrill |
Education |
7132 |
1.99 |
14 |
Anzoom |
Health & Fitness |
6941 |
14.99 |
21 |
kanity |
Health & Fitness |
2299 |
0.0 |
25 |
Zathcare |
Books |
16281 |
2.99 |
30 |
Basecode |
Sports |
21203 |
3.99 |
45 |
Hayholding |
Medical |
15845 |
14.99 |
59 |
Sailflex |
Productivity |
21984 |
0.99 |
60 |
waretam |
Catalogs |
31087 |
2.99 |
62 |
Donelectrics |
News |
6650 |
1.99 |
81 |
sonelectrics |
Entertainment |
9297 |
1.99 |
89 |
Zoodom |
Food & Drink |
29619 |
14.99 |
94 |
Quohouse |
Lifestyle |
4109 |
0.0 |
95 |
Quotetech |
Health & Fitness |
24934 |
0.0 |
104 |
Funtexon |
Utilities |
11229 |
0.99 |
108 |
Ozerphase |
Navigation |
14881 |
0.0 |
109 |
Kintonflex |
Catalogs |
28705 |
0.0 |
110 |
Tamptom |
Education |
5918 |
2.99 |
122 |
Whitetexon |
Games |
30959 |
0.0 |
123 |
Lanela |
Business |
21116 |
1.99 |
126 |
Ozerquote |
Lifestyle |
19422 |
3.99 |
128 |
Unosantech |
Finance |
15659 |
0.99 |
130 |
Canplus |
Lifestyle |
22923 |
1.99 |
132 |
Mathzoom |
Books |
30862 |
1.99 |
143 |
Canetouch |
Books |
29463 |
0.99 |
156 |
Lexilane |
News |
7630 |
0.99 |
157 |
Sunzoomron |
Reference |
28861 |
0.99 |
159 |
Sailline |
Sports |
9340 |
0.0 |
163 |
Gogoware |
Games |
7287 |
0.0 |
166 |
bigzap |
Games |
20126 |
0.0 |
175 |
O-electronics |
Music |
2469 |
0.0 |
181 |
Flexlane |
Navigation |
13576 |
1.99 |
182 |
Gogo-media |
Reference |
17242 |
2.99 |
185 |
santech |
Health & Fitness |
21359 |
1.99 |
199 |
Mediajob |
Games |
21821 |
0.99 |
200 |
Freelamfax |
Catalogs |
29513 |
2.99 |
212 |
Superfan |
Games |
2791 |
0.99 |
216 |
Bamgreen |
Utilities |
2212 |
0.0 |
222 |
Tincan |
Navigation |
17519 |
1.99 |
223 |
Konhow |
Sports |
11789 |
0.0 |
226 |
alphalux |
Books |
25956 |
0.99 |
233 |
Lamsoncode |
Education |
3910 |
0.99 |
234 |
Vaiatamace |
Productivity |
28473 |
0.0 |
245 |
Planetquote |
Finance |
24163 |
1.99 |
248 |
Zotzenhow |
Productivity |
15641 |
0.0 |
257 |
Duolux |
Entertainment |
6046 |
1.99 |
258 |
Zummaline |
Travel |
26779 |
0.99 |
259 |
E-planet |
Health & Fitness |
28069 |
14.99 |
272 |
superdrill |
Business |
15195 |
1.99 |
277 |
Fixcode |
Medical |
26243 |
0.0 |
282 |
Unoredware |
Education |
15769 |
2.99 |
284 |
Ladex |
Food & Drink |
30789 |
14.99 |
287 |
Duokix |
Medical |
15550 |
0.99 |
292 |
Biostrip |
Games |
17887 |
0.0 |
298 |
Mathcane |
Navigation |
10535 |
2.99 |
301 |
Daltquadla |
Education |
22593 |
0.99 |
308 |
Sanbam |
Business |
14554 |
0.0 |
314 |
Voyaelectronics |
News |
27793 |
0.99 |
317 |
Streethex |
Lifestyle |
17200 |
1.99 |
328 |
Indiace |
Education |
7678 |
0.99 |
332 |
yearfix |
Business |
4587 |
0.0 |
333 |
Newlam |
Finance |
30605 |
1.99 |
338 |
keyit |
Business |
22632 |
0.0 |
341 |
lanedax |
Entertainment |
1673 |
0.0 |
345 |
Triozone |
Photo & Video |
23817 |
0.99 |
347 |
Fasetom |
Games |
14555 |
0.0 |
349 |
Tamplam |
Health & Fitness |
31075 |
3.99 |
350 |
Zoovivaice |
Social Networking |
25682 |
14.99 |
352 |
Conelux |
Weather |
15307 |
0.0 |
354 |
Anstreet |
Catalogs |
4937 |
0.99 |
356 |
Zerzacom |
Navigation |
5541 |
0.0 |
357 |
Zenice |
Sports |
22849 |
0.0 |
362 |
Opetech |
Travel |
4940 |
14.99 |
366 |
Streetzim |
Travel |
28604 |
2.99 |
367 |
damgreen |
Reference |
15363 |
0.99 |
384 |
Highflex |
Business |
28488 |
14.99 |
390 |
technocorporation |
Navigation |
21061 |
1.99 |
396 |
Zaptech |
Reference |
11094 |
2.99 |
399 |
Danlane |
Utilities |
6896 |
1.99 |
402 |
Spanis |
Medical |
17651 |
0.0 |
403 |
Strong-techno |
Social Networking |
16479 |
0.0 |
408 |
K-fax |
Photo & Video |
16413 |
0.0 |
411 |
Cantechi |
Entertainment |
6465 |
1.99 |
412 |
Ranplus |
Weather |
17358 |
2.99 |
415 |
Technolux |
Sports |
24479 |
3.99 |
422 |
Sumzone |
Weather |
20344 |
0.99 |
428 |
Kontechnology |
Photo & Video |
20180 |
0.99 |
445 |
D-corporation |
Food & Drink |
18033 |
3.99 |
449 |
howtam |
News |
1826 |
0.0 |
457 |
Solois |
Weather |
27167 |
1.99 |
458 |
Tamp-can |
Education |
17669 |
14.99 |
459 |
Ganzcity |
Finance |
22201 |
1.99 |
466 |
Zercare |
Travel |
24377 |
0.0 |
474 |
Saozone |
Sports |
19280 |
0.99 |
490 |
Saltelectrics |
Games |
14120 |
0.99 |
495 |
Saltcode |
Travel |
2836 |
0.0 |
500 |
Line-house |
Education |
28906 |
0.0 |
506 |
K-in |
Reference |
23900 |
3.99 |
507 |
Damdox |
News |
19533 |
0.0 |
526 |
Duodox |
Music |
1654 |
0.0 |
536 |
Fasecan |
Catalogs |
29165 |
0.0 |
540 |
Zamgreen |
Catalogs |
22987 |
2.99 |
546 |
Stanunahex |
Utilities |
23118 |
0.0 |
548 |
Geotexon |
Utilities |
28419 |
2.99 |
560 |
Spandax |
Reference |
27686 |
0.99 |
562 |
Canecanway |
Weather |
12167 |
1.99 |
576 |
Domrancode |
Games |
12542 |
0.99 |
577 |
Xxx-namex |
Reference |
12107 |
0.0 |
578 |
Physelectrics |
Travel |
3240 |
0.99 |
580 |
Roundtom |
Finance |
18795 |
1.99 |
581 |
Redhouse |
Productivity |
24308 |
0.99 |
585 |
Treeace |
Social Networking |
21477 |
3.99 |
586 |
Isis |
Sports |
25640 |
0.0 |
590 |
Saltnix |
Finance |
10839 |
0.0 |
591 |
Tinunolane |
Navigation |
18388 |
14.99 |
592 |
stancity |
Education |
15606 |
1.99 |
596 |
Fasezoom |
Games |
14139 |
0.99 |
597 |
Roundfix |
Social Networking |
12734 |
1.99 |
600 |
tree-street |
Weather |
20902 |
0.0 |
604 |
Solodotam |
Weather |
7132 |
0.0 |
617 |
Joytechi |
Food & Drink |
7919 |
0.99 |
623 |
Zumholding |
Travel |
23020 |
0.0 |
633 |
Move-street |
News |
6476 |
0.99 |
635 |
Salthigh |
Business |
8928 |
2.99 |
638 |
Zimlane |
Music |
1387 |
3.99 |
640 |
fun-dax |
Games |
16153 |
0.0 |
641 |
Fax-it |
Entertainment |
28699 |
0.0 |
643 |
Joyice |
Games |
22284 |
0.0 |
644 |
Viamatity |
Entertainment |
6045 |
0.99 |
646 |
Fasezap |
Lifestyle |
30829 |
0.99 |
651 |
subzim |
Business |
30269 |
2.99 |
652 |
ontoline |
Lifestyle |
4831 |
0.99 |
654 |
Domit |
Navigation |
17791 |
0.0 |
656 |
Lamlane |
Photo & Video |
25946 |
1.99 |
658 |
Canstrip |
Sports |
6152 |
1.99 |
659 |
voltrax |
Utilities |
3304 |
0.0 |
669 |
Greenzone |
Social Networking |
30986 |
1.99 |
674 |
Funzap |
Sports |
20020 |
0.0 |
675 |
Unala |
Photo & Video |
19350 |
1.99 |
681 |
Tinplex |
Utilities |
2871 |
0.0 |
683 |
Trisholding |
Music |
9036 |
2.99 |
687 |
Technotaxon |
News |
21903 |
1.99 |
691 |
Highstrip |
Finance |
29633 |
0.0 |
697 |
Tampzone |
Music |
14633 |
14.99 |
703 |
Stanway |
Social Networking |
19191 |
0.0 |
706 |
Ranit |
Photo & Video |
28751 |
0.99 |
718 |
Goodhattom |
Travel |
18940 |
0.0 |
731 |
Lexihow |
Education |
4659 |
0.99 |
738 |
Faxdex |
Health & Fitness |
19020 |
1.99 |
740 |
Roundcity |
Entertainment |
23584 |
0.99 |
743 |
Vivalatlab |
Games |
4236 |
1.99 |
746 |
Doublelex |
Photo & Video |
5504 |
0.99 |
751 |
Alphatouch |
Education |
21709 |
1.99 |
752 |
K-zap |
Games |
12454 |
2.99 |
756 |
Newcity |
Music |
3326 |
0.0 |
757 |
Inch-tone |
Games |
11360 |
14.99 |
758 |
Ronzim |
Lifestyle |
13125 |
0.99 |
759 |
Dom-zoom |
Travel |
21379 |
0.0 |
765 |
Trippletouch |
Reference |
21905 |
2.99 |
779 |
Kinice |
Navigation |
22277 |
1.99 |
782 |
Latzap |
Medical |
1902 |
0.0 |
784 |
Jaytexon |
Finance |
8151 |
2.99 |
785 |
rankdom |
Weather |
23701 |
0.0 |
799 |
Hexgreen |
Catalogs |
10206 |
0.0 |
819 |
Mediatom |
Travel |
30990 |
0.0 |
821 |
Labtechno |
Lifestyle |
15895 |
2.99 |
822 |
Yearhottom |
Business |
15753 |
0.99 |
831 |
Can-taxon |
Food & Drink |
4590 |
0.0 |
834 |
Unaflex |
Health & Fitness |
16917 |
2.99 |
839 |
Labphase |
Lifestyle |
30799 |
0.0 |
841 |
ontocode |
Games |
13871 |
2.99 |
850 |
Zimtrax |
Health & Fitness |
14941 |
0.99 |
859 |
Conebase |
Sports |
1923 |
1.99 |
863 |
Tintonway |
Books |
21242 |
1.99 |
865 |
mathtouch |
Music |
5895 |
1.99 |
870 |
Toughlab |
Books |
1731 |
0.0 |
883 |
Zer-tom |
Navigation |
10545 |
1.99 |
885 |
Zer-zim |
Business |
17204 |
0.0 |
886 |
opecone |
Music |
20967 |
1.99 |
904 |
Hexquadline |
Productivity |
10212 |
0.99 |
908 |
Trusting |
Photo & Video |
24652 |
1.99 |
913 |
Anholding |
Photo & Video |
9573 |
0.0 |
925 |
Solotrax |
Education |
28916 |
0.0 |
934 |
J-electronics |
Travel |
25921 |
0.99 |
945 |
Freezoom |
Books |
13207 |
1.99 |
946 |
Biotex |
Sports |
14313 |
0.99 |
947 |
Singlelam |
Finance |
18117 |
1.99 |
949 |
Stripplex |
Lifestyle |
7699 |
3.99 |
959 |
Lathouse |
Travel |
31090 |
0.0 |
964 |
Quoace |
Books |
22122 |
0.0 |
972 |
Villatouch |
Weather |
17085 |
1.99 |
973 |
Roundhow |
Photo & Video |
10662 |
0.0 |
975 |
Groovelab |
Catalogs |
9496 |
0.0 |
977 |
Plexlane |
Utilities |
18050 |
0.0 |
982 |
Zotice |
Catalogs |
20062 |
1.99 |
985 |
tonkix |
Games |
19498 |
0.0 |
992 |
Acecane |
Entertainment |
13359 |
0.0 |
1000 |
Plusvolzim |
News |
2.SELECTCOUNT (*) FROM fake_apps;
3. QueryResults
COUNT (*) |
200 |
4. DatabaseSchema
fake_apps 200 rows |
|
id |
INTEGER |
name |
TEXT |
category |
TEXT |
downloads |
INTEGER |
price |
REAL |
第二十四課、count
AGGREGATE FUNCTIONS Count
SELECT COUNT(*) FROM fake_apps;
The fastest way to calculatethe number of rows in a table is to use the COUNT
()
function.
COUNT()
is
a function that takes thename of a column as an argument and counts the number of rows where the columnis not NULL
.
Here, we want to count every rowso we pass *
as
an argument.
1.Use COUNT()
to
return the total number of free apps in the table. In thecode editor type
SELECT COUNT(*) FROM fake_apps WHERE price = 0;
2.Count the number of apps at eachprice. Remove the previous query. Then type
SELECT price, COUNT(*) FROM fake_apps GROUP BY price;
1. SELECTCOUNT (*) FROM fake_apps
WHERE price = 0 ;
QueryResults
COUNT (*) |
73 |
DatabaseSchema
fake_apps 200 rows |
|
id |
INTEGER |
name |
TEXT |
category |
TEXT |
downloads |
INTEGER |
price |
REAL |
2. SELECTprice,COUNT (*) FROM fake_apps
Group by price;
QueryResults
price |
COUNT (*) |
0.0 |
73 |
0.99 |
43 |
1.99 |
42 |
2.99 |
21 |
3.99 |
9 |
14.99 |
12 |
DatabaseSchema
fake_apps 200 rows |
|
id |
INTEGER |
name |
TEXT |
category |
TEXT |
downloads |
INTEGER |
price |
REAL |
第二十五課、Group By
AGGREGATE FUNCTIONS Group By
SELECT price, COUNT(*)
FROM fake_apps
GROUP BY price;
Aggregate functions are more useful when they organize data intogroups.
GROUP BY
is
a clause in SQL that isonly used with aggregate functions. It is used in collaboration with the SELECT
statement
to arrangeidentical data into groups.
Here, our aggregate function is COUNT()
and
we are passing price
as
an argument to GROUP BY
.
SQL will count the total number of apps for each price
in
the table.
It is usually helpful to SELECT
the
column you pass as anargument to GROUP BY
.
Here we select price
and COUNT(*)
.
You can seethat the result set is organized into two columns making it easy to see thenumber of apps at each price.
1.Count the total number of appsat each price that have been downloaded more than 20,000 times. In the codeeditor type
SELECT price, COUNT(*)
FROM fake_apps
WHERE downloads > 20000 GROUP BY price;
2.What is the total number ofdownloads for all of the apps combined? Remove the previous query, then type
SELECT SUM(downloads)
FROM fake_apps;
1. SELECT price,COUNT(*) FROM fake_apps
WHEREdownloads>20000 GROUP BY price;
QueryResults
price |
COUNT (*) |
0.0 |
26 |
0.99 |
17 |
1.99 |
18 |
2.99 |
7 |
3.99 |
5 |
14.99 |
5 |
DatabaseSchema
fake_apps200 rows |
|
id |
INTEGER |
name |
TEXT |
category |
TEXT |
downloads |
INTEGER |
price |
REAL |
2.SELECT SUM(downloads)
FROMfake_apps;
QueryResults
SUM(downloads) |
3322760 |
DatabaseSchema
fake_apps 200 rows |
|
id |
INTEGER |
name |
TEXT |
category |
TEXT |
downloads |
INTEGER |
price |
REAL |
第二十六課、Sum
AGGREGATE FUNCTIONS Sum
SELECT SUM(downloads)
FROM fake_apps;
SQL makes it easy to add allvalues in a particular column using SUM()
.
SUM()
is
a function that takes thename of a column as an argument and returns the sum of all the values in thatcolumn. Here, it adds all the values in the downloads
column.
1.Calculate the total number ofdownloads for each category. In the code editor type
SELECT category,SUM(downloads)
FROM fake_appsGROUP BY category;
2.How many downloads does the mostpopular app have? Remove the previous query then type
SELECTMAX(downloads)
FROMfake_apps;
-
SELECT category ,SUM(downloads)
FROM fake_apps GROUPBY category;
QueryResults
category |
SUM(downloads) |
Books |
160864 |
Business |
178726 |
Catalogs |
186158 |
Education |
184724 |
Entertainment |
95168 |
Finance |
178163 |
Food & Drink |
90950 |
Games |
256083 |
Health & Fitness |
165555 |
Lifestyle |
166832 |
Medical |
77191 |
Music |
59367 |
Navigation |
152114 |
News |
103259 |
Photo & Video |
184848 |
Productivity |
117811 |
Reference |
162032 |
Social Networking |
126549 |
Sports |
176988 |
Travel |
242116 |
Utilities |
96099 |
Weather |
161163 |
fake_apps 200 rows |
|
id |
INTEGER |
name |
TEXT |
category |
TEXT |
downloads |
INTEGER |
price |
REAL |
2. SELECTMAX(downloads)
FROMfake_apps ;
QueryResults
MAX(downloads) |
31090 |
DatabaseSchema
fake_apps 200 rows |
|
id |
INTEGER |
name |
TEXT |
category |
TEXT |
downloads |
INTEGER |
price |
REAL |