-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSQL CODE
257 lines (157 loc) · 6.16 KB
/
SQL CODE
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
CREATE TABLE employee(employeeid int primary key, firstname varchar(15), middlename varchar(20), lastname varchar(30), age int, salary int,location varchar(50) default 'gwarko' not null);
CREATE TABLE employee(
employeeid int,
firstname varchar(15),
middlename varchar(20),
lastname varchar(30),
age int,
salary int,
location varchar(50) default 'gwarko' not null,
PRIMARY KEY(employeeid,firstname)
);
INSERT INTO employee(employeeid, firstname, middlename, lastname, age, salary, location) VALUES(1,"Ram","Kumar","Sharma",40, 40000);
SELECT * FROM employee;
CREATE TABLE employee(
employeeid int AUTO_INCREMENT,
firstname varchar(15),
middlename varchar(20),
lastname varchar(30),
age int,
salary int,
location varchar(50) default 'gwarko' not null,
PRIMARY KEY(employeeid,firstname)
);
Unique
============
only one primary key and primary key canno t have null value, it is use to get unique record
unique key can hold null value
mysql can hold many null but other compiler may hold only one null
table has only one primary key but can have multiple unique key in the table
CREATE TABLE employee(
employeeid int AUTO_INCREMENT,
firstname varchar(15),
lastname varchar(30),
age int,
PRIMARY KEY(firstname, lastname)
);
SELECT DISTINCT location FROM student;
SELECT * FROM studnet ORDER BY student_fname desc;
SELECT student_fname FROM student Order BY enrollment_date DESC LIMIT 1;
SELECT * FROM student WHERE student_fname LIKE '%/%%';
DISTINCT
==========
DISTINCT AND ORDERE BY DOESNT WORK AT THE SAME TIME
SELECT source_of_jonining FROM student;
ORDER OF EXECUTION
================
FROM (LOADING THE TABLE)
SELECT (PROJECTING source_of_joining)
SELECT source_of_joiningm enrollment_date FROM student;
SELECT source_of_joiningm enrollment_date FROM student ORDER BY enrollment_date;
================
FROM (LOADING THE TABLE)
SELECT (PROJECTING source_of_joining, enrollment_date);
ORDER BY (based on enrollment_date it will order by select source_of_joninig)
===========
SELECT DISTINCT source_of_joining FROM studnets ORDER BY enrollment_date;
ORDER OF EXECUTION
================
FROM
SELECT
DISTINCT
ORDER BY
========================================
AGGREGRATE FUNCTION
========================================
INPUT IS MANY LINE AND OUTPUT IS ONE LINE
COUNT
SELECT COUNT(*) FROM student;
SELECT COUNT(DISTINCT student_company) AS companies FROM student;
SELECT COUNT(DISTINCT source_of_joining) FROM student;
SELECT batch_date FROM student;
=====================================
GROUP BY
====================================
HOW MANY PEOPLE JOINED BY DIFFENERNT SOURCE LIKE 'FRIEND'
SELECT source_of_joining, COUNT(*) FROM student GROUP BY source_of_joining = 'friend';
location, source_of_joninig
SELECT location, source_of_joining FROM student;
SELECT location, source_of_joining, COUNT(*) FROM student GROUP BY location, source_of_joining;
SELECT selected_course, COUNT(*) FROM student GROUP BY selected_course;
=====================================
MIN & MAX
=====================================
SELECT MIN(years_of_exp) FROM student;
SELECT MAX(years_of_exp) FROM student;
Select source_of_joining, max(years_of_exp) from student group by source_of_joining;
SELECT SUM(SORCE OF JONING) FROM STUDENT;
AVG
=======
SELECT location, AVG(years_of_exp) FROM student group by location
GROUP BY
===============
data type
=============================================
Decimal
time stamp
MariaDB [trendytech]> create table course_new( courseid int not null, coursename varchar(30) not null, coursedurationmonths decimal(3,1) not null, coursefee int not null, changed_at timestamp default now() on update now()/current_timestamp(), primary key(courseid));
==========================================
Logical operator
========================
!= or <>--------> not equal to
like and not like ----> eg: '%data%'
=
>=
>
<=
<
in
between and not betweeen -----> eg: between 8 and 12
select * from student where student_company = 'flipkart' or student_company = 'walmart' or student_company = 'microsoft';
select * from student where student_company in ('filpkart','walmart', 'microsoft');
case
select courseid,coursename,coursefee,
-> case
-> when coursedurationmonths > 4 then 'master'
-> else 'diploma'
-> end as course_type
-> from course_new;
select student_id, student_fname, student_lname, student_company,
-> CASE
-> WHEN student_company in ('flipkart', 'walmart', 'microsoft') then 'product_based'
-> ELSE 'service_based'
-> END as 'company_type'
-> FROM student;
====================================================
JOINs
==============
tables
->student
student_fname, selected_course
->course
course_id, course_name
NESTED QUERY
================
select course_name from course where course_id = (select selected_course from student where student_fname = 'ram');
==================
select student_fname, course_name from student join course on student.selected_course = course.course_id;
BY DEFAULT join == inner join
inner join = matched in both table are only joined
outer join = every row in first table is linked with every data in second table unmatched records are set null
Left outer join
===============
all the matching record are considered + all the non matching recods in the left table which does not have match in the right
Right outer join
==============================
all the matching record are considered + all the non matching record in the right table which doesnt have mathc in the left
Full outer join (Union of left and right)
====================
all the matching record + non matchiing from left + non matching form right
select student_fname, student_lname, course_name from student_latest right join course_latest on student_latest.selected_course = course_latest.course_id union select student_fname, student_lname, course_name from student_latest left join course_latest on student_latest.selected_course = course_latest.course_id;
cross join
===============
select * from student join course;
=====================================================
where vs having clause in sql
===============================
select source_of_joining, count(*) as total from student group by source_of_joining;