-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL
359 lines (185 loc) · 10.4 KB
/
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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#write all keywords in capital
#paser will parse our commands , tell us if any error
******************************************************************************
CREATE DATABASE imdb;
USE imdb;
SOURCE {location of imdb.sql}
*******************************************************************************
sudo systemctl start mysqld
mariadb -u root -p
#now this will ask for password
********************************************************************************
SHOW DATABASES;
USE imdb;
SHOW TABLES;
DESCRIBE actors;
**********************************************************************************
SELECT * FROM movies;
#select all the columns from the movies table
SELECT name,year FROM movies;
#result set- set of rows with column name name, year from the movies table
->query only how much data you want , as amount of data that to be tranfered will be less when selected less colomns ,so it is going to be faster
SELECT rankscore,name FROM movies limit 10;
#order table will have order of columns as mentioned in our query
#but the row order will be preserved , atleast in these types of simple queries
******************************************************************************************************************************************************************
LIMIT :
SELECT name,rankscore FROM movies LIMIT 20;
#will give first 20 rows
SELECT name,rankscore FROM movies LIMIT 20 OFFSET 40;
#will give first 20 score from 41th row ,means ignore first 40 and give next 20
*************************************************************************************************************************************************************8
ORDER BY :
SELECT name FROM movies ORDER BY year DESC;
#list recent movies first
SELECT name FROM movies ORDER BY year;
#default : ascending
SELECT name,rankscore,year FROM movies ORDER BY year DESC LIMIT 20 OFFSET 40;
->the output row order maynot be same as the one in the table due to query optimizer and internal data-structure/indices.
************************************************************************************************************************************************************************
DISTINCT :
SELECT DISTINCT genre FROM movies_genres;
#list all genres of movies exist in our database
#think like , who the they show fabric type available on myntra
SELECT DISTINCT first_name,last_name FROM directors ORDER BY first_name;
# multiple column DISTINCT , same entry can be there in one column but the whole row will not be present in resulting table
************************************************************************************************************************************************
WHERE :
SELECT name FROM movies WHERE rankscore>9;
SELECT name,rankscore FROM movies WHERE rankscore>9 ORDER BY rankscore DESC LIMIT 20;
#comparison Operators: =, <> or !=, <, <=, >, >=
SELECT * FROM movies_genres WHERE genre !='Horror' ;
SELECT * FROM movies_genres WHERE genre ='Horror' ;
NULL => doesnot-exist /unknown /missing
-> "=" doesnot work with NULL , will give you empty result-set
SELECT * FROM movies WHERE rankscore IS NULL ;
SELECT * FROM movies WHERE rankscore IS NOT NULL ;
*****************
# LOGICAL OPERATORS: AND , OR , NOT , ALL , ANY , BETWEEN , EXISTS , IN , LIKE , SOME
SELECT name,year,rankscore FROM movies WHERE rankscore>9.7 AND year>2000 ;
SELECT name,year,rankscore FROM movies WHERE NOT rankscore<9.5 ;
SELECT name,year,rankscore FROM movies WHERE rankscore>9 OR year>2005;
-> will study ANY and ALL when study subqueries
SELECT name,year,rankscore FROM movies WHERE year BETWEEN 2004 AND 2005;
SELECT name,year,rankscore FROM movies WHERE year BETWEEN 2005 AND 2004;
#this will give empty result set ( first give smaller then larger )
SELECT director_id, genre FROM directors_genres WHERE genre IN ('comedy','Horror');
#same as director_id='comedy' OR director_id='horror'
SELECT name,year,rankscore FROM movies WHERE name LIKE 'Tisch%';
# wildcard character to imply 0 or more character
SELECT name FROM movies WHERE name LIKE '%es';
SELECT name FROM movies WHERE name LIKE 'esc%es';
SELECT name FROM movies WHERE name LIKE 'Agn_s';
->if we want to match % or _ , we should use the backslash as the escape character : \% and \_
-> \ is escape character
*************************************************************************************************************************
aggregate functions: computes a single value on a set of rows and returns the aggregate
COUNT , MIN , MAX , SUM , AVG
SELECT MIN(year) FROM movies;
SELECT MAX(year) FROM movies;
SELECT COUNT(*) FROM movies;
#number of rows
SELECT COUNT(year) FROM movies;
# no of rows of year equal to number of rows , same as above
SELECT COUNT(year) FROM movies WHERE year>2000;
Q) how to count distinct year from movies table ?
*************************************************************************************************************************
GROUP-BY :
#groups the rows as per some column we choose
SELECT year, COUNT(year) FROM movies GROUP BY year;
# find the number of movies released per year
SELECT year, COUNT(year) FROM movies GROUP BY year ORDER BY year;
SELECT year, COUNT(year) AS cnt FROM movies GROUP BY year ORDER BY cnt;
SELECT year, COUNT(year) cnt FROM movies GROUP BY year ORDER BY cnt;
-> if we are grouping columns contain NULL values , all null values are grouped together
*****************************************************************************************************************************************
HAVING :
# print years which have >2000 movies in our DB [Data scients for analysis]
SELECT year, COUNT(year) AS cnt FROM movies GROUP BY year HAVING cnt>2000;
Order of execution :
1.GROUP BY to create groups
2.apply the aggregate functions
3.Apply HAVING condition
->often used with GROUP BY , not mandatory
SELECT year,name FROM movies HAVING year>2000;
#HAVING without GROUP BY is same as WHERE
SELECT year, COUNT(year) as cnt FROM movies WHERE rankscore>9 GROUP BY year HAVING cnt>20;
# HAVING vs WHERE
# WHERE is applied on individual rows while HAVING is applied on groups
# HAVING is applied after grouping while WHERE is used before grouping
SELECT year, COUNT(year) as cnt FROM movies WHERE rankscore>9 GROUP BY year HAVING cnt>20 LIMIT 5;
*******************************************************************************************************************************************
JOINS :
#combine data in multiple table
#for each movie , print name and genres
SELECT m.name , g.genre from movies m JOIN movies_genres g ON m.id=g.movie_id ;
# table aliases : m and g
#natural join : a join where we have the same column-names across two tabels.
# T1:c1,c2
# T2:c1,c2,c3
SELECT * FROM T1 JOIN T2;
SELECT * FROM T1 JOIN T2 USING (c1);
#both will return c1,c2,c3,c4
#no need to use the keyword "ON"
#Inner join (default) vs left outer vs right outer vs full-outer join
T1: c1, c2 , c3
SELECT m.name , g.genre from movies m LEFT JOIN movies_genres g ON m.id=g.movie_id LIMIT 20;
#LEFT JOIN or LEFT OUTER JOIN
#RIGHT JOIN or RIGHT OUTER JOIN
#FULL JOIN or FULL OUTER JOIN
#JOIN or INNER JOIN
# NULL for missing counterpart rows
# 3-way joins and k-way joins
SELECT a.first_name , a.last_name FROM actors a JOIN roles r ON a.id=r.actor_id JOIN movies m on m.id=r.movie_id AND m.name='officer 444';
#practical note about joins : joins can be expensive computationally when we have large tables
examples->
-> select m.name, d.first_name,d.last_name from movies m JOIN directors d WHERE m.name='tave pe nachati hui chudel' AND d.first_name='janvhi';
-> select m.name as movie_name, d.first_name as director_first_name,d.last_name, g.genre from movies m JOIN directors d JOIN movies_genres g ON m.id=g.movie_id WHERE m.name='tave pe nachati hui chudel' AND d.first_name='janvhi';
**********************************************************************************************************************************************************************************
Sub-Queries or Nested Queries or Inner Queries
*************************************************************************************************************************************************************************
DML :
Data manipulation language : SELECT , INSERT , UPDATE , DELETE
INSERT INTO movies (id, name , year , rankscore ) values (412321, 'tave pe nachati hui chudel', 2022, 10);
INSERT INTO movies (id, name , year , rankscore ) values (412322, 'catboy', 2022, 8),(41232, 'gadhavacha lagna', 2021, 7);
#inserting multiple values
example->
->INSERT INTO phone_book2 SELECT * FROM phone_book WHERE name IN ('shrinija','sakshi','janvhi');
#inserting from abother table (subqueries)
*******************
UPDATE :
UPDATE <Table name> SET col1=val1, col2=val2 WHERE condition;
UPDATE movies SET rank=9 WHERE id=412321;
#Update multiple rows also.
#can be used along with sub-queries.
************************
DELETE :
DELETE FROM movies WHERE id=412321;
#Remove all rows : TRUNCATE TABLE <Table name>; (DDL)
#Same as select without WHERE clause
****************************************************************************************************************************
DDL :
CREATE TABLE student (id INT PRIMARY, name VARCHAR(50) NOT NULL);
NOT NULL - Ensures that a column cannot have a NULL value
UNIQUE - Ensures that all values in a column are different
PRIMARY KEY - A combination of NOT NULL and UNIQUE , uniquely identifies each row in a table
FOREIGN KEY - uniquely identifies a row/record in another table
CHECK - Ensures that all values in a column satifies a specific condition
DEFAULT - Sets a default value for a column when no values is specified
INDEX - Used to create and retrive data from the database very quickly
****************************
ALTER : ADD , MODIFY , DROP
ALTER TABLE language ADD country VARCHAR(50);
ALTER TABLE language MODIFY country VARCHAR(60);
ALTER TABLE language DROP country;
***************************
# Remove both the table and all of the data permanently.
DROP TABLE <Tablename>;
DROP TABLE <Tablename> IF EXISTS;
TRUNCATE TABLE <TableName>;
# same as DELETE FROM <TableName>
# TRUNCATE delete the content of the table not the table itself
****************************************************************************************************************************
DCL :
Data contro language :
***************************************************************************************************************************