Download Grouping - SoftUni

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Data Aggregation
How to get data insights?
SoftUni Team
Technical Trainers
Software University
http://softuni.bg
Table of Content
1. Grouping
2. Aggregate Functions
3. Having Clause
2
Questions
sli.do
#SQL
3
Grouping
Consolidating data based on criteria
Grouping (1)
 Grouping allows taking data into separate groups based on a
common property.
Employee DepartmentName
Salary
Adam
Database Support
5,000
John
Database Support
15,000
Jane
Application Support
10,000
George
Application Support
15,000
Lila
Application Support
5,000
Fred
Software Support
15,000
5
Grouping (1)
Grouping column
Single row
Employee DepartmentName
Salary
Adam
Database Support
5,000
John
Database Support
15,000
Jane
Application Support
10,000
George
Application Support
15,000
Lila
Application Support
5,000
Fred
Software Support
15,000
Can be
aggregated
6
Grouping (2)
 With GROUP BY you can get each separate group and use an
"aggregate" function over it (like Average, Min or Max):
SELECT e.DepartmentID
FROM Employees AS e
GROUP BY e.DepartmentID
Grouping
Columns
 With DISTINCT you will get all unique values:
SELECT DISTINCT e.DepartmentID
FROM Employees AS e
Unique
Values
7
Problem: Departments Total Salaries
 Use "SoftUni" database to create a query which prints the total
sum of salaries for each department.
 Order them by DepartmentID (ascending).
Employee DepartmentID
Adam
1
Salary
5,000
John
Jane
George
1
2
2
15,000
10,000
15,000
Lila
2
5,000
DepartmentID
1
2
3
TotalSalary
20,000
30,000
15,000
Fred
3
15,000
Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/291#12
8
Solution: Departments Total Salaries
 After grouping every employee by it's department we can use
aggregate function to calculate total amount of money per group.
Grouping
Column
New Column Alias
SELECT e.DepartmentID,
SUM(e.Salary) AS 'TotalSalary'
FROM Employees AS e
Table Alias
GROUP BY e.DepartmentID
Grouping
ORDER BY e.DepartmentID
Columns
Check your solution here: https://judge.softuni.bg/Contests/Practice/Index/291#12
9
Aggregate Functions
COUNT, SUM, MAX, MIN, AVG…
Aggregate Functions
 Aggregate functions are used to operate over one or more
groups (with some elements in them), performing data analysis
on every one.
 MIN, MAX, AVG, COUNT etc.
SELECT e. DepartmentID,
MIN(e.Salary) AS 'MinSalary'
FROM Employees AS e
GROUP BY e.DepartmentID
 They usually ignore NULL values.
COUNT
 COUNT - counts the values (not nulls) in one or more columns based
on grouping criteria.
Employee DepartmentName
Adam
Database Support
John
Database Support
Salary
5,000
15,000
Jane
George
Lila
Application Support
Application Support
Application Support
10,000
15,000
5,000
Fred
Software Support
15,000
DepartmentName
Database Support
Application Support
Software Support
SalaryCount
2
3
1
12
COUNT Syntax
 Note that we when we use COUNT we will ignore any employee with
NULL salary.
Grouping
Column
New Column Alias
SELECT e.DepartmentID,
COUNT(e.Salary) AS 'SalaryCount'
FROM Employees AS e
GROUP BY e.DepartmentID
Grouping
Columns
13
SUM
 SUM - sums the values in a column.
Employee DepartmentName
Adam
Database Support
John
Database Support
Salary
5,000
15,000
Jane
George
Lila
Fred
10,000
15,000
5,000
15,000
Application Support
Application Support
Application Support
Software Support
DepartmentName
Database Support
TotalSalary
20,000
Application Support 30,000
Software Support
15,000
14
SUM Syntax
 If any department has no salaries NULL will be displayed.
Grouping
Column
New Column Alias
SELECT e.DepartmentID,
SUM(e.Salary) AS 'TotalSalary'
Table Alias
FROM Employees AS e
GROUP BY e.DepartmentID
Grouping
Columns
15
MAX
 MAX - takes the maximum value in a column.
Employee DepartmentName
Adam
Database Support
John
Database Support
Salary
5,000
15,000
Jane
George
Lila
Fred
10,000
15,000
5,000
15,000
Application Support
Application Support
Application Support
Software Support
DepartmentName
Database Support
MaxSalary
15,000
Application Support 15,000
Software Support
15,000
16
MAX Syntax
Grouping
Column
New Column Alias
SELECT e.DepartmentID,
MAX(e.Salary) AS 'MaxSalary'
FROM Employees AS e
Table Alias
GROUP BY e.DepartmentID
Grouping
Columns
17
MIN
 MIN takes the minimum value in a column.
Employee DepartmentName
Adam
Database Support
John
Database Support
Salary
5,000
15,000
Jane
George
Lila
Fred
10,000
15,000
5,000
15,000
Application Support
Application Support
Application Support
Software Support
DepartmentName
Database Support
MinSalary
5,000
Application Support 5,000
Software Support
15,000
18
MIN Syntax
Grouping
Column
New Column Alias
SELECT e.DepartmentID,
MIN(e.Salary) AS 'MinSalary'
FROM Employees AS e
Table Alias
GROUP BY e.DepartmentID
Grouping
Columns
19
AVG
 AVG calculates the average value in a column.
Employee DepartmentName
Adam
Database Support
John
Database Support
Salary
5,000
15,000
Jane
George
Lila
Fred
10,000
15,000
5,000
15,000
Application Support
Application Support
Application Support
Software Support
DepartmentName
Database Support
AvgSalary
10,000
Application Support 10,000
Software Support
15,000
20
Demo: AVG Syntax
Grouping
Column
New Column Alias
SELECT e.DepartmentID,
AVG(e.Salary) AS 'AvgSalary'
FROM Employees AS e
Table Alias
GROUP BY e.DepartmentID
Grouping
Columns
21
Having
Using predicates while grouping
Having Clause
 The HAVING clause is used to filter data based on aggregate values.

This means that we cannot use it without grouping before that.
 Any Aggregate functions (like MIN, MAX, SUM etc.) used both in
the "HAVING" clause and in the select statement are executed one
time only.
 Unlike HAVING, the WHERE clause filters rows before the
aggregation happens.
23
Having Clause: Example
 Filter departments which have total salary more or equal 15,000.
Aggregated value
Employee DepartmentName
Adam
Database Support
Salary
5,000
TotalSalary
John
Jane
George
Lila
Database Support
Application Support
Application Support
Application Support
15,000
10,000
15,000 10,000
5,000
Fred
Software Support
15,000 15,000
20,000
DepartmentName TotalSalary
Database Support 20,000
Software Support 15,000
24
HAVING Syntax
Grouping
Column
Aggregate
Function
SELECT e.DepartmentID,
New
SUM(e.Salary) AS 'TotalSalary'
Column Alias
FROM Employees AS e
GROUP BY e.DepartmentID
Grouping
HAVING SUM(e.Salary) < 250000
Columns
Having
Predicate
25
Summary
1. Grouping
2. Aggregate Functions
3. Having
SELECT
SUM(e.Salary) AS 'TotalSalary'
FROM Employees AS e
GROUP BY e.DepartmentID
HAVING SUM(e.Salary) < 250000
27
Data Aggregation
?
https://softuni.bg/courses/
License
 This course (slides, examples, demos, videos, homework, etc.)
is licensed under the "Creative Commons AttributionNonCommercial-ShareAlike 4.0 International" license
 Attribution: this work may contain portions from

"Databases" course by Telerik Academy under CC-BY-NC-SA license
29
Free Trainings @ Software University
 Software University Foundation – softuni.org
 Software University – High-Quality Education,
Profession and Job for Software Developers

softuni.bg
 Software University @ Facebook

facebook.com/SoftwareUniversity
 Software University @ YouTube

youtube.com/SoftwareUniversity
 Software University Forums – forum.softuni.bg
Related documents