2. Agenda
• Uses of Set Operation(Union , Intersect , Except).
• Uses of Aggregate Functions(Avg , Max , Min , Sum , Count).
• Uses of Some Other SQL Functions.
3. Uses of Set Operations
• The SQL operations union, intersect and except operate on relations
and corresponds to the algebra operations U, ∩ and -.
Union:
• It is used to combine the result set of two select queries.
• Eliminate duplicates.
Union All:
For duplicates, use union all clause.
Intersect:
The intersection operation between two selections returns only the common data sets or rows between them.
* Eliminate duplicates.
Intersect All:
For duplicates, use intersect all clause.
Except/Minus:
Except operations are needed to extract those tuples that are in one
SQL query but not in other query.
* Eliminate duplicates.
4. Uses of Set Operations
Set Operations can be demonstrated as follows:
5. Uses of Set Operations
Implementations of Set Operations:
Table: person Table:person1
select id from person union select id from person1;
6. Uses of Set Operations
select id from person union all select id from person1;
select id from person intersect select id from person1;
7. Uses of Set Operations
Except/Minus:
person1 person
select id from person1 minus select id from person;
8. Uses of Aggregate Functions
• SQL offers five built-in aggregate functions:
1. Average: avg()
2. Minimum: min()
3. Maximum: max()
4. Total: sum()
5. Count: count()
avg():The AVG() function calculates the average of a set of values.
Min(): The MIN() aggregate function returns the lowest value (minimum) in a set of non-NULL values.
Max(): The MAX() aggregate function returns the highest value (maximum) in a set of non-NULL values.
Sum(): The SUM() function returns the total sum of a numeric column.
Count(): The COUNT() function returns the number of rows in a database table.
9. Uses of Aggregate Functions
Implementation of Aggregate Functions:
Table: person
select avg(ID) as New_ID from person;
select min(ID) as New_ID from person;
10. Uses of Aggregate Functions
• select max(ID) as New_ID from person;
select sum(ID) as New_ID from person;
• select count(ID) as New_ID from person;
11. Uses of Some Other SQL Functions.
• There is so many functions in SQL such as:
Abs():The ABS() function returns the absolute value of a number.
select abs(-13) from dual;
Power():The POWER() function returns the value of a number raised to the power of another number.
select power(5,2) from dual;
Round(): The ROUND() function rounds a number to a specified number of decimal places.
select Round(3.75) from dual;
12. Uses of Some Other SQL Functions.
Sqrt(): The SQRT() function returns the square root of a number.
select sqrt(25) from dual;
Truncate(): The TRUNCATE() function truncates a number to the specified number of decimal places.
select truncate(3.1416,2) from dual;