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

SQL Statements With Aggregation and Filtering

Uploaded by

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

SQL Statements With Aggregation and Filtering

Uploaded by

Amanuel Kassa
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 13

Unit Three: SQL statements with aggregation

and filtering

• This unit is developed to provide you the necessary information


regarding the following content coverage and topics
• Function of group by statement
• Function of having clause
• Backup database
3.1 Function of group by
statement
• The GROUP BY statement groups rows that have the same values into
summary rows. It is often used with aggregate functions (COUNT (), MAX (),
MIN (), SUM (), AVG ()) to group the result-set by one or more columns.
• The GROUP BY Statement in SQL is used to arrange identical data into
groups with the help of some functions. i.e., if a particular column has the
same values in different rows, then it will arrange these rows in a group.
• Features
• GROUP BY clause is used with the SELECT statement.
• In the query, the GROUP BY clause is placed after the WHERE clause.
• In the query, the GROUP BY clause is placed before the ORDER BY clause if used.
• In the query, the Group BY clause is placed before the Having clause.
• Place condition in the having clause.
• GROUP BY Syntax
• SELECT column_name(s) FROM table_name WHERE condition GROUP BY
column_name(s) ORDER BY column_name(s);
• Example: let’s say we have a customers table with the following data

OrderID CustomerID EmployeeID OrderDate Country

10248 90 5 1996-07-04 Adama

10249 81 6 1996-07-05 Welkite

10250 34 4 1996-07-08 Adama


• If we want the number of customers in each country, sorted high to
low: we can write the following query
• SELECT COUNT (CustomerID), Country FROM Customers GROUP BY Country
ORDER BY COUNT (CustomerID) DESC;
• I. GROUP BY With JOIN
• Below there is a customers table with the following data
OrderI CustomerI EmployeeI OrderDate ShipperI
D D D D

10248 90 5 1996-07-04 3

10249 81 6 1996-07-05 1

10250 34 4 1996-07-08 2
• And here there is a shippers table with the following data
• ShipperI ShipperName

1 Speedy Express

2 United Package

3 Federal Shipping

• If we want to retrieve the number of orders sent by each shipper, we can


write the following SQL statement
•➢ SELECT Shippers.ShipperName, COUNT (Orders.OrderID) AS
NumberOfOrders from Orders LEFT JOIN Shippers ON Orders.ShipperID =
Shippers.ShipperID GROUP BY ShipperName;Sorting aggregated data
• II. GROUP BY With ORDER BY CLAUSE
• you can sort aggregate data using the ORDER BY clause in
combination with the aggregate functions.
• Sorting aggregate data in SQL Server involves using aggregate
functions to calculate summary values and then sorting the result set
based on those calculations.
• SELECT Shippers.ShipperName, COUNT (Orders.OrderID) AS
NumberOfOrders from Orders LEFT JOIN Shippers ON
Orders.ShipperID = Shippers.ShipperID GROUP BY ShipperName
ordered by COUNT (Orders.OrderID) DESC ;
3.2. Function of having clause

• In simpler terms MYSQL, the HAVING clause is used to apply a filter


on the result of GROUP BY based on the specified condition.
• The conditions are Boolean type i.e. use of logical operators (AND,
OR).
• This clause was included in SQL as the WHERE keyword failed when
we use it with aggregate expressions.
• Having is a very generally used clause in SQL. Similar to WHERE it
helps to apply conditions, but HAVING works with groups. If you wish
to filter a group, the HAVING clause comes into action.
• Some important points:
• Having clause is used to filter data according to the conditions
provided.
• Having a clause is generally used in reports of large data.
• Having clause is only used with the SELECT clause.
• The expression in the syntax can only have constants.
• In the query, ORDER BY is to be placed after the HAVING clause, if any.
• HAVING Clause is implemented in column operation.
• Having clause is generally used after GROUPBY.
• The main difference between where and having clause
• HAVING clause syntax
• SELECT col_1, function_name(col_2) FROM tablename WHERE
condition GROUP BY column1, column2 HAVING Condition ORDER BY
column1, column2
• Example: Below there is a customer table
Customer CustomerName ContactName Address City Postal Country
ID Code

1 Alfreds Futterkiste Maria Anders Obere Str. 57 Berlin 12209 Germany

2 Ana Trujillo Ana Trujillo Avda. de la México D.F. 05021 Mexico


Emparedados y Constitución
helados 2222

3 Antonio Moreno Antonio Moreno Mataderos 2312 México D.F. 05023 Mexico
Taquería

4 Around the Horn Thomas Hardy 120 Hanover London WA1 UK


Sq. 1DP

5 Berglunds Christina Berguvsvägen 8 Luleå S-958 Sweden


snabbköp Berglund 22
• If we want to lists the number of customers in each country. And only
include countries with more than 5 customers: the SQL statement will be
•➢ SELECT COUNT (CustomerID), Country FROM Customers GROUP
BY Country HAVING COUNT (CustomerID) > 5;
• If we want to lists the number of customers in each country, sorted high
to low (Only include countries with more than 5 customers): the SQL
statement will be
•➢ SELECT COUNT (CustomerID), Country FROM Customers GROUP
BY Country HAVING COUNT (CustomerID) > 5 ORDER BY COUNT
(CustomerID) DESC;
3.4. Backup database

• The BACKUP DATABASE statement is used in SQL Server to create a full back up
of an existing SQL database.
• Syntax
• BACKUP DATABASE databasename TO DISK = 'filepath';
•• Backup with differential
• A differential backup reduces the backup time (since only the changes are
backed up).
• The following SQL statement creates a differential back up of the database
"testDB":
• Example: BACKUP DATABASE testDB TO DISK = 'D:\backups\testDB.bak' WITH
DIFFERENTIAL;

You might also like