0% found this document useful (0 votes)
8 views5 pages

Experiment No 5

Uploaded by

Anisha Cotta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views5 pages

Experiment No 5

Uploaded by

Anisha Cotta
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Experiment No :5 Date: / / 2023

Title: Grouping Data

Aim: To study the use of Group By and Having Clause

Theory:

The GROUP BY clause groups a set of rows into a set of summary rows by values of columns or
expressions. The GROUP BY clause returns one row for each group. In other words, it reduces the
number of rows in the result set.
You often use the GROUP BY clause with aggregate functions such as SUM, AVG, MAX, MIN, and COUNT.
The aggregate function that appears in the SELECT clause provides information about each group.
The GROUP BY clause is an optional clause of the SELECT statement. The following illustrates the GROUP
BY clause syntax:

The GROUP BY clause must appear after the FROM and WHERE clauses. Following the GROUP
BY keywords is a list of comma-separated columns or expressions that you want to use as criteria to
group rows.
MySQL evaluates the GROUP BY clause after the FROM, WHERE and SELECT clauses and before
the HAVING , ORDER BY and LIMIT clauses

54
MySQL GROUP BY examples
Let’s take some example of using the GROUP BY clause.
A) Simple MySQL GROUP BY example

Suppose you want to group values of the order’s status into


subgroups, you use the GROUP BY clause with the status column as the following query:

The GROUP BY clause returns unique occurrences of status values. It works like the DISTINCT operator as
shown in the following query

B) Using MySQL GROUP BY with aggregate functions


The aggregate functions allow you to perform the calculation of a set of rows and return a single value.
The GROUP BY clause is often used with an aggregate function to perform calculation and return a single
value for each subgroup.
For example, if you want to know the number of orders in each status, you can use the COUNT function
with the GROUP BY clause as follows

55
See the following orders and orderdetails table

To get the total amount of all orders by status, you join the orders table with the orderdetails table and
use the SUM function to calculate the total amount. See the following query:

Similarly, the following query returns the order numbers and the total amount of each order

56
C) MySQL GROUP BY with expression example
In addition to columns, you can group rows by expressions. The following query gets the total sales for
each year

In this example, we used the YEAR function to extract year data from order date ( orderDate).
We included only orders with shipped status in the total sales. Note that the expression which appears
in the SELECT clause must be the same as the one in the GROUP BY clause.
D) Using MySQL GROUP BY with HAVING clause example
To filter the groups returned by GROUP BY clause, you use a HAVING clause. The following query uses
the HAVING clause to select the total sales of the years after 2003.

57
MySQL also allows you to sort the groups in ascending or descending orders while the standard SQL
does not. The default order is ascending. For example, if you want to get the number of orders by status
and sort the status in descending order, you can use the GROUP BY clause with DESC as the following
query:

Conclusion: Studied the use of Group By and Having clause.

58

You might also like