-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqueries-sql-books.sql
More file actions
77 lines (60 loc) · 2.03 KB
/
queries-sql-books.sql
File metadata and controls
77 lines (60 loc) · 2.03 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
-- running the queries outlined in "sql-project_dataset and sql query questions.pdf" which can be found in this repo
USE books;
SELECT COUNT(*) AS column_count
FROM INFORMATION_SCHEMA.COLUMNS
WHERE TABLE_NAME = 'books_sql_4';
SELECT COUNT(*) AS row_count FROM books_sql_4;
-- =======================================
-- 1. GENERAL QUESTIONS
-- =======================================
-- a) How many pages do the books have on average?
SELECT ROUND(AVG(pages), 0)
FROM books_sql_4;
-- b) Which languages are the books in?
SELECT DISTINCT(language)
FROM books_sql_4;
-- c) Which genres are there?
SELECT DISTINCT(genre)
FROM books_sql_4;
-- d) Which type of binding do the books have?
SELECT DISTINCT(bookFormat)
FROM books_sql_4;
-- e) What is the average rating of the books?
SELECT ROUND(AVG(rating), 2) as avg_rating
FROM books_sql_4;
-- f) What is the highest price of a book?
SELECT MAX(rating) as max_rating
FROM books_sql_4;
-- =======================================
-- 2. QUESTIONS REGARDING AUTHORS
-- =======================================
-- a) Which authors have more than one book?
SELECT main_author, COUNT(title) as title_count
FROM books_sql_4
GROUP BY main_author
HAVING title_count > 1
ORDER BY title_count DESC;
-- b) Which author wrote the book with the highest rating?
SELECT main_author, title, MAX(rating) as max_rating
FROM books_sql_4
GROUP BY main_author, title
ORDER BY max_rating DESC
LIMIT 1;
-- =======================================
-- 3. QUESTIONS ON HIGHLY RATED BOOKS
-- =======================================
-- a) How many pages does the book with the highest rating have?
SELECT pages, title, rating
FROM books_sql_4
WHERE rating = (SELECT MAX(rating) FROM books_sql_4);
-- b) Which genres do the book with a rating higher than 4 have?
SELECT genre, title, rating
FROM books_sql_4
WHERE rating > 4
ORDER BY genre ASC;
-- c) What are the five leading books in the “Best Books Ever” list?
SELECT main_author, title, MAX(bbeScore) as best_ever
FROM books_sql_4
GROUP BY main_author, title
ORDER BY best_ever DESC
LIMIT 5;