-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise 04 - String Functions.sql
More file actions
132 lines (100 loc) · 3.28 KB
/
Exercise 04 - String Functions.sql
File metadata and controls
132 lines (100 loc) · 3.28 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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
-- Exercise 4 String Functions
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)
);
INSERT INTO books (title, author_fname, author_lname, released_year, stock_quantity, pages)
VALUES
('The Namesake', 'Jhumpa', 'Lahiri', 2003, 32, 291),
('Norse Mythology', 'Neil', 'Gaiman',2016, 43, 304),
('American Gods', 'Neil', 'Gaiman', 2001, 12, 465),
('Interpreter of Maladies', 'Jhumpa', 'Lahiri', 1996, 97, 198),
('A Hologram for the King: A Novel', 'Dave', 'Eggers', 2012, 154, 352),
('The Circle', 'Dave', 'Eggers', 2013, 26, 504),
('The Amazing Adventures of Kavalier & Clay', 'Michael', 'Chabon', 2000, 68, 634),
('Just Kids', 'Patti', 'Smith', 2010, 55, 304),
('A Heartbreaking Work of Staggering Genius', 'Dave', 'Eggers', 2001, 104, 437),
('Coraline', 'Neil', 'Gaiman', 2003, 100, 208),
('What We Talk About When We Talk About Love: Stories', 'Raymond', 'Carver', 1981, 23, 176),
("Where I'm Calling From: Selected Stories", 'Raymond', 'Carver', 1989, 12, 526),
('White Noise', 'Don', 'DeLillo', 1985, 49, 320),
('Cannery Row', 'John', 'Steinbeck', 1945, 95, 181),
('Oblivion: Stories', 'David', 'Foster Wallace', 2004, 172, 329),
('Consider the Lobster', 'David', 'Foster Wallace', 2005, 92, 343);
/* -------------------------------------------- */
SELECT
*
FROM
books;
/* -------------------------------------------- */
-- Get the authors initials
SELECT
CONCAT(
SUBSTR(author_fname, 1, 1),
'.',
SUBSTR(author_lname, 1, 1),
'.') AS Initials
FROM
books;
/* -------------------------------------------- */
-- Reverse and uppercase a string of your choice
SELECT REVERSE(UPPER('Hello World!'));
/* -------------------------------------------- */
-- What does this return?
SELECT
REPLACE(CONCAT('I', ' ', 'like', ' ', 'cats'),
' ',
'-');
-- My answer: I-like-cats
/* -------------------------------------------- */
-- Replace the book title spaces with ->
SELECT REPLACE(title, ' ', '->')
FROM books;
/* -------------------------------------------- */
-- print authors fname as forwards and its reverse as backwards
SELECT
author_fname AS forwards,
REVERSE(author_fname) AS backwards
FROM books;
/* -------------------------------------------- */
-- print the authors full name in caps
SELECT
UPPER
(
CONCAT
(
author_fname,
' ',
author_lname
)
) AS full_name_caps
FROM books;
/* -------------------------------------------- */
-- Show when the books got released
SELECT
CONCAT(
title,
' was released in ',
released_year
)
FROM books;
/* -------------------------------------------- */
-- show the title length (characters)
SELECT title,
char_length(title) AS length
FROM books;
/* -------------------------------------------- */
-- show the shortened booktitle xxx...., the author Smith, John and the quantities in stock
SELECT
CONCAT(SUBSTR(title, 1,10), '...') AS short_title,
CONCAT(author_lname, ', ', author_fname) AS author_fullname,
CONCAT(stock_quantity, ' in stock') AS quantity
FROM books;
/* -------------------------------------------- */