-
Notifications
You must be signed in to change notification settings - Fork 3
/
14_CodeWars.sql
61 lines (29 loc) · 1.06 KB
/
14_CodeWars.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
-- Question: Given a demographics table in the following format:
-- ** demographics table schema **
-- id
-- name
-- birthday
-- race
-- you need to return a table that shows a count of each race represented, ordered by the count in descending order as:
-- Solution
SELECT RACE, COUNT(ID) AS COUNT FROM DEMOGRAPHICS
GROUP BY RACE
ORDER BY COUNT DESC;
-- Question: Return a table with a single column named Greeting with the phrase 'hello world!'
-- Solution
CREATE TABLE greetings (Greeting VARCHAR(12));
INSERT INTO greetings (Greeting)
VALUES ('hello world!');
SELECT Greeting FROM greetings
WHERE Greeting = 'hello world!';
-- Question: You work at a book store. It's the end of the month, and you need to find out the 5 bestselling books at your store.
-- Use a select statement to list names, authors, and number of copies sold of the 5 books which were sold most.
-- books table schema
-- name
-- author
-- copies_sold
-- Solution
SELECT name, author, count(copies_sold) as "count" FROM books
GROUP BY name, author
ORDER BY "count" DESC
LIMIT 5;