to study and implement setoperations, aggregate functions and joins
TRANSCRIPT
-
7/21/2019 To Study and Implement Setoperations, Aggregate Functions and Joins
1/7
AIM:To study and implement set operations, aggregate functions, and joins in SQL.
THEORY:
SET OPERATIONS IN SQL
The SQL operations union, intersect, and except operate on relations and correspond to the
mathematical set-theory operations , , and . We shall no construct !ueries in"ol"ing the
union, intersect, and minus operations o"er to sets.
1. UNION:
#$%&$ is used to com'ine the results of to or more S(L()T statements. *oe"er
it ill eliminate duplicate ros from its result set. %n case of union, num'er of
columns and data type must 'e same in 'oth the ta'les.
Syntax+
S(L()T column_name(s)& table1
#$%&$S(L()T column_name(s)& table2/
2. UNION ALL:
This operation is similar to #$%&$ 'ut it allos the duplicate ros.
Syntax+
S(L()T column_name(s)& table1
#$%&$ 0LL
S(L()T column_name(s)& table2/
3. INTERSECT:
%$T(S()T operation is used to com'ine to S(L()T statements, 'ut it only
returns the records hich are common from 'oth S(L()T statements. %n case of
%$T(S()T the num'er of columns and data type must 'e same.
Syntax+
S(L()T column_name(s)& table1
%$T(S()T
S(L()T column_name(s) & table2/
4. MINUS:
%$#S operation com'ines result of to S(L()T statements and return only those
results hich 'elongs to first set of result. Syntax+
S(L()T column_name(s) & table1
%$#S
S(L()T column_name(s) & table2/
AGGREGATE FUNCTIONS IN SQL
SQL has many 'uilt-in functions for performing processing on string or numeric data 1non
as 2Aggregate functions3. SQL aggregate functions return a single "alue, calculated from
"alues in a column. olloing are the list of the aggregate functions from SQL.
1. AVG()
-
7/21/2019 To Study and Implement Setoperations, Aggregate Functions and Joins
2/7
The 04567 function returns the a"erage "alue of a numeric column.
Syntax+
S(L()T 0456column_name7 & table_name
2. COUNT()
The )$T67 function returns the num'er of ros that matches a specified criteria.
Syntax+
S(L()T )$T6column_name7 & table_name/
3. FIRST()
The %ST67 function returns the first "alue of the selected column.
Syntax+
S(L()T %ST6column_name7 & table_name/
4. LAST()
The L0ST67 function returns the last "alue of the selected column.
Syntax+
S(L()T L0ST6column_name7 & table_name/
5. MAX()
The 0867 function returns the largest "alue of the selected column.
Syntax+
S(L()T 086column_name7 & table_name/
6. MIN()
The %$67 function returns the smallest "alue of the selected column.
Syntax+
S(L()T %$6column_name7 & table_name/
. SUM()
The S#67 function returns the total sum of a numeric column.
Syntax+
S(L()T S#6column_name7 & table_name/
!OINS IN SQL0n SQL 9&%$ clause is used to com'ine ros from to or more ta'les, 'ased on a common
field 'eteen them. There are different types of joins a"aila'le in SQL as shon 'elo.
1. INNER !OIN:
The %$$( 9&%$ 1eyord selects all ros from 'oth ta'les as long as there is a
match 'eteen the columns in 'oth ta'les.
Syntax+
S(L()T column_name(s)
& table1
%$$( 9&%$ table2
&$ table1.column_name=table2.column_name/
-
7/21/2019 To Study and Implement Setoperations, Aggregate Functions and Joins
3/7
2. LEFT !OIN:
The L(T 9&%$ 1eyord returns all ros from the left ta'le 6ta'le:7, ith the
matching ros in the right ta'le 6ta'le;7. The result is $#LL in the right side hen
there is no match.
Syntax+
S(L()T column_name(s)
& table1
L(T 9&%$ table2
&$ table1.column_name=table2.column_name;
3. RIGHT !OIN:
The %5*T 9&%$ 1eyord returns all ros from the right ta'le 6ta'le;7, ith the
matching ros in the left ta'le 6ta'le:7. The result is $#LL in the left side hen thereis no match.
Syntax+
S(L()T column_name(s)
& table1
%5*T 9&%$ table2
&$ table1.column_name=table2.column_name;
4. FULL OUTER !OIN:
The #LL T( 9&%$ 1eyord returns all ros from the left ta'le 6ta'le:7 and
from the right ta'le 6ta'le;7. The #LL T( 9&%$ 1eyord com'ines the result of 'oth L(T and %5*T
joins.
Syntax+
S(L()T column_name(s)
& table1
#LL T( 9&%$ table2
&$ table1.column_name=table2.column_name/
CONCLUSION:Thus e ha"e studied and implemented set operations, aggregate functions,
and joins in SQL.
-
7/21/2019 To Study and Implement Setoperations, Aggregate Functions and Joins
4/7
AGGREGATE FUNCTIONS QUERIES:
SQL< select = from Stud>io/
?&$& $0( @&> 05( 0@@(SS
--------------- ----------- ------------------ ---------- -----------------
: Sainath ;A-@()-BA ;: 9ogeshari
; ?rati1 :C-&)T-BD :B 0ndheri
A Shahid AE-0F-BD ;E alad
G ?rasad ;G-&)T-BA ;: 4ashi
D 0ditya :E-$&4-BC :H >andra
SQL< S(L()T 04560ge7 as 0"gI0ge from Stud>io/
045I05(-----------------
:B.H
SQL< S(L()T count6=7 as $oI&fIStud from Stud>io/
$&I&IST#@
--------------------
D
SQL< S(L()T 0860ge7 as *ighest0ge from Stud>io/
*%5*(ST05(
---------------------
;:
SQL< S(L()T %$60ge7 as Smallest0ge from Stud>io/
S0LL(ST05(
-----------------------
:H
SQL< S(L()T S#60ge7 as SumIofI0ge from Stud>io/
S#I&I05(
---------------------
BB
-
7/21/2019 To Study and Implement Setoperations, Aggregate Functions and Joins
5/7
!OINS QUERIES:
SQL< S(L()T = & Students/
($&LL$& %ST$0( L0ST$0( >0$) S( 5?0
------------------ ----------------- ----------------- ------------- -------- -------
GJH Sainath ?ar1ar )omp A J.G
GHE ?rati1 Sutar )omp A H.;
GJB Shahid 0nsari (8T) A B
GJ; aiKan han ()* A :E
GJE ?rati1 hadtale (8T) A B.J
SQL< S(L()T = & Stud>io/
?&$& $0( @&> 05( 0@@(SS ($&LL$&--------------- ---------- ----------------- --------------- -------------- -----------------
: Sainath ;A-@()-BA ;: 9ogeshari GJH
; ?rati1 :C-&)T-BD :B 0ndheri GHE
A Shahid AE-0F-BD ;E alad GJB
G ?rasad ;G-&)T-BA ;: 4ashi GHJ
D 0ditya :E-$&4-BC :H >andra GHH
INNER !OIN:
SQL< S(L()T Students.(nroll$o, Students.irst$ame, Stud>io.0ge, Stud>io.0ddress
& Students %$$( 9&%$ Stud>io &$ Students.(nroll$oMStud>io.(nroll$o/
($&LL$& %ST$0( 05( 0@@(SS
----------------- ------------------ ---------- --------------------
GJH Sainath ;: 9ogeshari
GJB Shahid ;E alad
GHE ?rati1 :B 0ndheri
LEFT OUTER !OIN:
SQL< S(L()T Students.(nroll$o, Students.irst$ame, Stud>io.0ge, Stud>io.0ddress
& Students L(T 9&%$ Stud>io &$ Students.(nroll$oMStud>io.(nroll$o/
($&LL$& %ST$0( 05( 0@@(SS
------------------ ------------------ ----------- --------------------
GJH Sainath ;: 9ogeshari
GHE ?rati1 :B 0ndheri
GJB Shahid ;E alad
GJE ?rati1
GJ; aiKan
-
7/21/2019 To Study and Implement Setoperations, Aggregate Functions and Joins
6/7
RIGHT OUTER !OIN:
SQL< S(L()T Students.(nroll$o, Students.irst$ame, Stud>io.0ge, Stud>io.0ddress
& Students %5*T 9&%$ Stud>io &$ Students.(nroll$oMStud>io.(nroll$o/
($&LL$& %ST$0( 05( 0@@(SS
------------------ ----------------- ------------ --------------------
GJH Sainath ;: 9ogeshari
GHE ?rati1 :B 0ndheri
GJB Shahid ;E alad
:H >andra
;: 4ashi
FULL OUTER !OIN:
SQL< S(L()T Students.(nroll$o, Students.irst$ame, Stud>io.0ge, Stud>io.0ddress
& Students #LL 9&%$ Stud>io &$ Students.(nroll$oMStud>io.(nroll$o/
($&LL$& %ST$0( 05( 0@@(SS
------------------ ------------------ ----------- --------------------
GJH Sainath ;: 9ogeshari
GHE ?rati1 :B 0ndheri
GJB Shahid ;E alad
GJE ?rati1
GJ; aiKan
:H >andra;: 4ashi
SET OPERATIONS QUERIES:
UNION:
SQL< S(L()T irst$ame from Students #$%&$ S(L()T $ame & Stud>io/
%ST$0(
------------------
0dityaaiKan
?rasad
?rati1
Sainath
Shahid
-
7/21/2019 To Study and Implement Setoperations, Aggregate Functions and Joins
7/7
UNION ALL:
SQL< S(L()T irst$ame & Students #$%&$ 0LL S(L()T $ame & Stud>io/
%ST$0(
------------------
Sainath
?rati1
Shahid
aiKan
?rati1
Sainath
?rati1
Shahid
?rasad0ditya
INTERSECT:
SQL< S(L()T irst$ame & Students %$T(S()T S(L()T $ame & Stud>io/
%ST$0(
------------------
?rati1
Sainath
Shahid
MINUS:
SQL< S(L()T irst$ame & Students %$#S S(L()T $ame & Stud>io/
%ST$0(
------------------
aiKan