forked from jetlijetski/Working-With-Functions-MySQL
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPractice with Logical Operators.sql
More file actions
111 lines (72 loc) · 3.67 KB
/
Copy pathPractice with Logical Operators.sql
File metadata and controls
111 lines (72 loc) · 3.67 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#Evaluate the result of the following queries:
SELECT 10 != 10;
-- Solution: 0 or False
SELECT 15 > 14 && 99 - 5 <= 94;
-- Solution: 1 or True
SELECT 1 IN (5,3) || 9 BETWEEN 8 AND 10; (Select 1 in (5,3) or is 9 between 8 and 10?)
-- Solution: 1 or True, the first part is false because 1 is not a number in the set of numbers 5 and 3. However because this
-- is an OR statement indicated by the dual pipe ||, the second part is True, 9 is between 8 and 10. This indicates the result
-- will be 1 or True because at least one side of the argument is True.
#SELECT all books written before 1980 (Not including 1980)
-- Solution:
SELECT
title, released_year
FROM books
WHERE released_year < 1980;
#SELECT all books written by Eggers or Chabon
-- Solution:
SELECT title, author_lname FROM books
WHERE author_lname IN ('Eggers', 'Chabon');
#SELECT all books written by Lahiri published after the year 2000
-- Solution:
SELECT title, author_lname, released_year FROM books
WHERE author_lname = 'Lahiri'
AND released_year > 2000;
#SELECT all books where the page count is between 100 and 200 pages.
-- Solution:
SELECT title, pages FROM books
WHERE pages BETWEEN 100 AND 200;
#SELECT all books where the author last name starts with a C or an S
-- Solution:
SELECT title, author_lname FROM books
WHERE author_lname LIKE 'C%'
OR author_lname LIKE 'S%';
-- Solution 2: We can utilize the SUBSTRING function along with an IN subset to shorten the code.
SELECT title, author_lname FROM books
WHERE SUBSTR(author_lname,1,1) IN ('C','S');
-- (author_lname,1,1) indicates we are looking at the author_lname column, and asking for JUST the first letter.
-- the 1,1 indicates we are starting with letter 1 and ending at letter 1.
#Create a CASE statement that produces the title, author_lname, and a new column called 'TYPE' with the following
#conditions:
-- If the the title of a book contains the word 'stories' classify its TYPE as 'Short Stories'
-- If the title is exactly 'Just Kids' or 'A Heartbreaking Work of Staggering Genius' classify its TYPE as a 'Memoir'
-- Everything else should be classified as a 'Novel'
-- Solution:
SELECT
title,
author_lname,
CASE
WHEN title LIKE '%stories%' THEN 'Short Stories'
WHEN title = 'Just Kids' OR title = 'A Heartbreaking Work of Staggering Genius' THEN 'Memoir'
ELSE 'Novel'
END AS TYPE
FROM books;
#Create a CASE statement that produces the title, author_lname, and a new column called 'COUNT' which indicates how many
#books the author has written. Be sure to indicate the correct singular or plural for book(s).
-- Solution:
-- We can start by concatenating the first/last name of the author into a single column with the alias "Name".
-- We then create a case statement using the WHEN clause to specify that when an author's total book count is just one , then their Count value should be displayed using the string "1 book".
-- The ELSE clause utilizes a concatenation of their total published book count, along with the string " books".
-- We apply the GROUP BY author_lname, author_fname statement because in this data set we have two authors with the same last name. By using this statement, we can separate Dan Harris from Freida Harris
-- to eliminate inconsistencies.
-- Additionally we can use ORDER BY to place their total book count in ascending order.
SELECT
CONCAT(author_fname, ' ', author_lname) AS 'Name',
CASE
WHEN COUNT(*) = 1 THEN '1 book'
ELSE CONCAT(COUNT(*), ' books')
END AS COUNT
FROM books
GROUP BY author_lname,
author_fname
ORDER BY COUNT(*);