-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathaggregate_functions.sql
More file actions
162 lines (135 loc) · 4.42 KB
/
aggregate_functions.sql
File metadata and controls
162 lines (135 loc) · 4.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
-- Aggregate functions
-- COUNT returns the number of rows
SELECT COUNT(*) FROM books; -- returns the number of rows in our table
-- Counts every value that is present in column author_fname
INSERT INTO books () VALUES (); -- adding NULL - 21 rows in total
SELECT COUNT(author_fname) FROM books; -- returns 19, it does not count NULLs
SELECT COUNT(DISTINCT author_fname) FROM books;
SELECT COUNT(DISTINCT released_year) FROM books;
-- How many titles contain "the"?
SELECT COUNT(*) AS titles_with_the
FROM books
WHERE title LIKE '%the%';
SELECT DISTINCT title FROM books;
-- GROUP BY
-- It summarizes or aggregates identical data into single rows
SELECT author_lname FROM books
GROUP BY author_lname;
-- COUNT how many books each author has written
-- counting records for each group
SELECT author_lname, COUNT(title) AS books_written
FROM books
GROUP BY author_lname
ORDER BY books_written DESC;
-- COUNT books by realeased year
-- we cannot select a column that is not used in GROUP BY
-- and any aggregate function is not performed on.
SELECT released_year, COUNT(title) AS books_released -- we cannot select title - error
FROM books
GROUP BY released_year
ORDER BY books_released DESC;
-- MIN and MAX
-- return minimum and maximum value respectively
-- work both with numbers and text
SELECT MIN(released_year) AS min_year, MAX(released_year) AS max_year
FROM books;
-- What if I want the title of the longest book?
-- We can use ORDER BY pages DESC and LIMIT 1
SELECT
title, pages
FROM
books
ORDER BY pages DESC
LIMIT 1;
SELECT MAX(pages) FROM books;
-- We can also use subquery
-- Subquery is a query inside another query, it is in ()
-- If there are many rows containing the maximum value
-- The solution with subquery will return all these records
-- and the one with ORDER BY and LIMIT will return only one.
SELECT
title, pages
FROM
books
WHERE
pages = (SELECT
MAX(pages)
FROM
books);
-- Find the book that was realeased the earliest
SELECT
title, released_year
FROM
books
WHERE
released_year = (SELECT
MIN(released_year)
FROM
books);
-- We can GROUP BY multiple columns
SELECT author_lname, author_fname, COUNT(title) AS books_written
FROM books
GROUP BY author_lname, author_fname
ORDER BY books_written DESC;
-- In MySQL we can use aliases from SELECT in GROUP BY
SELECT CONCAT(author_fname, ' ', author_lname) AS author, COUNT(title) AS books_written
FROM books
GROUP BY author;
-- Find the year each author published their first book,
-- their latest book and count how many books they have written
SELECT
CONCAT(author_fname, ' ', author_lname) AS author,
MIN(released_year) AS earliest_release,
MAX(released_year) AS latest_release,
COUNT(title) AS books_written
FROM
books
GROUP BY author;
SELECT CONCAT(author_fname, ' ', author_lname) AS author, MAX(pages) AS max_pages
FROM books
GROUP BY author;
-- SUM
-- sums values together
SELECT SUM(pages) AS all_pages FROM books;
-- Sum pages by authors
SELECT author_lname, author_fname, SUM(pages) AS pages_written
FROM books
GROUP by author_lname, author_fname
ORDER BY pages_written DESC;
-- AVG
-- calculates the average
SELECT AVG(released_year) FROM books;
SELECT AVG(pages) FROM books;
-- Calculate the average stock quantity for books released in the same year
SELECT released_year, ROUND(AVG(stock_quantity), 0) AS avg_stock
FROM books
GROUP BY released_year;
-- STD
-- calculates the standard deviation
SELECT STD(pages) FROM books;
-- Print the number of books in the database
SELECT COUNT(title) AS number_of_books FROM books;
-- Print out how many books were released in each year
SELECT released_year, COUNT(title) AS books_released
FROM books
GROUP BY released_year;
-- Print out the total number of books in stock
SELECT SUM(stock_quantity) AS books_in_stock FROM books;
-- Find the average released_year for each author
SELECT
CONCAT(author_fname, ' ', author_lname) AS author,
AVG(released_year) AS average_release_year
FROM
books
GROUP BY author;
-- Find the full name of the author who wrote the longest book
SELECT CONCAT(author_fname, ' ', author_lname) AS author
FROM books
WHERE pages = (SELECT MAX(pages) FROM books);
-- For reach released_year print the number of books and the average number of pages, order by year
SELECT released_year AS year,
COUNT(title) AS '#_books',
AVG(pages) AS avg_pages
FROM books
GROUP BY year
ORDER BY year;