Вы находитесь на странице: 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 now construct queries involving the
union, intersect, and minus operations over two sets.
1. UNION:
UNION is used to combine the results of two or more SELECT statements. However
it will eliminate duplicate rows from its result set. In case of union, number of
columns and data type must be same in both the tables.
Syntax:
SELECT column_name(s) FROM table1
UNION
SELECT column_name(s) FROM table2;
2. UNION ALL:
This operation is similar to UNION but it allows the duplicate rows.
Syntax:
SELECT column_name(s) FROM table1
UNION ALL
SELECT column_name(s) FROM table2;
3. INTERSECT:
INTERSECT operation is used to combine two SELECT statements, but it only
returns the records which are common from both SELECT statements. In case of
INTERSECT the number of columns and data type must be same.
Syntax:
SELECT column_name(s) FROM table1
INTERSECT
SELECT column_name(s) FROM table2;
4. MINUS:
MINUS operation combines result of two SELECT statements and return only those
results which belongs to first set of result.
Syntax:
SELECT column_name(s) FROM table1
MINUS
SELECT column_name(s) FROM table2;
AGGREGATE FUNCTIONS IN SQL
SQL has many built-in functions for performing processing on string or numeric data known
as Aggregate functions. SQL aggregate functions return a single value, calculated from
values in a column. Following are the list of the aggregate functions from SQL.
1. AVG()

The AVG() function returns the average value of a numeric column.


Syntax:
SELECT AVG(column_name) FROM table_name

2. COUNT()
The COUNT() function returns the number of rows that matches a specified criteria.
Syntax:
SELECT COUNT(column_name) FROM table_name;
3. FIRST()
The FIRST() function returns the first value of the selected column.
Syntax:
SELECT FIRST(column_name) FROM table_name;
4. LAST()
The LAST() function returns the last value of the selected column.
Syntax:
SELECT LAST(column_name) FROM table_name;
5. MAX()
The MAX() function returns the largest value of the selected column.
Syntax:
SELECT MAX(column_name) FROM table_name;
6. MIN()
The MIN() function returns the smallest value of the selected column.
Syntax:
SELECT MIN(column_name) FROM table_name;
7. SUM()
The SUM() function returns the total sum of a numeric column.
Syntax:
SELECT SUM(column_name) FROM table_name;
JOINS IN SQL
An SQL JOIN clause is used to combine rows from two or more tables, based on a common
field between them. There are different types of joins available in SQL as shown below.
1. INNER JOIN:
The INNER JOIN keyword selects all rows from both tables as long as there is a
match between the columns in both tables.
Syntax:
SELECT column_name(s)
FROM table1
INNER JOIN table2
ON table1.column_name=table2.column_name;

2. LEFT JOIN:
The LEFT JOIN keyword returns all rows from the left table (table1), with the
matching rows in the right table (table2). The result is NULL in the right side when
there is no match.
Syntax:
SELECT column_name(s)
FROM table1
LEFT JOIN table2
ON table1.column_name=table2.column_name;
3. RIGHT JOIN:
The RIGHT JOIN keyword returns all rows from the right table (table2), with the
matching rows in the left table (table1). The result is NULL in the left side when there
is no match.
Syntax:
SELECT column_name(s)
FROM table1
RIGHT JOIN table2
ON table1.column_name=table2.column_name;
4. FULL OUTER JOIN:
The FULL OUTER JOIN keyword returns all rows from the left table (table1) and
from the right table (table2).
The FULL OUTER JOIN keyword combines the result of both LEFT and RIGHT
joins.
Syntax:
SELECT column_name(s)
FROM table1
FULL OUTER JOIN table2
ON table1.column_name=table2.column_name;
CONCLUSION: Thus we have studied and implemented set operations, aggregate functions,
and joins in SQL.

AGGREGATE FUNCTIONS QUERIES:


SQL> select * from StudBio;
PROFNO NAME
DOB
AGE ADDRESS
--------------- ----------- ------------------ ---------- ----------------1
Sainath 23-DEC-93
21
Jogeshwari
2
Pratik
16-OCT-95
19
Andheri
3
Shahid 30-MAY-95
20
Malad
4
Prasad 24-OCT-93
21
Vashi
5
Aditya 10-NOV-96
18
Bandra
SQL> SELECT AVG(Age) as Avg_Age from StudBio;
AVG_AGE
----------------19.8
SQL> SELECT count(*) as No_Of_Stud from StudBio;
NO_OF_STUD
-------------------5
SQL> SELECT MAX(Age) as HighestAge from StudBio;
HIGHESTAGE
--------------------21
SQL> SELECT MIN(Age) as SmallestAge from StudBio;
SMALLESTAGE
----------------------18
SQL> SELECT SUM(Age) as Sum_of_Age from StudBio;
SUM_OF_AGE
--------------------99

JOINS QUERIES:
SQL> SELECT * FROM Students;
ENROLLNO FIRSTNAME LASTNAME BRANC SEM GPA
------------------ ----------------- ----------------- ------------- -------- ------478 Sainath
Parkar
Comp
3 7.4
480 Pratik
Sutar
Comp
3 8.2
479 Shahid
Ansari
EXTC
39
472 Faizan
Khan
MECH
3 10
470 Pratik
Khadtale
EXTC
3 9.7
SQL> SELECT * FROM StudBio;
PROFNO NAME DOB
AGE ADDRESS ENROLLNO
--------------- ---------- ----------------- --------------- -------------- ----------------1 Sainath 23-DEC-93
21 Jogeshwari 478
2 Pratik 16-OCT-95
19 Andheri
480
3 Shahid 30-MAY-95
20 Malad
479
4 Prasad 24-OCT-93
21 Vashi
487
5 Aditya 10-NOV-96
18 Bandra
488
INNER JOIN:
SQL> SELECT Students.EnrollNo, Students.FirstName, StudBio.Age, StudBio.Address
FROM Students INNER JOIN StudBio ON Students.EnrollNo=StudBio.EnrollNo;
ENROLLNO FIRSTNAME AGE ADDRESS
----------------- ------------------ ---------- -------------------478 Sainath
21 Jogeshwari
479 Shahid
20 Malad
480 Pratik
19 Andheri
LEFT OUTER JOIN:
SQL> SELECT Students.EnrollNo, Students.FirstName, StudBio.Age, StudBio.Address
FROM Students LEFT JOIN StudBio ON Students.EnrollNo=StudBio.EnrollNo;
ENROLLNO FIRSTNAME
AGE ADDRESS
------------------ ------------------ ----------- -------------------478 Sainath
21 Jogeshwari
480 Pratik
19 Andheri
479 Shahid
20 Malad
470 Pratik
472 Faizan

RIGHT OUTER JOIN:


SQL> SELECT Students.EnrollNo, Students.FirstName, StudBio.Age, StudBio.Address
FROM Students RIGHT JOIN StudBio ON Students.EnrollNo=StudBio.EnrollNo;
ENROLLNO FIRSTNAME
AGE ADDRESS
------------------ ----------------- ------------ -------------------478 Sainath
21 Jogeshwari
480 Pratik
19 Andheri
479 Shahid
20 Malad
18 Bandra
21 Vashi
FULL OUTER JOIN:
SQL> SELECT Students.EnrollNo, Students.FirstName, StudBio.Age, StudBio.Address
FROM Students FULL JOIN StudBio ON Students.EnrollNo=StudBio.EnrollNo;
ENROLLNO FIRSTNAME
AGE ADDRESS
------------------ ------------------ ----------- -------------------478 Sainath
21 Jogeshwari
480 Pratik
19 Andheri
479 Shahid
20 Malad
470 Pratik
472 Faizan
18 Bandra
21 Vashi
SET OPERATIONS QUERIES:
UNION:
SQL> SELECT FirstName from Students UNION SELECT Name FROM StudBio;
FIRSTNAME
-----------------Aditya
Faizan
Prasad
Pratik
Sainath
Shahid

UNION ALL:
SQL> SELECT FirstName FROM Students UNION ALL SELECT Name FROM StudBio;
FIRSTNAME
-----------------Sainath
Pratik
Shahid
Faizan
Pratik
Sainath
Pratik
Shahid
Prasad
Aditya
INTERSECT:
SQL> SELECT FirstName FROM Students INTERSECT SELECT Name FROM StudBio;
FIRSTNAME
-----------------Pratik
Sainath
Shahid
MINUS:
SQL> SELECT FirstName FROM Students MINUS SELECT Name FROM StudBio;
FIRSTNAME
-----------------Faizan

Вам также может понравиться