Database Query using SQL (Contd…)
Aggregate Functions
Aggregate functions are also called multiple row functions. These functions work on a set of records as
a whole, and return a single value for each column of the records on which the function is applied.
Function Description
Max(column) Returns the largest value from the specified column.
MIN(column) Returns the smallest value from the specified column.
AVG(column) Returns the average of the values in the specified column.
SUM(column) Returns the sum of the values for the specified column.
COUNT(column) Returns the number of values in the specified column ignoring the NULL values.
COUNT(*) Returns the number of records in a table including NULL values
GROUP BY in SQL:
• Group by clause groups the rows together that contain the same values in a specified column.
• We can use the aggregate functions (COUNT, MAX, MIN, AVG and SUM) to work on the grouped
values.
• HAVING Clause in SQL is used to specify conditions on the rows with GROUP BY clause.
Points to be remembered:
The group by clause always written after from clause.
The column which is to be grouped will be written in the select clause and group by clause.
SYNTAX:
SELECT [Column] group-function (Column
FROM table
[WHERE Condition]
[GROUP BY Column1,column2,column3];
Having clause:
The having clause is used to apply a condition in the group by query results.
It is working like where clause but there is one difference between them
i.e. the WHERE clause can’t include aggregate functions
whereas the HAVING clause can include them.
Syntax:
SELECT [Column] group-function (Column
FROM table
[WHERE Condition]
[GROUP BY Column1,column2,column3]
HAVING [<Condition1….. condition N];
Cartesian Product:
• Cartesian product operation combines tuples from two relations.
• It results in pair of rows regardless of same values on common attribute.
• It is denoted as ‘X’.
The degree of the resulting relation= the sum of the degrees of both the relations
(no of columns of table1+no of columns of table2)
The cardinality of the resulting relation = product of the cardinality of relations
(no of rows of table1+ no of rows of table2)
Cartesian product on two tables
• When more than one table is to be used in a query, then we must specify the table names by separating
commas in the FROM clause
• MySql will first apply cartesian product on specified tables to have a single table.
• Example: Cartesian product of two tables Dance and Music are Dance x Music
Example:
a) Display all possible combinations of tuples of relations DANCE and MUSIC
SELECT * FROM DANCE, MUSIC;
b) Display only those rows such that the attribute name in both have the same value.
SELECT * FROM DANCE D, MUSIC M WHERE D.Name = M.Name;
We used table aliases (shortened name) (D for DANCE and M for MUSIC)
JOIN on two tables:
JOIN operation combines tuples (rows ) from two tables on specified conditions.
Using the JOIN clause of SQL, we specify conditions on the related attributes (columns) of two tables
within the FROM clause.
Such an attribute is the primary key in one table and foreign key in another table.
EXAMPLE
UCode is Primary Key in table UNIFORM.
UCode Is the foreign key in the table COST
Therefore, Ucode is a common attribute between the two tables which can be used to fetch the common
data from both the tables.
1. List the UCode, UName, UColor, Size and Price of related tuples of tables UNIFORM and COST.
In three ways we CAN USE Joins
a) Using condition in where clause (EQUI JOIN – COMPARISON OPERATOR (=) SIGN
SELECT * FROM UNIFORM U, COST C WHERE U.UCode = C.UCode;
b) Explicit use of JOIN clause
SELECT * FROM UNIFORM U JOIN COST C ON U.Ucode=C.Ucode;
c) Explicit use of NATURAL JOIN clause
NATURAL JOIN (removes redundant attribute)
SELECT * FROM UNIFORM NATURAL JOIN COST;
Points to be considered while applying JOIN operations on two or more relations:
• If two tables are to be joined on equality condition on the common attribute, then one may use JOIN
with ON clause or NATURAL JOIN in FROM clause.
• If three tables are to be joined on equality condition, then two JOIN or NATURAL JOIN are required.
• In general, N-1 joins are needed to combine N tables on equality condition.
• With JOIN clause, we may use any relational operators to combine tuples of two tables.