|
| 1 | +/****** |
| 2 | +
|
| 3 | +Khan Academy SQL course exercises |
| 4 | +
|
| 5 | +https://www.khanacademy.org/computing/computer-programming/sql |
| 6 | +
|
| 7 | +******/ |
| 8 | + |
| 9 | +/* Books table */ |
| 10 | + |
| 11 | +CREATE TABLE books ( |
| 12 | + id integer PRIMARY key, |
| 13 | + name text, |
| 14 | + rating integer |
| 15 | +); |
| 16 | + |
| 17 | +insert into books values (1, 'Harry Potter', 5); |
| 18 | +insert into books values (2, 'Chronicles of Narnia', 4); |
| 19 | +insert into books values (3, 'The Martian', null); |
| 20 | + |
| 21 | + |
| 22 | +/* Groceries table */ |
| 23 | + |
| 24 | +SELECT * FROM groceries WHERE aisle > 5 ORDER BY aisle; |
| 25 | + |
| 26 | +SELECT aisle, SUM(quantity) FROM groceries GROUP BY aisle; |
| 27 | + |
| 28 | +/* Online store */ |
| 29 | + |
| 30 | +create table bikes ( |
| 31 | + id integer primary key, |
| 32 | + name text, |
| 33 | + quantity integer, |
| 34 | + price numeric, |
| 35 | + category text |
| 36 | +); |
| 37 | + |
| 38 | +insert into bikes values (1, 'Nimbus 2000', 10, 500.00, 'Mountain'); |
| 39 | +insert into bikes values (2, 'Firebolt', 5, 599.99, 'Racing'); |
| 40 | +insert into bikes values (3, 'RoadRacer', 1, 445.50, 'Racing'); |
| 41 | +insert into bikes values (4, 'MountainClimber', 6, 330.00, 'Mountain'); |
| 42 | +insert into bikes values (5, 'Beach Cruiser', 12, 220, 'Cruiser'); |
| 43 | + |
| 44 | +select category from bikes group by category order by quantity desc; |
| 45 | + |
| 46 | +select * from bikes where category = 'Mountain' order by price desc; |
| 47 | +select * from bikes where category = 'Racing' order by price desc; |
| 48 | +select * from bikes where category = 'Cruiser' order by price desc; |
| 49 | + |
| 50 | +select sum(quantity) from bikes; |
| 51 | + |
| 52 | +/* Karaoke selector */ |
| 53 | + |
| 54 | +select title from songs; |
| 55 | +select title from songs where released > 1990 or mood = 'epic'; |
| 56 | +select title from songs where released > 1990 and mood = 'epic' and duration < 240; |
| 57 | + |
| 58 | +/* Exercise logs */ |
| 59 | + |
| 60 | +create table exercise_logs ( |
| 61 | + id integer primary key autoincrement, |
| 62 | + type, text, minutes integer, |
| 63 | + calories integer, |
| 64 | + heart_rate integer |
| 65 | +); |
| 66 | +create table drs_favorites ( |
| 67 | + id integer primary key, |
| 68 | + type text, |
| 69 | + reason text |
| 70 | +); |
| 71 | + |
| 72 | +insert into exercise_logs(type, minutes, calories, heart_rate) values ('biking', 30, 100, 110); |
| 73 | +insert into drs_favorites(type, reason) values ('biking', 'improves endurance and flexibility.'); |
| 74 | + |
| 75 | +/* selects all activities that the user did that are recommended by doctor for cardio health */ |
| 76 | +select * from exercise_logs where type in (select type from drs_favorites where reason like "%cardiovascular%"); |
| 77 | + |
| 78 | +/* table of all types of activity and the total amount of calories burned by doing that activity */ |
| 79 | +select type, sum(calories) as total_calories from exercise_logs group by type; |
| 80 | + |
| 81 | +select type, sum(calories) as total_calories |
| 82 | +from exercise_logs |
| 83 | +group by type |
| 84 | +having total_calories > 150; /* having applies to the grouped condition */ |
| 85 | + |
| 86 | +/* 50-90% of max heart rate for activity */ |
| 87 | +select count(*) from exercise_logs |
| 88 | +where heart_rate >= round(.5 * (220 -30)) |
| 89 | +and heart_rate <= round(.9 * (220 - 30)); |
| 90 | + |
| 91 | +/* add column for activity's target heart rate status */ |
| 92 | +select type, heart_rate, |
| 93 | + case |
| 94 | + when heart_rate > 220 - 30 then 'above max' |
| 95 | + when heart_rate > round(.9 * (220 - 30)) then 'above target' |
| 96 | + when heart_rate > round(.5 * (220 - 30)) then 'within target' |
| 97 | + else 'below target' |
| 98 | + end as 'heart_rate_zone' |
| 99 | +from exercise_logs; |
| 100 | + |
| 101 | +/* Group the count of exercises by each heart rate zone */ |
| 102 | +select count(*), |
| 103 | + case |
| 104 | + when heart_rate > 220 - 30 then 'above max' |
| 105 | + when heart_rate > round(.9 * (220 - 30)) then 'above target' |
| 106 | + when heart_rate > round(.5 * (220 - 30)) then 'within target' |
| 107 | + else 'below target' |
| 108 | + end as 'heart_rate_zone' |
| 109 | +from exercise_logs |
| 110 | +group by heart_rate_zone; |
| 111 | + |
| 112 | +/* Playlist maker */ |
| 113 | + |
| 114 | +select title from songs where artist in (select name from artists where genre = 'Pop'); |
| 115 | + |
| 116 | +/* Wordiest author */ |
| 117 | + |
| 118 | +/* from books, author that has written more than a million words */ |
| 119 | +select author, sum(words) as total_words from books group by author having total_words > 1000000; |
| 120 | + |
| 121 | +/* author whose average book is over 150,000 words */ |
| 122 | +select author, avg(words) as avg_words from books group by author having avg_words > 150000; |
| 123 | + |
| 124 | +/* Gradebook */ |
| 125 | + |
| 126 | +CREATE TABLE student_grades ( |
| 127 | + id INTEGER PRIMARY KEY AUTOINCREMENT, |
| 128 | + name TEXT, |
| 129 | + number_grade INTEGER, |
| 130 | + fraction_completed REAL); |
| 131 | + |
| 132 | +INSERT INTO student_grades (name, number_grade, fraction_completed) |
| 133 | + VALUES ("Winston", 90, 0.805); |
| 134 | +INSERT INTO student_grades (name, number_grade, fraction_completed) |
| 135 | + VALUES ("Winnefer", 95, 0.901); |
| 136 | +INSERT INTO student_grades (name, number_grade, fraction_completed) |
| 137 | + VALUES ("Winsteen", 85, 0.906); |
| 138 | +INSERT INTO student_grades (name, number_grade, fraction_completed) |
| 139 | + VALUES ("Wincifer", 66, 0.7054); |
| 140 | +INSERT INTO student_grades (name, number_grade, fraction_completed) |
| 141 | + VALUES ("Winster", 76, 0.5013); |
| 142 | +INSERT INTO student_grades (name, number_grade, fraction_completed) |
| 143 | + VALUES ("Winstonia", 82, 0.9045); |
| 144 | + |
| 145 | +select name, number_grade, round(100.00 * fraction_completed) as percent_completed from student_grades; |
| 146 | + |
| 147 | +/* how many students have earned which letter grade */ |
| 148 | +select count(number_grade), |
| 149 | + case |
| 150 | + when number_grade > 90 then 'A' |
| 151 | + when number_grade > 80 then 'B' |
| 152 | + when number_grade > 70 then 'C' |
| 153 | + else 'F' |
| 154 | + end as 'letter_grade' |
| 155 | +from student_grades |
| 156 | +group by letter_grade; |
| 157 | + |
| 158 | +/* Marvel characters database |
| 159 | +https://gist.github.com/pamelafox/585364b62390ea720858 |
| 160 | +*/ |
| 161 | + |
| 162 | +/* Marvel Heroes and Villains |
| 163 | + Based on the website http://marvel.wikia.com/Main_Page |
| 164 | + with popularity data from http://observationdeck.io9.com/something-i-found-marvel-character-popularity-poll-cb-1568108064 |
| 165 | + and power grid data from http://marvel.wikia.com/Power_Grid#Power |
| 166 | + Collected by: https://www.khanacademy.org/profile/Mentrasto/ |
| 167 | + */ |
| 168 | + |
| 169 | +CREATE TABLE marvels (ID INTEGER PRIMARY KEY, |
| 170 | + name TEXT, |
| 171 | + popularity INTEGER, |
| 172 | + alignment TEXT, |
| 173 | + gender TEXT, |
| 174 | + height_m NUMERIC, |
| 175 | + weight_kg NUMERIC, |
| 176 | + hometown TEXT, |
| 177 | + intelligence INTEGER, |
| 178 | + strength INTEGER, |
| 179 | + speed INTEGER, |
| 180 | + durability INTEGER, |
| 181 | + energy_Projection INTEGER, |
| 182 | + fighting_Skills INTEGER); |
| 183 | + |
| 184 | +INSERT INTO marvels VALUES(1, "Spider Man", 1, "Good", "Male", 1.78, 75.75, "USA", 4, 4, 3, 3, 1, 4); |
| 185 | +INSERT INTO marvels VALUES(2, "Iron Man", 20, "Neutral", "Male", 1.98, 102.58, "USA", 6, 6, 5, 6, 6, 4); |
| 186 | +INSERT INTO marvels VALUES(3, "Hulk", 18, "Neutral", "Male", 2.44, 635.29, "USA", 6, 7, 3, 7, 5, 4); |
| 187 | +INSERT INTO marvels VALUES(4, "Wolverine", 3, "Good", "Male", 1.6, 88.46, "Canada", 2, 4, 2, 4, 1, 7); |
| 188 | +INSERT INTO marvels VALUES(5, "Thor", 5, "Good", "Male", 1.98, 290.3, "Norway", 2, 7, 7, 6, 6, 4); |
| 189 | +INSERT INTO marvels VALUES(6, "Green Goblin", 91, "Bad", "Male", 1.93, 174.63, "USA", 4, 4, 3, 4, 3, 3); |
| 190 | +INSERT INTO marvels VALUES(7, "Magneto", 11, "Neutral", "Male", 1.88, 86.18, "Germany", 6, 3, 5, 4, 6, 4); |
| 191 | +INSERT INTO marvels VALUES(8, "Thanos", 47, "Bad", "Male", 2.01, 446.79, "Titan", 6, 7, 7, 6, 6, 4); |
| 192 | +INSERT INTO marvels VALUES(9, "Loki", 32, "Bad", "Male", 1.93, 238.14, "Jotunheim", 5, 5, 7, 6, 6, 3); |
| 193 | +INSERT INTO marvels VALUES(10, "Doctor Doom", 19, "Bad", "Male", 2.01, 188.24, "Latveria", 6, 4, 5, 6, 6, 4); |
| 194 | +INSERT INTO marvels VALUES(11, "Jean Greay", 8, "Good", "Female", 1.68, 52.16, "USA", 3, 2, 7, 7, 7, 4); |
| 195 | +INSERT INTO marvels VALUES(12, "Rogue", 4, "Good", "Female", 1.73, 54.43, "USA", 7, 7, 7, 7, 7, 7); |
| 196 | + |
| 197 | +/* most popular and least popular */ |
| 198 | +select name, min(popularity) from marvels; |
| 199 | +select name, max(popularity) from marvels; |
| 200 | + |
| 201 | + |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + |
0 commit comments