Skip to content

Commit 14e714c

Browse files
committed
add datatypes and logical operators
1 parent 7177a92 commit 14e714c

File tree

4 files changed

+546
-128
lines changed

4 files changed

+546
-128
lines changed

Diff for: 09. Aggregate Functions.sql

-127
This file was deleted.

Diff for: Ultimate MySQL/09. Aggregate Functions.sql

+114-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,117 @@ FROM books;
1111

1212
/*How many titles contain "the"?*/
1313
SELECT COUNT(*) FROM books
14-
WHERE title LIKE '%the%'
14+
WHERE title LIKE '%the%';
15+
16+
/*COUNT how many books each author has written*/
17+
SELECT CONCAT(author_fname,' ',author_lname) AS 'author name',COUNT(*) AS 'number of books'
18+
FROM books
19+
GROUP BY 1
20+
ORDER BY 2 DESC;
21+
22+
23+
/*books count by released years*/
24+
SELECT released_year, COUNT(*) AS 'number of books'
25+
FROM books
26+
GROUP BY released_year
27+
ORDER BY released_year DESC;
28+
29+
30+
/*Find the minimum released_year*/
31+
SELECT MIN(released_year) FROM books;
32+
33+
/*Find the longest book (but took long as 2 quries have to run)*/
34+
SELECT *
35+
FROM books
36+
WHERE pages = (SELECT MAX(pages) FROM books);
37+
38+
/* Faster way */
39+
SELECT * FROM books
40+
ORDER BY pages DESC
41+
LIMIT 1;
42+
43+
44+
/*Find the year each author published their first book*/
45+
SELECT author_fname,author_lname,MIN(released_year)
46+
FROM books
47+
GROUP BY 1,2;
48+
49+
/*Find the longest page count for each author*/
50+
SELECT author_fname,author_lname,MAX(pages)
51+
FROM books
52+
GROUP BY 1,2;
53+
54+
55+
/*Sum all pages in the entire database*/
56+
SELECT SUM(pages) FROM books;
57+
58+
/*Sum all pages each author has written*/
59+
SELECT author_fname,author_lname,SUM(pages)
60+
FROM books
61+
GROUP BY author_fname,author_lname;
62+
63+
64+
/*Calculate the average released_year across all books*/
65+
SELECT AVG(released_year) FROM books;
66+
67+
68+
/*Calculate the average stock quantity for books released in the same year*/
69+
SELECT released_year, AVG(stock_quantity)
70+
FROM books
71+
GROUP BY released_year;
72+
73+
/*-----------------------------------------------------------*/
74+
75+
/*-------------- Challenges --------------------------------*/
76+
/*Print the number of books in the database*/
77+
SELECT COUNT(*) AS 'number of books' FROM books;
78+
79+
80+
/*Print out how many books were released in each year*/
81+
SELECT released_year, COUNT(*) as 'number of books'
82+
FROM books
83+
GROUP BY released_year;
84+
85+
86+
/*Print out the total number of books in stock*/
87+
SELECT SUM(stock_quantity) AS 'total number books in stock'
88+
FROM books;
89+
90+
91+
/*Find the average released_year for each author*/
92+
SELECT author_fname,author_lname,AVG(released_year)
93+
FROM books
94+
GROUP BY author_fname,author_lname;
95+
96+
97+
/*Find the full name of the author who wrote the longest book*/
98+
SELECT CONCAT(author_fname,' ',author_lname) AS 'Author Full Name', title, pages
99+
FROM books
100+
ORDER BY pages DESC
101+
LIMIT 1;
102+
103+
/*
104+
+------+---------+-----------+
105+
| year | # books | avg pages |
106+
+------+---------+-----------+
107+
| 1945 | 1 | 181.0000 |
108+
| 1981 | 1 | 176.0000 |
109+
| 1985 | 1 | 320.0000 |
110+
| 1989 | 1 | 526.0000 |
111+
| 1996 | 1 | 198.0000 |
112+
| 2000 | 1 | 634.0000 |
113+
| 2001 | 3 | 443.3333 |
114+
| 2003 | 2 | 249.5000 |
115+
| 2004 | 1 | 329.0000 |
116+
| 2005 | 1 | 343.0000 |
117+
| 2010 | 1 | 304.0000 |
118+
| 2012 | 1 | 352.0000 |
119+
| 2013 | 1 | 504.0000 |
120+
| 2014 | 1 | 256.0000 |
121+
| 2016 | 1 | 304.0000 |
122+
| 2017 | 1 | 367.0000 |
123+
+------+---------+-----------+
124+
*/
125+
SELECT released_year AS 'year' ,COUNT(*) AS '# books', AVG(pages) AS 'avg pages'
126+
FROM books
127+
GROUP BY released_year;

0 commit comments

Comments
 (0)