forked from bhattbhavesh91/cheat_sheets
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathAll_about_SQL.txt
More file actions
executable file
·293 lines (260 loc) · 9.2 KB
/
Copy pathAll_about_SQL.txt
File metadata and controls
executable file
·293 lines (260 loc) · 9.2 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
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
==================
To install MySQL Server
- sudo apt-get install mysql-server
- Default user is root
- Enter a simple password for root user
- enter mysql command to check if its installed
- mysql -u root -p
- show databases; - to check databases present
- quit - to quit sql server
==================
To install MySQL workbench
- sudo apt-get install mysql-workbench
Go to search
- MySQL
- Open MySQL workbench
- Click Database - Query database
- Click Ok
- Enter the root password
==================
==================
Showing the databases
show databases;
==================
Creating a database
create database users;
==================
Go inside that database
use users;
==================
To view all the tables
show tables;
==================
Creating a Table
create table users1(id int(20),campaign char(20),signed_up_on Date);
==================
View Table information
describe user1;
==================
Adding data to the databases
insert into users1
values(1,'facebook','2014-10-01');
Inserting multiple rows
insert into users1
values(2,'twitter','2014-10-02'),
(3,'direct','2014-10-02'),
(4,'facebook','2014-10-03'),
(5,'organic','2014-10-03'),
(6,'organic','2014-10-03'),
(7,'organic','2014-10-04'),
(8,'direct','2014-10-05'),
(9,'twitter','2014-10-05'),
(10,'organic','2014-10-05');
==================
To View the table
mysql> select * from users1;
+------+----------+--------------+
| id | campaign | signed_up_on |
+------+----------+--------------+
| 1 | facebook | 2014-10-01 |
| 2 | twitter | 2014-10-02 |
| 3 | direct | 2014-10-02 |
| 4 | facebook | 2014-10-03 |
| 5 | organic | 2014-10-03 |
| 6 | organic | 2014-10-03 |
| 7 | organic | 2014-10-04 |
| 8 | direct | 2014-10-05 |
| 9 | twitter | 2014-10-05 |
| 10 | organic | 2014-10-05 |
+------+----------+--------------+
==================
Sorting with ORDER BY
mysql> select * from users1 order by campaign;
+------+----------+--------------+
| id | campaign | signed_up_on |
+------+----------+--------------+
| 3 | direct | 2014-10-02 |
| 8 | direct | 2014-10-05 |
| 1 | facebook | 2014-10-01 |
| 4 | facebook | 2014-10-03 |
| 5 | organic | 2014-10-03 |
| 6 | organic | 2014-10-03 |
| 7 | organic | 2014-10-04 |
| 10 | organic | 2014-10-05 |
| 2 | twitter | 2014-10-02 |
| 9 | twitter | 2014-10-05 |
+------+----------+--------------+
Here is how you can sort by campaign AND signed_up_on
mysql> select * from users1 order by campaign,signed_up_on;
+------+----------+--------------+
| id | campaign | signed_up_on |
+------+----------+--------------+
| 3 | direct | 2014-10-02 |
| 8 | direct | 2014-10-05 |
| 1 | facebook | 2014-10-01 |
| 4 | facebook | 2014-10-03 |
| 5 | organic | 2014-10-03 |
| 6 | organic | 2014-10-03 |
| 7 | organic | 2014-10-04 |
| 10 | organic | 2014-10-05 |
| 2 | twitter | 2014-10-02 |
| 9 | twitter | 2014-10-05 |
+------+----------+--------------+
You can also sort in reverse order by adding “DESC”.
mysql> select * from users1 order by campaign desc;
+------+----------+--------------+
| id | campaign | signed_up_on |
+------+----------+--------------+
| 2 | twitter | 2014-10-02 |
| 9 | twitter | 2014-10-05 |
| 5 | organic | 2014-10-03 |
| 6 | organic | 2014-10-03 |
| 7 | organic | 2014-10-04 |
| 10 | organic | 2014-10-05 |
| 1 | facebook | 2014-10-01 |
| 4 | facebook | 2014-10-03 |
| 3 | direct | 2014-10-02 |
| 8 | direct | 2014-10-05 |
+------+----------+--------------+
==================
Filtering with WHERE
mysql> select * from users1 where campaign ='twitter';
+------+----------+--------------+
| id | campaign | signed_up_on |
+------+----------+--------------+
| 2 | twitter | 2014-10-02 |
| 9 | twitter | 2014-10-05 |
+------+----------+--------------+
2 rows in set (0.00 sec)
Multiple value selection -
mysql> select * from users1 where campaign in ('twitter','direct');
+------+----------+--------------+
| id | campaign | signed_up_on |
+------+----------+--------------+
| 2 | twitter | 2014-10-02 |
| 3 | direct | 2014-10-02 |
| 8 | direct | 2014-10-05 |
| 9 | twitter | 2014-10-05 |
+------+----------+--------------+
What if you wanted to fetch all the users EXCEPT the ones that came Facebook or Twitter? This is supported with “NOT IN” like this:
mysql> select * from users1 where campaign not in ('twitter','direct');
+------+----------+--------------+
| id | campaign | signed_up_on |
+------+----------+--------------+
| 1 | facebook | 2014-10-01 |
| 4 | facebook | 2014-10-03 |
| 5 | organic | 2014-10-03 |
| 6 | organic | 2014-10-03 |
| 7 | organic | 2014-10-04 |
| 10 | organic | 2014-10-05 |
+------+----------+--------------+
Multiple column conditions for selecting a criteria
mysql> select * from users1 where campaign in ('twitter','direct') and not signed_up_on='2014-10-05';
+------+----------+--------------+
| id | campaign | signed_up_on |
+------+----------+--------------+
| 2 | twitter | 2014-10-02 |
| 3 | direct | 2014-10-02 |
+------+----------+--------------+
Now it’s time to show that SQL’s WHERE is more powerful than Excel’s filters.
In addition to AND, You can actually use OR to say something like “Get me all the users that signed up before 2014-10-04 OR came in organically”.
Here is the query:
mysql> select * from users1 where campaign = 'organic' or signed_up_on < '2014-10-04';
+------+----------+--------------+
| id | campaign | signed_up_on |
+------+----------+--------------+
| 1 | facebook | 2014-10-01 |
| 2 | twitter | 2014-10-02 |
| 3 | direct | 2014-10-02 |
| 4 | facebook | 2014-10-03 |
| 5 | organic | 2014-10-03 |
| 6 | organic | 2014-10-03 |
| 7 | organic | 2014-10-04 |
| 10 | organic | 2014-10-05 |
+------+----------+--------------+
==================
Filtering and sorting
select * from users1 where campaign in ('twitter','direct') order by campaign;
+------+----------+--------------+
| id | campaign | signed_up_on |
+------+----------+--------------+
| 3 | direct | 2014-10-02 |
| 8 | direct | 2014-10-05 |
| 2 | twitter | 2014-10-02 |
| 9 | twitter | 2014-10-05 |
+------+----------+--------------+
4 rows in set (0.00 sec)
==================
All about Joins
create table t1(cid int(20), cname char(20));
insert into t1
values (1,'India'),
(2, 'USA'),
(3, 'Australia');
Table 1 =
+------+-----------+
| cid | cname |
+------+-----------+
| 1 | India |
| 2 | USA |
| 3 | Australia |
+------+-----------+
create table t2(sid int(20), cid int(20), sname char(20));
insert into t2
values (1,1,'Maharashtra'),
(2,1,'Punjab'),
(3,2,'Massachusetts'),
(4,NULL,'Kathmandu');
Table 2 =
+------+------+---------------+
| sid | cid | sname |
+------+------+---------------+
| 1 | 1 | Maharashtra |
| 2 | 1 | Punjab |
| 3 | 2 | Massachusetts |
| 4 | NULL | Kathmandu |
+------+------+---------------+
Inner join
mysql> select *
-> from t1
-> inner join t2
-> on
-> t1.cid = t2.cid;
+------+-------+------+------+---------------+
| cid | cname | sid | cid | sname |
+------+-------+------+------+---------------+
| 1 | India | 1 | 1 | Maharashtra |
| 1 | India | 2 | 1 | Punjab |
| 2 | USA | 3 | 2 | Massachusetts |
+------+-------+------+------+---------------+
Right join
mysql> select * from t1 right join t2 on t1.cid = t2.cid;
+------+-------+------+------+---------------+
| cid | cname | sid | cid | sname |
+------+-------+------+------+---------------+
| 1 | India | 1 | 1 | Maharashtra |
| 1 | India | 2 | 1 | Punjab |
| 2 | USA | 3 | 2 | Massachusetts |
| NULL | NULL | 4 | NULL | Kathmandu |
+------+-------+------+------+---------------+
Left join
mysql> select * from t1 left join t2 on t1.cid = t2.cid;
+------+-----------+------+------+---------------+
| cid | cname | sid | cid | sname |
+------+-----------+------+------+---------------+
| 1 | India | 1 | 1 | Maharashtra |
| 1 | India | 2 | 1 | Punjab |
| 2 | USA | 3 | 2 | Massachusetts |
| 3 | Australia | NULL | NULL | NULL |
+------+-----------+------+------+---------------+
Outer join
mysql> select * from t1 left join t2 on t1.cid = t2.cid union select * from t1 right join t2 on t1.cid = t2.cid;
+------+-----------+------+------+---------------+
| cid | cname | sid | cid | sname |
+------+-----------+------+------+---------------+
| 1 | India | 1 | 1 | Maharashtra |
| 1 | India | 2 | 1 | Punjab |
| 2 | USA | 3 | 2 | Massachusetts |
| 3 | Australia | NULL | NULL | NULL |
| NULL | NULL | 4 | NULL | Kathmandu |
+------+-----------+------+------+---------------+