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

SQL

The document explains the SQL GROUP BY statement, which groups rows with the same values into summary rows, often used with aggregate functions like COUNT, MAX, and SUM. It also covers the HAVING clause, which filters groups created by GROUP BY based on aggregate functions, and provides syntax and examples for using aggregate functions such as MIN, MAX, COUNT, AVG, and SUM. Additionally, it highlights the importance of these functions in summarizing data while ignoring null values.

Uploaded by

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

SQL

The document explains the SQL GROUP BY statement, which groups rows with the same values into summary rows, often used with aggregate functions like COUNT, MAX, and SUM. It also covers the HAVING clause, which filters groups created by GROUP BY based on aggregate functions, and provides syntax and examples for using aggregate functions such as MIN, MAX, COUNT, AVG, and SUM. Additionally, it highlights the importance of these functions in summarizing data while ignoring null values.

Uploaded by

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

SQL

Structured Query
Language
next slide
GROUP BY ()
The GROUP BY statement groups rows that have the same
values into summary rows, like "find the number of customers in
each country.
The GROUP BY statement is often used with aggregate functions
(COUNT(), MAX(), MIN(), SUM(), AVG()) to group the result-set by
one or more columns.

next slide
GROUP BY Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);

Note: Here, column_names must include the columns on the basis of which
grouping is to be done
next slide
EXAMPLE:
SELECT company, COUNT(product)
FROM product_mast
GROUP BY company;

OUTPUT:
com1 5
com2 3 next slide
com3 2
EXAMPLE:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;

OUTPUT:
The following SQL statement lists the number of customers in each country,
sorted high to low
next slide
HAVING Clause
The HAVING clause was added to SQL because the WHERE
keyword cannot be used with aggregate functions.
HAVING is used to filter groups created by GROUP BY, bassed
on aggregate functions (like count(),sum(), etc) but technically,
we can use HAVING without GROUP BY- it just treats the whole
result as one group.

next slide
HAVING Syntax:
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
HAVING condition
ORDER BY column_name(s);

next slide
EXAMPLE:
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
HAVING COUNT(CustomerID) > 5;

OUTPUT:
The following SQL statement lists the number of customers in each country.
Only include countries with more than 5 customers.
next slide
Aggregate functions
Aggregate functions are also called multiple row functions.
Performs calculation on set of values and returns a single value.
used in SQL to summerizing data
ignores null value (except for COUNT()
these fuctions are also used with GROUP BY clause of SELECT statements.
GROUP BY split the result-set into group of values and aggregate function can
be used to return a single value for each group.

next slide
Most commonly used sql aggregate
function are:
MIN() : returns the smallest value within the selected column
MAX() : returns the largest value within the selected values
COUNT() : Returns the no. of rows in a set.
SUM() : returns the total sum of a nummerical column
MIN() : returns the avg. value within the selected column
count(*) : counts all rows in a table, including row with null
value
MIN() and MAX() Functions:
The MIN() function returns the smallest value of the selected column.
The MAX() function returns the largest value of the selected column.

SYNTAX:
SELECT MAX(column_name)
SELECT MIN(column_name) FROM table_name
FROM table_name WHERE condition;
WHERE condition;

next slide
COUNT() Functions:
The COUNT() function returns the number of rows that matches a specified
criterion.

SYNTAX: EXAMPLE:
SELECT COUNT(column_name) SELECT COUNT(ProductID)
FROM table_name FROM Products;
WHERE condition;

next slide
AVG() Functions:
The AVG() function returns the average value of a numeric column.

SYNTAX: EXAMPLE:
SELECT AVG(column_name) SELECT AVG(Price)
FROM table_name FROM Products;
WHERE condition;

next slide
SUM() Functions:
The SUM() function returns the total sum of a numeric column.

SYNTAX: EXAMPLE:
SELECT SUM(column_name) SELECT SUM(Quantity)
FROM table_name FROM OrderDetails;
WHERE condition;

Note: NULL values are ignored. next slide

You might also like