Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

Name: Aman Chavan

PRN:120A7016
DIV: B(SE-ECS)

EXPERIMENT NO. 5
AIM: Perform Simple queries, string manipulation operations and aggregate functions.
.

OBJECTIVES: To perform various aggregate functions to extract data from 2 (or


more) tables.

THEORY: AGGREGATE Functions: - In addition to simply retrieving data,


computation or summarization can also be performed using a powerful class of
constructs for computing aggregate values such as MIN and SUM. These features
represent a significant extension of relational algebra. SQL supports five aggregate
operations, which can be applied on any column, say A, of a relation:

1. COUNT (A):- The number of values in the A column.

Or COUNT (DISTINCT A): The number of unique values in the A column.

Ex:-1) To count number SIDs of sailors in Sailors table

SELECT COUNT (SID) FROM SAILORS;

2) To count numbers of boats booked in Reserves table.

SELECT COUNT (DISTINCT BID) FROM RESERVES;

3) To count number of Boats in Boats table.

SELECT COUNT (*) FROM BOATS;

2. SUM (A) :- The sum of all values in the A column.

Or SUM (DISTINCT A): The sum of all unique values in the A column.

Department of EXTC, SIES GST


Ex:- 1) To find sum of rating from Sailors

SELECT SUM (RATING) FROM SAILORS;

2) To find sum of distinct age of Sailors (Duplicate ages are eliminated).

SELECT SUM (DISTINCT AGE) FROM SAILORS;

3. AVG (A) :- The average of all values in the A column.

Or AVG (DISTINCT A): The average of all unique values in the A column.

Ex:-1) To display average age of Sailors.

SELECT AVG (AGE) FROM SAILORS;

2) To find average of distinct age of Sailors (Duplicate ages are eliminated).

SELECT AVG (DISTINCT AGE) FROM SAILORS;

4. MAX (A) :- The maximum value in the A column.

Ex:-To find age of Oldest Sailor.

SELECT MAX (AGE) FROM SAILORS;

5. MIN (A) :-The minimum value in the A column.

Ex:- To find age of Youngest Sailor.

SELECT MIN (AGE) FROM SAILORS;

Note that it does not make sense to specify DISTINCT in conjunction with MIN or
MAX(although SQL does not preclude this).

ORDER BY Clause: - The ORDER BY keyword is used to sort the result-set by a


specified column. The ORDER BY keyword sorts the records in ascending order by

Department of EXTC, SIES GST


default. To sort the records in a descending order, DESC keyword can be used. The
general syntax is:

SELECT ATT_LIST FROM TABLE_LIST ORDER BY ATT_NAMES [ASC |


DESC];

Ex: - 1) Display all the sailors according to their ages.

SELECT * FROM SAILORS ORDER BY AGE;

2) Display all the sailors according to their ratings (topper first).

SELECT * FROM SAILORS ORDER BY RATING DESC;

3) Displays all the sailors according to rating, if rating is same then sort according to
age.

SELECT * FROM SAILORS ORDER BY RATING, AGE;

GROUP BY and HAVING Clauses: - Thus far, aggregate operations are applied to all
(qualifying) rows in a relation. Often we want to apply aggregate operations to each of
a number of groups of rows in a relation, where the number of groups depends on the
relation instance. For this purpose we can use Group by clause.

GROUP BY: - Group by is used to make each a number of groups of rows in a relation,
where the number of groups depends on the relation instances. The general syntax is

SELECT [DISTINCT] ATT_LIST FROM TABLE_LIST WHERE CONDITION


GROUP BY

GROUPING_LIST;

Ex: - Find the age of the youngest sailor for each rating level.

SELECT S.RATING, MIN (S.AGE) FROM SAILORS S GROUP BY S.RATING;

Department of EXTC, SIES GST


HAVING: - The extension of GROUP BY is HAVING clause which can be used to
specify the qualification over group.

The general syntax is SELECT [DISTINCT] ATT_LIST FROM TABLE_LIST


WHERE CONDITION GROUP BY GROUPING_LIST HAVING
GROUP_CONDITIION;

Ex: - Find the age of youngest sailor with age >= 18 for each rating with at least 2 such
sailors.

SELECT S.RATING, MIN (S.AGE) AS MINAGE FROM SAILORS S


WHERES.AGE >= 18 GROUP BY S.RATING HAVING COUNT (*) > 1;

LIKE: The LIKE operator is used in a WHERE clause to search for a specified
pattern in a column.

There are two wildcards used in conjunction with the LIKE operator:

• % - The percent sign represents zero, one, or multiple characters


• _ - The underscore represents a single character

Syntax: SELECT column1, column2, ...


FROM table_name
WHERE columnN LIKE pattern

Examples:

1. selects all customers with a CustomerName starting with "a":

SELECT * FROM Customers


WHERE CustomerName LIKE 'a%';

2. selects all customers with a CustomerName that have "r" in the second
position:

SELECT * FROM Customers


WHERE CustomerName LIKE '_r%';

Alias: The use of table aliases is to rename a table in a specific SQL statement. The
renaming is a temporary change and the actual table name does not change in the
database. The column aliases are used to rename a table's columns for the purpose of a
particular SQL query.

Department of EXTC, SIES GST


The basic syntax of a table alias is as follows.

SELECT column1, column2....

FROM table_name AS alias_name

WHERE [condition];

The basic syntax of a column alias is as follows.

SELECT column_name AS alias_name

FROM table_name

WHERE [condition];

Example:

SELECT C.ID, C.NAME, C.AGE, O.AMOUNT

FROM CUSTOMERS AS C, ORDERS AS O

WHERE C.ID = O.CUSTOMER_ID;

Distinct: The SELECT DISTINCT statement is used to return only distinct (different)
values.

Syntax: SELECT DISTINCT column1, column2, ...


FROM table_name;

Example: SELECT DISTINCT Country FROM Customers;

Code:

Department of EXTC, SIES GST


Department of EXTC, SIES GST
Department of EXTC, SIES GST
Department of EXTC, SIES GST
Department of EXTC, SIES GST
Conclusion: We learnt to use simple aggregate functions.

Department of EXTC, SIES GST

You might also like