-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlog_opp.sql
More file actions
executable file
·88 lines (65 loc) · 2.4 KB
/
log_opp.sql
File metadata and controls
executable file
·88 lines (65 loc) · 2.4 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
78
79
80
81
82
83
84
85
86
87
/*
Simplified Library Database
CREATE TABLE books
(
book_id INT NOT NULL AUTO_INCREMENT,
title VARCHAR(100),
author_fname VARCHAR(100),
author_lname VARCHAR(100),
released_year INT,
stock_quantity INT,
pages INT,
PRIMARY KEY(book_id)
);
*/
-----------------------------------------------------------------------
-- 1. Select All Books Written Before 1980 (non inclusive)
SELECT title, released_year FROM books
WHERE released_year < 1980;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-- 2.Select All Books Written By Eggers Or Chabon
SELECT title FROM books
WHERE author_lname = "Eggers" || author_lname = "Chabon";
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-- 3.Select All Books Written By Lahiri, Published after 2000
SELECT title FROM books
WHERE author_lname = "Lahiri" && released_year> 2000;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-- 4.Select All books with page counts between 100 and 200
SELECT title, pages FROM books
WHERE pages BETWEEN 100 AND 200;
-----------------------------------------------------------------------
-----------------------------------------------------------------------
-- 5.Select all books where author_lname starts with a 'C' or an 'S''
SELECT title, author_lname FROM books
WHERE author_lname LIKE "C%" OR author_lname LIKE "S%";
-----------------------------------------------------------------------
-----------------------------------------------------------------------
/*
6. Create another column called "TYPE" which holds:
- If title contains 'stories' -> Short Stories
- If title = "Just Kids" or "A Heartbreaking Work%" -> Memoir
- Everything Else -> Novel
*/
SELECT title, author_lname,
CASE
WHEN title LIKE "%stories%"
THEN "Short Stories"
WHEN title = "Just Kids" OR title LIKE "A Heartbreaking Work%"
THEN "Memoir"
ELSE "Novel"
END AS "TYPE"
FROM books;
/*
7. Print the title, the last name of the author and how many books
each author has written (e.g. "1 book", "2 books")
*/
SELECT title,author_lname,
CASE
WHEN COUNT(*) = 1 THEN "1 book"
ELSE CONCAT (COUNT(*), " books")
END AS COUNT
FROM books GROUP BY author_lname ;