-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsql.php
More file actions
1417 lines (1386 loc) · 47.5 KB
/
sql.php
File metadata and controls
1417 lines (1386 loc) · 47.5 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
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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
session_start();
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<style type="text/css">
@import url('https://fonts.googleapis.com/css?family=Josefin+Sans&display=swap');
*{
margin: 0;
padding: 0;
box-sizing: border-box;
text-decoration: none;
font-family: sans-serif;
}
body{
background-color: #f3f5f9;
}
.course{
cursor: pointer;
width: 40px;
height: 40px;
position: relative;
display: inline-block;
margin-left: 240px;
height: auto;
margin-top:-2px;
}
.course i{
font-size: 40px;
}
.dd-content{
display: none;
position: absolute;
padding: 10px 20px;
background-color: white;
width: 200px;
box-shadow: 2px 2px 4px rgba(0,0,0,0.125);
box-sizing: 0 5px 25px rgba(0,0,0,0.1);
transition: 0.5s;
z-index: 1;
}
.course ul li{
list-style: none;
padding: 10px 0;
border-bottom: 1px solid rgba(0,0,0,0.01);
display: flex;
align-items: center;
}
.course ul li i{
max-width: 20px;
margin-right: 10px;
opacity: 0.5s;
transition: 0.5s;
}
.course ul li:hover i{
opacity: 1;
}
.course ul li a{
display: inline-block;
text-decoration: none;
color: #555;
font-weight: 500;
transition: 0.5s;
}
.course ul li:hover a{
color: #ff5d94;
}
.wrapper{
position: relative;
margin: 10px;
}
.wrapper .sidebar{
width: 280px;
height: auto;
margin-top: 1px;
padding: 30px 0px;
position: absolute;
background-color: white;
}
.c2{
padding: 15px;
border-bottom: 1px solid #bdb8d7;
border-bottom: 1px solid rgba(0,0,0,0.05);
border-top: 1px solid rgba(255,255,255,0.05);
list-style: none;
}
.c1{
color: black;
display: block;
}
.c2:hover{
background-color: lightgrey;
}
.wrapper .sidebar ul li:hover a{
color: #fff;
}
.wrapper .Content
{
float: right;
width:1180px;
min-height: 720px;
height:auto;
background-color:white;
margin-right:35px;
margin-top: 1px;
}
.y
{
text-align: center;
margin-top: 20px;
}
.sql
{
padding:10px;
display: block;
}
.sql p,h3{
margin-left: 10px;
padding: 16px;
font-size: 19px;
}
.join,.clause,.object,.nested,.difference{
display: none;
}
.join p,h2{
margin-left: 10px;
padding: 16px;
font-size: 19px;
}
.clause p,h2{
margin-left: 10px;
padding: 16px;
font-size: 19px;
}
.object p,h2{
margin-left: 10px;
padding: 16px;
font-size: 19px;
}
.nested p,h2{
margin-left: 10px;
padding: 16px;
font-size: 19px;
}
.difference p,h2{
margin-left: 10px;
padding: 16px;
font-size: 19px;
}
table, th, td {
border: 1px solid black;
border-collapse: collapse;
}
th, td {
padding: 15px;
text-align: left;
}
.z
{
width: 100%;
padding-top: 20px;
height: 80px;
outline: none;
top: 0;
background-color: white;
}
.left
{
float: left;
width: 140px;
margin-left: 10px;
}
.g
{
float: right;
margin-right: 60px;
margin-bottom: 25px;
margin-top: -8px;
}
.x71
{
background-color:rgb(153, 204, 255);
color: white;
padding: 8px 18px;
margin: 8px 0;
border: none;
cursor: pointer;
width: auto;
border-radius: 2px;
}
.x71:hover
{
opacity: 0.8;
}
.dropdown{
float: right;
cursor: pointer;
width: 40px;
height: 40px;
position: relative;
display: inline-block;
margin-right: 240px;
height: auto;
}
.dropdown i{
font-size: 40px;
}
.dropdown-content {
display: none;
position: absolute;
top: 50px;
padding: 10px 20px;
background-color: lightgrey;
width: 200px;
box-sizing: 0 5px 25px rgba(0,0,0,0.1);
transition: 0.5s;
z-index: 1;
border-radius:15px;
}
.dropdown .dropdown-content::before{
content: '';
position: absolute;
top: -5px;
right: 160px;
width: 20px;
height: 20px;
transform: rotate(45deg);
background-color: lightgrey;
}
.dropdown ul li{
list-style: none;
padding: 10px 0;
border-top: 1px solid rgba(0,0,0,0.1);
display: flex;
align-items: center;
}
.dropdown ul li i{
max-width: 20px;
margin-right: 10px;
opacity: 0.5s;
transition: 0.5s;
}
.dropdown ul li:hover i{
opacity: 1;
}
.dropdown ul li a{
display: inline-block;
text-decoration: none;
color: #555;
font-weight: 500;
transition: 0.5s;
}
.dropdown ul li:hover a{
color: #ff5d94;
}
.show {display: block;}
.modal {
display: none; /* Hidden by default */
position: fixed; /* Stay in place */
z-index: 1; /* Sit on top */
left: 0;
top: 0;
width: 100%; /* Full width */
height: 100%; /* Full height */
overflow: auto; /* Enable scroll if needed */
background-color: rgb(0,0,0); /* Fallback color */
background-color: rgba(0,0,0,0.4); /* Black w/ opacity */
padding-top: 60px;
}
.modal-content {
background-color: #fefefe;
margin: 5% auto 15% auto; /* 5% from the top, 15% from the bottom and centered */
border: 1px solid #888;
width: 30%;
height:auto; /* Could be more or less, depending on screen size */
}
.container {
padding: 16px;
}
span.psw {
float: right;
padding-top: 16px;
}
.r1,.r2
{
width:100%;
padding:10px 15px;
margin:5px 0px;
border: 1px solid lightgrey;
outline: none;
border-radius: 2px;
font-size: 17px;
}
::placeholder
{
text-align: center;
}
/* The Close Button (x) */
.close {
position: absolute;
right: 30px;
top: 0;
color: #000;
font-size: 35px;
font-weight: bold;
}
.close:hover,
.close:focus {
color: red;
cursor: pointer;
}
.submit-btn
{
background-color:#4CAF50;
color: white;
width: 100%;
padding: 10px 30px;
display: block;
outline: none;
margin:auto;
border:none;
cursor: pointer;
font-size: 20px;
}
a
{
text-decoration: none;
}
.slide-controls
{
width:100%;
margin: 35px auto;
position: relative;
box-shadow: 0 0 20px 9px #ff61241f;
}
.toggle-btn
{
text-align: center;
padding: 10px 45px;
cursor: pointer;
background: transparent;
border:0;
outline: none;
position: relative;
font-size: 20px;
}
#btn
{
text-align: center;
position: absolute;
width:50%;
height: 100%;
background: linear-gradient(to right,lightgrey,lightgrey);
transition: .5s;
}
.v
{
text-align: center;
color: white;
padding-top: 200px;
font-size: 45px;
}
.footer
{
padding:50px;
color: #fff;
background-color: #011c39;
}
#login
{
left: 50%;
}
.footer p{
text-align: center;
}
.footer a{
text-decoration: none;
}
.footer-bottom{
text-align: center;
color: #999;
margin-bottom:-30px;
}
.socials a{
background: #364a62;
width: 40px;
height: 40px;
display: inline-block;
}
.socials a i{
color: #e7f2f4;
padding: 10px 12px;
font-size: 20px;
}
.imgcontainer {
position: relative;
}
.animate {
-webkit-animation: animatezoom 0.6s;
animation: animatezoom 0.6s
}
@-webkit-keyframes animatezoom {
from {-webkit-transform: scale(0)}
to {-webkit-transform: scale(1)}
}
@keyframes animatezoom {
from {transform: scale(0)}
to {transform: scale(1)}
}
/* Change styles for span and cancel button on extra small screens */
@media screen and (max-width: 300px) {
span.psw {
display: block;
float: none;
}
img.avatar {
width: 40%;
border-radius: 50%;
}
</style>
<body>
<div class="z">
<div class="left">
<a href="newproject.php"><img src="https://www.simplilearn.com/ice9/new_logo.svgz" style="width: 140px;margin-left: 10px;cursor: pointer;">
</div></a>
<div class="course">
<input type="button" onclick="mycourse()" class="x71" value="Courses" name="">
<ul id='myDrop' class='dd-content'>
<li><a href='coursesection.php'>C++</a></li>
<li><a href='javacoursepage.php'>Java</a></li>
<li><a href='sql.php' >Sql</a></li>
<li><a href='phpcourse.php' >Php</a></li>
<li><a href='PythonCourseNew.php' >Python</a></li>
</ul>
</div>
<?php if(!isset($_SESSION['name']))
echo " <div class='g' id='first'>
<button class='x71' onclick='loginform()' style='width:auto;' type='submit'>Login</button>
</div> " ?>
<?php if(isset($_SESSION['name']))
echo " <div class='dropdown' id='third'>
<i onclick='myFunction()' class='fa fa-user-circle-o'></i>
<ul id='myDropdown' class='dropdown-content'>
<li><i class='fa fa-user' style='font-size:15px;'></i> <a href='profilepage.php'>my profile</a></li>
<li><i class='fa fa-edit' style='font-size:15px;'></i> <a href='editprofile.php'>Edit profile</a></li>
<li><i class='fa fa-sign-out' style='font-size:15px;'></i> <a href='newlogout.php' id='second' >Logout</a></li>
</ul>
</div>";?>
</div>
<div id="id01" class="modal">
<div class="modal-content animate">
<div class="imgcontainer">
<span onclick="document.getElementById('id01').style.display='none'" class="close" title="Close Modal">×</span>
</div>
<div class="wrapper1" style="width:70%;height:70%;margin-left: 60px;padding: 30px;border-radius:5px;overflow: hidden;">
<div class="title-text" style="display: flex;width: 200%">
<div class="title login" style="width: 50%;font-size:35px;font-weight: 600;text-align: center">Login Form</div>
</div>
<div class="form-container" style="width: 100%;overflow: hidden;">
<div class="form-inner" style="display: flex;width: 200%;">
<form action="newlogin.php" method="post" class="login" id="login" style="width:50%;">
<div style="height: 50px;width: 90%;margin-top: 20px;padding-top: 2px;padding-bottom: 2px;">
<input type="text" class="r1" placeholder="username or email" name="name" required="">
</div>
<div style="height: 50px;width: 90%;margin-top: 20px;">
<input type="password" class="r2" placeholder="Password" name="password" required="">
</div><br><br>
<div class="pass-link">
<a href="#">Forgot password?</a><br><br>
</div>
<div>
<input type="submit" class="submit-btn" value="Login" onclick="account()">
</div><br>
<div class="signup-link">Not a member? <a href="http://localhost/signup.php">Signup now</a></div>
</form>
</div>
</div>
</div>
</div>
</div>
<div class="wrapper">
<div class="sidebar">
<ul>
<li class="c2"><a href="#" class="c1" onclick="sql()">Structured Query Language (SQL)</a></li>
<li class="c2"><a href="#" class="c1" onclick="join()">Inner Join vs Outer Join</a></li>
<li class="c2"><a href="#" class="c1" onclick="clause()">Having vs Where Clause in SQL</a></li>
<li class="c2"><a href="#" class="c1" onclick="object()">Database Objects in DBMS</a></li>
<li class="c2"><a href="#" class="c1" onclick="nested()">Nested Queries in SQL</a></li>
<li class="c2"><a href="#" class="c1" onclick="difference()">Join operation Vs Nested query in DBMS</a></li>
<li class="c2"><a href="https://docs.google.com/forms/d/e/1FAIpQLSeMcWInfDTa-DzeUl0F9sa1QGSTHqwVCDdsk7kJYwWhb_MVkw/viewform?usp=sf_link" class="c1" target="_blank">Quiz-Sql</a></li>
</ul>
</div>
<div class="Content" style="font-family: sans-serif;">
<div class="sql" id="sql">
<h1 class="y">Structured Query Language (SQL)</h1>
<p>Structured Query Language is a standard Database language which is used to create, maintain and retrieve the relational database. Following are some interesting facts about SQL.<br>
SQL is case insensitive. But it is a recommended practice to use keywords (like SELECT, UPDATE, CREATE, etc) in capital letters and use user defined things (liked table name, column name, etc) in small letters.<br>
We can write comments in SQL using “–” (double hyphen) at the beginning of any line.<br>
SQL is the programming language for relational databases (explained below) like MySQL, Oracle, Sybase, SQL Server, Postgre, etc. Other non-relational databases (also called NoSQL) databases like MongoDB, DynamoDB, etc do not use SQL<br>
Although there is an ISO standard for SQL, most of the implementations slightly vary in syntax. So we may encounter queries that work in SQL Server but do not work in MySQL.
</p>
<h2>What is Relational Database?</h2>
<p>Relational database means the data is stored as well as retrieved in the form of relations (tables). Table 1 shows the relational database with only one relation called STUDENT which stores ROLL_NO, NAME, ADDRESS, PHONE and AGE of students.</p>
<h3>STUDENT</h3>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> ROLL_NO </th>
<th> NAME </th>
<th> ADDRESS </th>
<th> PHONE </th>
<th> AGE </th>
</tr>
<tr>
<th> 1</th>
<th> RAM </th>
<th> DELHI </th>
<th> 9455123451</th>
<th> 18 </th>
</tr>
<tr>
<th> 2</th>
<th> RAMESH </th>
<th> GURGAON </th>
<th>9652431543 </th>
<th>18 </th>
</tr>
<tr>
<th> 3</th>
<th> SUJIT </th>
<th> ROHTAK </th>
<th>9156253131 </th>
<th> 20</th>
</tr>
<tr>
<th>4 </th>
<th> SURESH </th>
<th> DELHI </th>
<th>9156768971 </th>
<th> 18 </th>
</tr>
</table>
<p>These are some important terminologies that are used in terms of relation.<br>
<b>Attribute:</b> Attributes are the properties that define a relation. e.g.; ROLL_NO, NAME etc.<br>
<b>Tuple: </b>Each row in the relation is known as tuple. The above relation contains 4 tuples, one of which is shown as:</p>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> 1</th>
<th> RAM </th>
<th> DELHI </th>
<th> 9455123451 </th>
<th>18</th>
</tr>
</table>
<p><b>Degree:</b> The number of attributes in the relation is known as degree of the relation. The STUDENT relation defined above has degree 5.<br>
<b>Cardinality:</b> The number of tuples in a relation is known as cardinality. The STUDENT relation defined above has cardinality 4.<br>
<b>Column:</b> Column represents the set of values for a particular attribute. The column ROLL_NO is extracted from relation STUDENT.</p>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> ROLL_NO </th>
</tr>
<tr>
<th>1 </th>
</tr>
<tr>
<th>2 </th>
</tr>
<tr>
<th>3 </th>
</tr>
<tr>
<th>4 </th>
</tr>
</table>
<p>The queries to deal with relational database can be categories as:<br>
<b>Data Definition Language:</b> It is used to define the structure of the database. e.g; CREATE TABLE, ADD COLUMN, DROP COLUMN and so on.<br><br>
<b>Data Manipulation Language: </b>It is used to manipulate data in the relations. e.g.; INSERT, DELETE, UPDATE and so on.<br><br>
<b>Data Query Language: </b>It is used to extract the data from the relations. e.g.; SELECT<br><br>
So first we will consider the Data Query Language. A generic query to retrieve from a relational database is:</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT [DISTINCT] Attribute_List FROM R1,R2….RM<br>
[WHERE condition]<br>
[GROUP BY (Attributes)[HAVING condition]]<br>
[ORDER BY(Attributes)[DESC]];
</div>
<p>Part of the query represented by statement 1 is compulsory if you want to retrieve from a relational database. The statements written inside [] are optional. We will look at the possible query combination on relation shown in Table 1.<br><br>
Case 1: If we want to retrieve attributes ROLL_NO and NAME of all students, the query will be:
</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">SELECT ROLL_NO, NAME FROM STUDENT;
</div><br>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> ROLL_NO </th>
<th> NAME </th>
</tr>
<tr>
<th> 1</th>
<th> RAM </th>
</tr>
<tr>
<th> 2</th>
<th> RAMESH </th>
</tr>
<tr>
<th>3 </th>
<th> SUJIT </th>
</tr>
<tr>
<th>4 </th>
<th> SURESH </th>
</tr>
</table>
<p>Case 2: If we want to retrieve ROLL_NO and NAME of the students whose ROLL_NO is greater than 2, the query will be:</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT ROLL_NO, NAME FROM STUDENT <br>
WHERE ROLL_NO>2;
</div><br>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> ROLL_NO </th>
<th> NAME </th>
</tr>
<tr>
<th>3</th>
<th> SUJIT </th>
</tr>
<tr>
<th>4</th>
<th> SURESH </th>
</tr>
</table>
<p>CASE 3: If we want to retrieve all attributes of students, we can write * in place of writing all attributes as:</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT * FROM STUDENT <br>
WHERE ROLL_NO>2;
</div><br>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> ROLL_NO </th>
<th> NAME </th>
<th> ADDRESS </th>
<th> PHONE </th>
<th> AGE </th>
</tr>
<tr>
<th>3</th>
<th> SUJIT </th>
<th> ROHTAK </th>
<th>9156253131</th>
<th>20</th>
</tr>
<tr>
<th>4</th>
<th> SURESH </th>
<th> DELHI </th>
<th>9156768971</th>
<th>18</th>
</tr>
</table>
<p>CASE 4: If we want to represent the relation in ascending order by AGE, we can use ORDER BY clause as:</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT * FROM STUDENT ORDER BY AGE;
</div><br>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> ROLL_NO </th>
<th> NAME </th>
<th> ADDRESS </th>
<th> PHONE </th>
<th> AGE </th>
</tr>
<tr>
<th>1</th>
<th> RAM </th>
<th> DELHI </th>
<th>9455123451</th>
<th>18</th>
</tr>
<tr>
<th>2</th>
<th> RAMESH </th>
<th> GURGAON </th>
<th>9652431543</th>
<th>18</th>
</tr>
<tr>
<th>4</th>
<th> SURESH </th>
<th> DELHI </th>
<th>9156768971</th>
<th>18</th>
</tr>
<tr>
<th>3</th>
<th> SUJIT </th>
<th> ROHTAK </th>
<th>9156253131</th>
<th>20</th>
</tr>
</table>
<p>Note: ORDER BY AGE is equivalent to ORDER BY AGE ASC. If we want to retrieve the results in descending order of AGE, we can use ORDER BY AGE DESC.<br>
CASE 5: If we want to retrieve distinct values of an attribute or group of attribute, DISTINCT is used as in:</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT DISTINCT ADDRESS FROM STUDENT;
</div><br>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> ADDRESS</th>
</tr>
<tr>
<th> DELHI</th>
</tr>
<tr>
<th> GURGAON</th>
</tr>
<tr>
<th> ROHTAK </th>
</tr>
</table>
<p>If DISTINCT is not used, DELHI will be repeated twice in result set. Before understanding GROUP BY and HAVING, we need to understand aggregations functions in SQL.<br><br>
<b>AGGRATION FUNCTIONS:</b> Aggregation functions are used to perform mathematical operations on data values of a relation. Some of the common aggregation functions used in SQL are:<br><br>
<b>COUNT: </b>Count function is used to count the number of rows in a relation. e.g;</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT COUNT (PHONE) FROM STUDENT;<br>
COUNT(PHONE)<br>
4
</div>
<p><b>SUM: </b>SUM function is used to add the values of an attribute in a relation. e.g;</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT SUM (AGE) FROM STUDENT;<br>
SUM(AGE)<br>
74
</div>
<p>In the same way, MIN, MAX and AVG can be used. As we have seen above, all aggregation functions return only 1 row.<br>
<b>AVERAGE: </b>It gives the average values of the tupples. It is also defined as sum divided by count values.</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
Syntax:AVG(attributename)<br>
OR<br>
Syntax:SUM(attributename)/COUNT(attributename)
</div>
<p>The above mentioned syntax also retrieves the average value of tupples.<br>
<b>MAXIMUM:</b>It extracts the maximum value among the set of tupples.</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
Syntax:MAX(attributename)
</div>
<p><b>MINIMUM:</b>It extracts the minimum value amongst the set of all the tupples.</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
Syntax:MIN(attributename)
</div>
<p><b>GROUP BY: </b>Group by is used to group the tuples of a relation based on an attribute or group of attribute. It is always combined with aggregation function which is computed on group. e.g.;</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT ADDRESS, SUM(AGE) FROM STUDENT<br>
GROUP BY (ADDRESS);
</div>
<p>In this query, SUM(AGE) will be computed but not for entire table but for each address. i.e.; sum of AGE for address DELHI(18+18=36) and similarly for other address as well. The output is:</p>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> ADDRESS </th>
<th> SUM(AGE)</th>
</tr>
<tr>
<th> DELHI </th>
<th>36</th>
</tr>
<tr>
<th> GURGAON </th>
<th>18</th>
</tr>
<tr>
<th> ROHTAK </th>
<th>20</th>
</tr>
</table>
<p>If we try to execute the query given below, it will result in error because although we have computed SUM(AGE) for each address, there are more than 1 ROLL_NO for each address we have grouped. So it can’t be displayed in result set. We need to use aggregate functions on columns after SELECT statement to make sense of the resulting set whenever we are using GROUP BY.</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT ROLL_NO, ADDRESS, SUM(AGE) FROM STUDENT<br>
GROUP BY (ADDRESS);
</div>
<p>NOTE: An attribute which is not a part of GROUP BY clause can’t be used for selection. Any attribute which is part of GROUP BY CLAUSE can be used for selection but it is not mandatory. But we could use attributes which are not a part of the GROUP BY clause in an aggregrate function.</p>
</div>
<div class="join" id="joi">
<h1 class="y">Inner Join vs Outer Join</h1>
<h2>What is Join?</h2>
<p>An SQL Join is used to combine data from two or more tables, based on a common field between them. For example, consider the following two tables.</p>
<h3>Student Table</h3>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> ENROLLNO </th>
<th> STUDENTNAME </th>
<th> ADDRESS </th>
</tr>
<tr>
<th>1001</th>
<th> geek1</th>
<th> geeksquiz1</th>
</tr>
<tr>
<th>1002</th>
<th> geek2</th>
<th> geeksquiz2</th>
</tr>
<tr>
<th>1003</th>
<th> geek3</th>
<th> geeksquiz3</th>
</tr>
<tr>
<th>1004</th>
<th> geek4</th>
<th> geeksquiz4</th>
</tr>
</table>
<h3>StudentCourse Table</h3>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> COURSEID </th>
<th> ENROLLNO </th>
</tr>
<tr>
<th>1</th>
<th>1001</th>
</tr>
<tr>
<th>2</th>
<th>1001</th>
</tr>
<tr>
<th>3</th>
<th>1001</th>
</tr>
<tr>
<th>1</th>
<th>1002</th>
</tr>
<tr>
<th>2</th>
<th>1003</th>
</tr>
</table>
<p>Following is join query that shows names of students enrolled in different courseIDs.</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT StudentCourse.CourseID,Student.StudentName<br>
FROM Student<br>
INNER JOIN StudentCourse <br>
ON StudentCourse.EnrollNo = Student.EnrollNo<br>
ORDER BY StudentCourse.CourseID
</div>
<p>Note: INNER is optional above. Simple JOIN is also considered as INNER JOIN</p>
<p>The above query would produce following result.</p>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> COURSEID </th>
<th> STUDENTNAME </th>
</tr>
<tr>
<th>1</th>
<th> geek1</th>
</tr>
<tr>
<th>1</th>
<th> geek2</th>
</tr>
<tr>
<th>2</th>
<th> geek1</th>
</tr>
<tr>
<th>2</th>
<th> geek3</th>
</tr>
<tr>
<th>3</th>
<th> geek1</th>
</tr>
</table>
<p>What is the difference between inner join and outer join?<br>
Outer Join is of 3 types</p>
<ul>
<li style="margin-left: 40px;color:#696969;font-size: 18px;padding: 5px;"> 1) Left outer join</li>
<li style="margin-left: 40px;color:#696969;font-size: 18px;padding: 5px;"> 2) Right outer join</li>
<li style="margin-left: 40px;color:#696969;font-size: 18px;padding: 5px;"> 3) Full Join</li>
</ul>
<p><b>1) Left outer join</b> returns all rows of table on left side of join. The rows for which there is no matching row on right side, result contains NULL in the right side.</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT Student.StudentName,
StudentCourse.CourseID<br>
FROM Student<br>
LEFT OUTER JOIN StudentCourse<br>
ON StudentCourse.EnrollNo = Student.EnrollNo<br>
ORDER BY StudentCourse.CourseID
</div>
<p>Note: OUTER is optional above. Simple LEFT JOIN is also considered as LEFT OUTER JOIN</p>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> STUDENTNAME </th>
<th> COURSEID </th>
</tr>
<tr>
<th> geek4</th>
<th> NULL </th>
</tr>
<tr>
<th> geek2</th>
<th>1</th>
</tr>
<tr>
<th> geek1</th>
<th>1</th>
</tr>
<tr>
<th> geek1</th>
<th>2</th>
</tr>
<tr>
<th> geek3</th>
<th>2</th>
</tr>
<tr>
<th> geek1</th>
<th>3</th>
</tr>
</table>
<p><b>2) Right Outer Join</b> is similar to Left Outer Join (Right replaces Left everywhere)<br>
<b>3) Full Outer Join</b> Contains results of both Left and Right outer joins.<br></p>
</div>
<div class="clause" id="cla">
<h1 class="y">Having vs Where Clause in SQL</h1>
<P>The difference between the having and where clause in SQL is that the where clause cannot be used with aggregates, but the having clause can.</p>
<p>The where clause works on row’s data, not on aggregated data. Let us consider below table ‘Marks’.</p>
<table style="width:50%;margin-left: 200px;">
<tr>
<th> Student </th>
<th> Course </th>
<th> Score </th>
</tr>
<tr>
<th> a </th>
<th> c1</th>
<th>40 </th>
</tr>
<tr>
<th> a </th>
<th> c2 </th>
<th> 50</th>
</tr>
<tr>
<th> b </th>
<th> c3 </th>
<th> 60</th>
</tr>
<tr>
<th> d </th>
<th> c1 </th>
<th>70 </th>
</tr>
<tr>
<th> e </th>
<th> c2</th>
<th>80 </th>
</tr>
</table>
<p>Consider the query</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT Student, Score FROM Marks WHERE Score >=40
</div>
<p>This would select data row by row basis.</p>
<p>The having clause works on aggregated data.</p>
<p>For example, output of below query</p>
<div style="background-color: lightgrey;margin-left: 400px;width:400px;padding: 15px;">
SELECT Student, SUM(score) AS total FROM Marks GROUP BY Student
</div>
<br>