-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise 08 - Operators and Cases.sql
More file actions
60 lines (45 loc) · 1.62 KB
/
Exercise 08 - Operators and Cases.sql
File metadata and controls
60 lines (45 loc) · 1.62 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
-- Exercise 8 - Operators and Cases
/*-----------------------------------------*/
-- Select all Books written before 1980
SELECT title
FROM books
WHERE released_year < 1980;
/*-----------------------------------------*/
-- Select all Books written by Eggers or Chabon
SELECT title
FROM books
WHERE author_lname IN ('Eggers', 'Chabon');
/*-----------------------------------------*/
-- Select all Books written by Lahiri after the year 2000
SELECT title
FROM books
WHERE author_lname = 'Lahiri' AND released_year > 2000;
/*-----------------------------------------*/
-- Select all Books with a page count between 100 and 200
SELECT title
FROM books
WHERE pages BETWEEN 100 AND 200;
/*-----------------------------------------*/
-- Select all Books where authors last name starts with an L or an S
SELECT title
FROM books
WHERE author_lname LIKE 'L%' OR 'S%';
/*-----------------------------------------*/
-- Create another column output: if title conatins 'stories' list it as Short Stories, Just Kids and a heartbreaking work... as Memoir, everything else: Novel
SELECT title, author_lname,
CASE
WHEN title LIKE '%stories%' THEN 'Short Stories'
WHEN title = 'Just Kids' THEN 'Memoir'
WHEN title = 'A Heartbreaking Work of Staggering Genius' THEN 'Memoir'
ELSE 'Novel'
END AS type
FROM books;
/*-----------------------------------------*/
-- Count the number of books an author has written and list it as x books in a seperate column
SELECT author_fname, author_lname,
CASE
WHEN COUNT(*) = 1 THEN '1 book'
ELSE CONCAT(COUNT(*), ' ', 'books')
END AS books_written
FROM books
GROUP BY author_fname, author_lname;