1
+ /* ********************************/
2
+ /* Reterive Data */
3
+ /* ********************************/
4
+ USE mosh_sql_store;
5
+
6
+ SELECT * FROM customers
7
+ WHERE customer_id > 5
8
+ ORDER BY first_name;
9
+
10
+ SELECT DISTINCT state
11
+ FROM customers;
12
+
13
+ /* return all the products name, unit price, new price (unit price *1.1) */
14
+ SELECT name, unit_price, (unit_price * 1 .1 ) AS new_price
15
+ FROM products;
16
+
17
+ /* ********************************/
18
+ /* Operators */
19
+ /* ********************************/
20
+ /*
21
+ >
22
+ <
23
+ >=
24
+ <=
25
+ !=
26
+ <>
27
+ AND
28
+ OR
29
+ NOT
30
+ IN
31
+ NOT IN
32
+ BETWEEN
33
+ LIKE
34
+ IS NULL
35
+ */
36
+
37
+ /* get the orders placed this year*/
38
+ SELECT * FROM orders
39
+ WHERE order_date >= ' 2020-01-01' ;
40
+
41
+
42
+ SELECT *
43
+ FROM customers
44
+ WHERE birth_date > ' 1990-01-01' AND points > 1000 ;
45
+
46
+ SELECT *
47
+ FROM customers
48
+ WHERE NOT (birth_date > ' 1990-01-01' AND points > 1000 );
49
+
50
+ /*
51
+ get items of order #6 where total price is greater than 30
52
+ */
53
+ SELECT *
54
+ FROM order_items
55
+ WHERE order_id = 6 AND quantity * unit_price > 30 ;
56
+
57
+
58
+ SELECT *
59
+ FROM customers
60
+ WHERE state IN (' VA' ,' GA' ,' FL' );
61
+
62
+ SELECT *
63
+ FROM customers
64
+ WHERE state NOT IN (' VA' ,' GA' ,' FL' );
65
+
66
+ /* return products with quanity in stock equal to 49, 38, 72*/
67
+ SELECT *
68
+ FROM products
69
+ WHERE quantity_in_stock IN (49 ,38 ,72 );
70
+
71
+
72
+ SELECT *
73
+ FROM customers
74
+ WHERE points BETWEEN 1000 AND 3000 ;
75
+
76
+ /* return customers born between 1/1/1990 and 1/1/2000*/
77
+ SELECT *
78
+ FROM customers
79
+ WHERE birth_date BETWEEN ' 1990-01-01' AND ' 2000-01-01' ;
80
+
81
+ /* get the orders that are not shipped*/
82
+ SELECT *
83
+ FROM orders
84
+ WHERE shipped_date IS NULL ;
85
+
86
+
87
+ SELECT *
88
+ FROM customers
89
+ ORDER BY last_name DESC , state ASC
90
+ LIMIT 10 ;
91
+
92
+ SELECT order_id, product_id, quantity, unit_price
93
+ FROM order_items
94
+ WHERE order_id = 2 ;
95
+
96
+ /* offset start from 2nd record */
97
+ SELECT order_id, product_id, quantity, unit_price
98
+ FROM order_items
99
+ LIMIT 5 OFFSET 2 ;
100
+
101
+ -- customer 5 records start from 6 for pagniation (example 1-5 for one page, 6-10 for second page...)
102
+ SELECT *
103
+ FROM customers
104
+ LIMIT 5 ,6 ;
105
+
106
+ /* get top 3 loyal customers*/
107
+ SELECT *
108
+ FROM customers
109
+ ORDER BY points DESC
110
+ LIMIT 3 ;
111
+
112
+ /*
113
+ % any number of characters
114
+ _ single character
115
+ */
116
+ SELECT *
117
+ FROM customers
118
+ WHERE last_name LIKE ' _____y' ;
119
+
120
+ SELECT *
121
+ FROM customers
122
+ WHERE last_name LIKE ' b____y' ;
123
+
124
+ /* get customers addresses contain TRAIL or AVENUE OR phone numbers end with 9*/
125
+ SELECT *
126
+ FROM customers
127
+ WHERE address LIKE ' %TRAIL%' OR address LIKE ' %AVENUE%'
128
+ OR phone LIKE ' %9' ;
129
+
130
+
131
+ SELECT *
132
+ FROM customers
133
+ WHERE address REGEXP ' trail|avenue' ;
134
+
135
+ SELECT *
136
+ FROM customers
137
+ WHERE phone REGEXP ' 9$' ;
138
+
139
+ /* ********************************/
140
+ /* Regular Expressions */
141
+ /* ********************************/
142
+ SELECT *
143
+ FROM customers
144
+ WHERE last_name LIKE ' %field%' ;
145
+
146
+ SELECT *
147
+ FROM customers
148
+ WHERE last_name REGEXP ' field' ;
149
+
150
+ SELECT *
151
+ FROM customers
152
+ WHERE first_name REGEXP ' I...' ;
153
+
154
+ /*
155
+ ^ : beginning of the string of the field
156
+ $ : end of the string
157
+ | : either OR
158
+ [] : includes one of these insides []. set of characters [abc] OR [a-h]
159
+ https://www.oreilly.com/library/view/mysql-cookbook/0596001452/ch04s08.html
160
+ */
161
+ SELECT *
162
+ FROM customers
163
+ WHERE last_name REGEXP ' field$' ;
164
+
165
+ SELECT *
166
+ FROM customers
167
+ WHERE last_name REGEXP ' field|mac|rose' ;
168
+
169
+ SELECT *
170
+ FROM customers
171
+ WHERE last_name REGEXP ' ^field|mac|rose' ;
172
+
173
+ /*
174
+ ge
175
+ ie
176
+ me
177
+ includes in last name
178
+ */
179
+ SELECT *
180
+ FROM customers
181
+ WHERE last_name REGEXP ' [gim]e' ;
182
+
183
+ /*
184
+ ae
185
+ be
186
+ ...
187
+ he
188
+ includes in last name
189
+ */
190
+ SELECT *
191
+ FROM customers
192
+ WHERE last_name REGEXP ' [a-h]e' ;
193
+
194
+ /*
195
+ Get the customers whose
196
+ - first names are ELKA or AMBUR
197
+ - last names end with EY or ON
198
+ - last name start with MY or contain SE
199
+ - last name contain B followed by R or U
200
+ */
201
+ SELECT *
202
+ FROM customers
203
+ WHERE first_name REGEXP ' ELKA|AMBUR' ;
204
+
205
+ SELECT *
206
+ FROM customers
207
+ WHERE last_name REGEXP ' EY$|ON$' ;
208
+
209
+ SELECT *
210
+ FROM customers
211
+ WHERE last_name REGEXP ' ^MY|SE' ;
212
+
213
+ SELECT *
214
+ FROM customers
215
+ WHERE last_name REGEXP ' B[R|U]' ;
0 commit comments