-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise 05 - Refining Selections.sql
More file actions
72 lines (55 loc) · 1.86 KB
/
Exercise 05 - Refining Selections.sql
File metadata and controls
72 lines (55 loc) · 1.86 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
-- Exercise 5
INSERT INTO books
(title, author_fname, author_lname, released_year, stock_quantity, pages)
VALUES ('10% Happier', 'Dan', 'Harris', 2014, 29, 256),
('fake_book', 'Freida', 'Harris', 2001, 287, 428),
('Lincoln In The Bardo', 'George', 'Saunders', 2017, 1000, 367);
SELECT *
FROM books;
/*----------------------------------------------------*/
-- Select titles that contain 'stories'
SELECT title
FROM books
WHERE title LIKE '%stories%';
/*----------------------------------------------------*/
-- Find the book with the most pages
SELECT title
FROM books
ORDER BY pages DESC
LIMIT 1;
/*----------------------------------------------------*/
-- print a summary containing title and year of the three most recent books
SELECT CONCAT(title,' - ', released_year) AS summary
FROM books
ORDER BY released_year DESC
LIMIT 3;
/*----------------------------------------------------*/
-- find all books with an author lastname that contains a space character
SELECT title, author_lname
FROM books
WHERE author_lname LIKE '% %';
/*----------------------------------------------------*/
-- find the three books with the lowest stock quantity
SELECT title, stock_quantity
FROM books
ORDER BY stock_quantity
LIMIT 3;
/*----------------------------------------------------*/
-- sort titles and author names
SELECT title, author_lname
FROM books
ORDER BY author_lname, title;
/*----------------------------------------------------*/
-- create output shown in the udemy course
-- ( MY FAVOURITE AUTHOR IS XXX <-authors full names sorted by last name)
SELECT CONCAT(
'MY FAVOURITE AUTHOR IS',
' ',
UPPER(author_fname),
' ',
UPPER(author_lname),
'!') AS yell
FROM books
ORDER BY yell DESC;
/*----------------------------------------------------*/
-- that's it with chapter 8