0% found this document useful (0 votes)
1 views

Aggregate Functions

The document explains aggregate functions such as SUM, AVG, MAX, MIN, and COUNT, which can be used with or without the GROUP BY clause to analyze groups of records in a database. It also describes Cartesian products and various types of joins, including Natural Join and Equi Join, detailing how they combine records from two tables based on specified conditions. Examples of SQL queries for each function and join type are provided for clarity.

Uploaded by

jyotitahad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

Aggregate Functions

The document explains aggregate functions such as SUM, AVG, MAX, MIN, and COUNT, which can be used with or without the GROUP BY clause to analyze groups of records in a database. It also describes Cartesian products and various types of joins, including Natural Join and Equi Join, detailing how they combine records from two tables based on specified conditions. Examples of SQL queries for each function and join type are provided for clarity.

Uploaded by

jyotitahad
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Aggregate functions

SUM( ) AVG( ) MAX( ) MIN( ) COUNT( )

Aggregate or statistical functions can be used on a group of records.

Using GROUP BY clause: Display outputs regarding each group formed by the GROUP BY field.

Without using GROUP BY clause: Display output corresponding to the overall table may or may

not be filtered by where clause.

For example, consider the following ITEM table:

GROUP BY: GROUP BY clause is used if statistical records of a table are to be displayed based

on a field. Once the group is formed individual records cannot be accessed in that query. Several

clusters or groups are formed based on the number of different values in the GROUP BY column

present in the table.

For example, if GROUP BY is applied on TYPE field of ITEM table 3 groups are formed – Crops

have 2 records, Leaves and Pulses have one record each.

SELECT Type, COUNT(*)

FROM ITEM

GROUP BY Type;

Cartesian Product

Cartesian product is performed on two tables and it produces all the combination of records in both

tables. It does not require any common column.

If tables A, B have m, n columns and p, q records respectively then resultant table A x B has m+n

columns and p x q records.

Perform Cartesian Product between EMPL and DEPT.


SOLUTION 1 : SELECT *
FROM EMPL, DEPT;
[RECOMMENDED
STATEMENT]

SOLUTION 2: SELECT *
FROM EMPL INNER JOIN
DEPT;

SOLUTION 3 : SELECT *
FROM EMPL JOIN
DEPT;

JOIN
NATURAL JOIN: Natural join is a binary operator which works on two tables. They should have
one column which have same name and domain. It a combination of Cartesian product and a where
clause with equality checking on the common columns.
1)Other conditions in that query are ANDed with the join condition.
2) Natural join is mostly done on Foreign key field of one table and Primary key field of
another table.
3) If tables A, B have m, n columns and p, q records respectively then resultant table has m+n
columns and minimum(p,q) records.
EQUI JOIN: Equi join is a join operation which works on the equality condition of values in two
columns from two tables having similar data type. NATURAL JOIN, EQUI JOIN are said to be
INNER JOIN.
 Perform Natural Join between these two tables.
SOLUTION 1
SELECT *
FROM EMPL NATURAL JOIN
DEPT;
SOLUTION 2
SELECT *
FROM EMPL, DEPT
WHERE EMPL.DEPT_ID =
DEPT.DEPT_ID;
[RECOMMENDED STATEMENT]

You might also like