-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathREGEX-WILDCARD.sql
314 lines (214 loc) · 10.6 KB
/
REGEX-WILDCARD.sql
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
-- WILDCARD and REGEX Usage
CREATE TABLE employees2
(
id CHAR (9),
name VARCHAR(50),
state VARCHAR (50),
salary SMALLINT,
company VARCHAR (20)
);
INSERT INTO employees2 VALUES (123456789, 'John Walker', 'Florida', 2500, 'IBM');
INSERT INTO employees2 VALUES (234567890, 'Brad Pitt', 'Florida', 1500, 'APPLE');
INSERT INTO employees2 VALUES (345678901, 'Eddie Murphy', 'Texas', 3000, 'IBM');
INSERT INTO employees2 VALUES (456789012, 'Eddie Murphy', 'Virginia', 1000, 'GOOGLE');
INSERT INTO employees2 VALUES (567890123, 'Eddie Murphy', 'Texas', 7000, 'MICROSOFT');
INSERT INTO employees2 VALUES (456789012, 'Brad Pitt', 'Texas', 1500, 'GOOGLE');
INSERT INTO employees2 VALUES (123456710, 'Mark Stone', 'Pennsylvania', 2500, 'IBM');
SELECT * FROM employees2;
-- WILDCARDS
-- Wildcards in SQL
-- LIKE Clause: Used with wildcards.
-- 1) % percent sign wildcard: queries if there are zero or more characters
-- %a
-- , Sophia, Olivia, Isabella, Emma, Mia, Ava, a
-- %a%
-- Ayşe, Fatma, Ahmet, Aslı, Canan, Melisa
-- % -> 0 characters or 1 character, any character, multiple characters
-- a%
-- Adam, Alexander, Aaron, Andrew, Anthony, a
-- Briefly, how to comment -> Ctrl+/
SELECT * FROM employees2;
-- Retrieve employee names starting with 'E'.
SELECT Name FROM employees2 WHERE Name LIKE 'E%';
-- Retrieve employee names ending with 'e'.
SELECT Name FROM Employees2 WHERE Name LIKE '%e';
-- Retrieve names containing the letter 'e' anywhere
SELECT Name FROM Employees2 WHERE Name LIKE '%e%';
SELECT * FROM words;
SELECT * FROM employees2;
-- Retrieve employee names starting with 'B' and ending with 't'
-- LIKE _wildcard with WHERE
SELECT Name FROM employees2 WHERE Name LIKE 'B%t';
-- Retrieve names containing the letter 'a' anywhere
SELECT Name FROM employees2 WHERE Name LIKE '%a%';
-- Retrieve names containing the letter 'e' or 'r' anywhere
SELECT Name FROM employees2 WHERE Name LIKE '%e%' OR Name LIKE '%r%';
SELECT Name FROM employees2 WHERE Name LIKE '%e%r%'; -- order matters
INSERT INTO employees2 VALUES (123456712,'xcreu','x',6000,'x');
SELECT Name FROM employees2 WHERE Name LIKE '%e%r%' OR Name LIKE '%r%e%';
SELECT Name FROM employees2 WHERE Name ~ 'e';
---- WILDCARD
-- _ -> only one character
-- Retrieve state values where the second character is 'e' and the fourth character is 'n'
SELECT State FROM employees2 WHERE State LIKE '_e_n%';
SELECT * FROM employees2;
-- Retrieve state values where the second last character is 'i'
SELECT State FROM employees2 WHERE State LIKE '%i_';
-- Retrieve state values where the second character is 'e' and at least 6 characters long
SELECT State FROM employees2 WHERE State LIKE '_e____%';
-- Retrieve state values where there is an 'i' character anywhere after the second character
SELECT State FROM employees2 WHERE State LIKE '__%i%';
CREATE TABLE words
(
word_id CHAR(10) UNIQUE,
word VARCHAR(50) NOT NULL,
number_of_letters SMALLINT
);
INSERT INTO words VALUES (1001,'hot', 3);
INSERT INTO words VALUES (1002,'hat', 3);
INSERT INTO words VALUES (1003,'hit', 3);
INSERT INTO words VALUES (1004,'hbt', 3);
INSERT INTO words VALUES (1008,'hct', 3);
INSERT INTO words VALUES (1005,'adem',4);
INSERT INTO words VALUES (1006,'selena', 6);
INSERT INTO words VALUES (1007,'yusuf', 5);
INSERT INTO words VALUES (1009,'hoait', 5);
INSERT INTO words VALUES (1010,'htc', 3);
INSERT INTO words VALUES (1011,'haxxyt', 6);
SELECT * FROM words;
SELECT word FROM words WHERE word ~ 'tc';
-- order matters
--- NOT LIKE
-- Retrieve words that do not contain the letter 'h'
SELECT word FROM words WHERE word NOT LIKE '%h%';
-- Retrieve words that end with the letter 't' or 'f'
SELECT word FROM words WHERE word LIKE '%t' OR word LIKE '%f';
-- Retrieve words that do not end with the letter 't' or 'f'
SELECT word FROM words WHERE word NOT LIKE '%t' AND word NOT LIKE '%f';
-- Retrieve words that start with any character and do not contain the letter 'a' or 'e'
SELECT word FROM words WHERE word NOT LIKE '_%a%' AND word NOT LIKE '_%e%';
-- Retrieve words that do not contain the letter 'a' or 'e'
SELECT word FROM words WHERE word NOT LIKE '%a%' AND word NOT LIKE '%e%';
-- REGEX
------ Regular Expression Condition: -- REGEX
-- Retrieve words where the first character is 'h', the second character is 'o', 'a', 'i' and the last character is 't'
-- Method 1
SELECT word FROM words
WHERE word
LIKE 'ho%t' OR word
LIKE 'ha%t' OR word
LIKE 'hi%t';
-- Method 2
-- 3 letters
SELECT word FROM words WHERE word ~ 'h[oai]t';
SELECT * FROM words
-- Retrieve words where the first character is 'h', the second character is 'o', 'a', 'i' and the last character is 't', even if it is more than 3 letters
SELECT word FROM words WHERE word ~ 'h[oai](.*)t';
SELECT * FROM words;
INSERT INTO words VALUES (1011,'habit',5);
INSERT INTO words VALUES (1012,'ahmet',5);
INSERT INTO words VALUES (1013,'ahmetaga',8);
SELECT word FROM words WHERE word ~ 'h(.*)[oae]t';
SELECT word FROM words WHERE word ~ 'h(.*)[oae]t$';
SELECT word FROM words WHERE word ~ '^h(.*)[oae]t';
SELECT word FROM words WHERE word ~ 'h';
-- ^ -> first character
-- $ -> last character
-- (.*) -> any character
-- . -> _ -> one character
-- * -> % -> 0, 1, multiple characters
-- Retrieve words where the first character is 'h', the last character is 't' and the second character is any character
-- between 'a' and 'e'
SELECT word FROM words WHERE word ~ '^h[a-e](.*)t$';
-- Retrieve words where the first character is 's', 'a' or 'y'
SELECT word FROM words WHERE word ~ '^[say]';
-- Retrieve words where the last character is 'm', 'a' or 'f'
SELECT word FROM words WHERE word ~ '[maf]$';
-- Retrieve words where the first character is 's' and the last character is 'a'
-- 's' is only one character, so there is no need to say ^
Select word from words where word ~ '^s(.*)a$';
--- The following code will not work, if there is only one character, there is no need to put it in square brackets,
-- extra characters in the middle can use (.*),
-- '^sa$'; this regex will not work because we need to indicate that there may be other characters in the middle
--ERROR!!
Select word from words where word ~ '^s[a]$';
Select word from words where word ~ '^sa$';
Select word from words where word ~ '^s(.*)a$';
-- Retrieve words that contain the letter 'a' anywhere
SELECT word from words where word ~ 'a';
-- Retrieve words where the first character is between 'd' and 't', followed by any character, and the third character is 'l'
SELECT word from words where word ~ '^[d-t].l';
SELECT word from words where word ~ '^[a-e](.*)m$'; --adem
SELECT word from words where word ~ '^[a-e].e'; --adem
-- Retrieve words where the first character is between 'd' and 't', followed by any character, and the fourth character is 'e'
SELECT word from words where word ~ '^[d-t]..e';
-- Retrieve words where the first character is between 'r' and 'z', followed by any character, and the fourth character is 'u'
SELECT word from words where word ~ '^[r-z]..u';
SELECT word from words where word ~ '[r-z]..u';
CREATE TABLE workers3
(
id CHAR (9),
name VARCHAR (50),
state VARCHAR (50),
salary SMALLINT,
company VARCHAR (20)
);
INSERT INTO workers3 VALUES (123456789, 'John Walker', 'Florida', 2500, 'IBM');
INSERT INTO workers3 VALUES (234567890, 'Brad Pitt', 'Florida', 1500, 'APPLE');
INSERT INTO workers3 VALUES (345678901, 'Eddie Murphy', 'Texas', 3000, 'IBM');
INSERT INTO workers3 VALUES (456789012, 'Eddie Murphy', 'Virginia', 1000, 'GOOGLE');
INSERT INTO workers3 VALUES (567890123, 'Eddie Murphy', 'Texas', 7000, 'MICROSOFT');
INSERT INTO workers3 VALUES (456789012, 'Brad Pitt', 'Texas', 1500, 'GOOGLE');
INSERT INTO workers3 VALUES (123456710, 'Mark Stone', 'Pennsylvania', 2500, 'IBM');
SELECT * FROM workers3;
-- EN
--^ when written outside square brackets, it means the first letter
--^ when written inside square brackets, it means any character except this character
-- Retrieve names from workers3 that start with 'E' and end with 'y'
SELECT name FROM workers3 WHERE Name ~ '^E(.*)y$';
-- Retrieve names from workers2 that do not start with 'E' and do not end with 'y'
SELECT name FROM workers3 WHERE Name ~ '^[^E](.*)[^y]$';
-- Retrieve names that start with 'J', 'B', or 'E' and end with 'r' or 't'
SELECT name FROM workers3 WHERE Name ~ '^[JBA](.*)[rt]$';
-- Retrieve names that start with 'J', 'B', or 'E' or end with 'r' or 't'
SELECT name FROM workers3 WHERE Name ~ '^[JBE]' OR Name ~ '[rt]$';
-- Retrieve names that do not start with 'J', 'B', or 'E' AND do not end with 'r' or 't'
SELECT name FROM workers3 WHERE Name ~ '^[^JBE](.*)[^rt]$';
DROP TABLE workers2;
CREATE TABLE workers2
(
id CHAR (9),
name VARCHAR (50),
state VARCHAR (50),
salary SMALLINT,
company VARCHAR (20),
number_of_employees numeric(6)
);
INSERT INTO workers2 VALUES (123456789, 'John Walker', 'Florida', 2500, 'IBM',15000);
INSERT INTO workers2 VALUES (234567890, 'Brad Pitt', 'Florida', 1500, 'APPLE',34900);
INSERT INTO workers2 VALUES (345678901, 'Eddie Murphy', 'Texas', 3000, 'IBM',903400);
INSERT INTO workers2 VALUES (456789012, 'Eddie Murphy', 'Virginia', 1000, 'GOOGLE',259000);
INSERT INTO workers2 VALUES (567890123, 'Eddie Murphy', 'Texas', 7000, 'MICROSOFT',90100);
INSERT INTO workers2 VALUES (456789012, 'Brad Pitt', 'Texas', 1500, 'GOOGLE',56900);
INSERT INTO workers2 VALUES (123456710, 'Mark Stone', 'Pennsylvania', 2500, 'IBM',45690);
INSERT INTO workers2 VALUES (123456711, 'Maks Stelon', 'Ohio', 12500, 'IBM',5790);
INSERT INTO workers2 VALUES (123456712, 'Maksim Mark', 'Ohio', 3500, 'Google',3090);
SELECT * FROM workers2;
-- Retrieve names that contain the letter 'a' or 'k' anywhere
-- The following codes query the same result
SELECT name FROM workers2 WHERE name ~ 'a' OR name ~ 'k'; = SELECT name FROM workers2 WHERE name ~ '[ak]';
SELECT name FROM workers2 WHERE name ~ 'ak'; = SELECT name FROM workers2 WHERE name LIKE '%ak%';
SELECT name FROM workers2 WHERE name LIKE '%ak%';
SELECT name FROM workers2 WHERE name ~ '[ak]';
-- Both letters are present in the name field, but order matters
SELECT name FROM workers2 WHERE name ~ 'a(.*)k';
SELECT * FROM workers2;
-- Retrieve names where the first letter is between 'A' and 'F', the second letter is any character, and the third letter is 'a'
SELECT name FROM workers2 WHERE name ~ '^[A-F].a';
-- Retrieve state values where the third character is 'o' or 'x'
SELECT state FROM workers2 WHERE state ~ '..[ox]';
-- When using the caret symbol, it means from the beginning
SELECT state FROM workers2 WHERE state ~ '^..[ox]';
-- Retrieve state values where the third character is not 'o' or 'x'
SELECT state FROM workers2 WHERE state !~ '^..[ox]';
SELECT state FROM workers2 WHERE state ~ '^..[^ox]';