This repository has been archived by the owner on Apr 11, 2021. It is now read-only.
forked from KATRINAHIGH/sql-khan-academy-challenges
-
Notifications
You must be signed in to change notification settings - Fork 0
/
challenge7_wordiest_author.sql
59 lines (50 loc) · 2.37 KB
/
challenge7_wordiest_author.sql
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
/*We've created a database of a few popular authors and their books, with word counts
for each book.*/
CREATE TABLE books (
id INTEGER PRIMARY KEY AUTOINCREMENT,
author TEXT,
title TEXT,
words INTEGER);
INSERT INTO books (author, title, words)
VALUES ("J.K. Rowling", "Harry Potter and the Philosopher's Stone", 79944);
INSERT INTO books (author, title, words)
VALUES ("J.K. Rowling", "Harry Potter and the Chamber of Secrets", 85141);
INSERT INTO books (author, title, words)
VALUES ("J.K. Rowling", "Harry Potter and the Prisoner of Azkaban", 107253);
INSERT INTO books (author, title, words)
VALUES ("J.K. Rowling", "Harry Potter and the Goblet of Fire", 190637);
INSERT INTO books (author, title, words)
VALUES ("J.K. Rowling", "Harry Potter and the Order of the Phoenix", 257045);
INSERT INTO books (author, title, words)
VALUES ("J.K. Rowling", "Harry Potter and the Half-Blood Prince", 168923);
INSERT INTO books (author, title, words)
VALUES ("J.K. Rowling", "Harry Potter and the Deathly Hallows", 197651);
INSERT INTO books (author, title, words)
VALUES ("Stephenie Meyer", "Twilight", 118501);
INSERT INTO books (author, title, words)
VALUES ("Stephenie Meyer", "New Moon", 132807);
INSERT INTO books (author, title, words)
VALUES ("Stephenie Meyer", "Eclipse", 147930);
INSERT INTO books (author, title, words)
VALUES ("Stephenie Meyer", "Breaking Dawn", 192196);
INSERT INTO books (author, title, words)
VALUES ("J.R.R. Tolkien", "The Hobbit", 95022);
INSERT INTO books (author, title, words)
VALUES ("J.R.R. Tolkien", "Fellowship of the Ring", 177227);
INSERT INTO books (author, title, words)
VALUES ("J.R.R. Tolkien", "Two Towers", 143436);
INSERT INTO books (author, title, words)
VALUES ("J.R.R. Tolkien", "Return of the King", 134462);
/*select all the authors who have written more
than 1 million words, using GROUP BY and HAVING. Your results table should
include the 'author' and their total word count as a 'total_words' column.*/
SELECT author, SUM(words) AS total_words
FROM books GROUP BY author
HAVING total_words > 1000000;
/*Select all the authors that write more than an average of 150,000 words per book.
Your results table should include the 'author' and average words as an 'avg_words'
column.*/
SELECT author, AVG(words) AS avg_words
FROM books
GROUP BY author
HAVING avg_words > 150000;