-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagg.sql
More file actions
executable file
·84 lines (62 loc) · 2.2 KB
/
agg.sql
File metadata and controls
executable file
·84 lines (62 loc) · 2.2 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
/*
Simplified Library Database
CREATE TABLE books
(
book_id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(100),
author_fname VARCHAR(100),
author_lname VARCHAR(100),
released_year INT,
stock_quantity INT,
pages INT,
PRIMARY KEY(book_id)
);
*/
-----------------------------------------------------------------------
/*
1.Print the number of books in the database.
*/
SELECT COUNT(*) AS "Number of books" FROM books;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
2.Print out how many books were released in each year
*/
SELECT released_year AS "Release Year", COUNT(*) AS "Number of books" FROM books
GROUP BY released_year;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
3.Print out the total number of books in stock
*/
SELECT SUM(stock_quantity) AS "Total books in stock" FROM books;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
4.Find the average released_year for each author
*/
SELECT CONCAT (author_fname," ", author_lname) AS "Author name",
AVG(released_year) AS "Average year of release" FROM books
GROUP BY author_fname, author_lname;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
5.Find the full name of the author who wrote the longest book
*/
SELECT
CONCAT(
author_fname,
" ",
author_lname
) FROM books
ORDER BY pages LIMIT 1;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
6.Print out for each year, the number of released books and the
average number of pages
*/
SELECT released_year AS 'year', COUNT(*) AS ' # books', AVG(pages) AS 'avg pages'
FROM books
GROUP BY released_year;
-----------------------------------------------------------------------