Aggregations
Consider the case of sports tournaments like cricket. Players’ performances are analysed based on their batting average, maximum number of sixes hit, the least score in a tournament, etc.
We perform aggregations in such scenarios to combine multiple values into a single value, i.e., individual scores to an average score.
Let’s learn more about aggregations to perform insightful analysis using the following database.
Database The database consists of a player_match_details table that stores the information of players' details in a match like name, match, score, year, number of fours and sixes scored.
Schema
Aggregation Functions
Combining multiple values into a single value is called aggregation. Following are the functions provided by SQL to perform aggregations on the given data:
Aggregate Functions | Description |
---|---|
COUNT | Counts the number of values |
SUM | Adds all the values |
MIN | Returns the minimum value |
MAX | Returns the maximum value |
AVG | Calculates the average of the values |
Syntax
Examples
- Get the total runs scored by "Ram" from theplayer_match_detailstable.
Output
SUM(score) |
---|
221 |
- Get the highest and least scores among all the matches that happened in the year 2011.
Output
MAX(score) | MIN(score) |
---|---|
75 | 62 |
COUNT Variants
- Calculate the total number of matches played in the tournament.
Variant 1
Variant 2
Variant 3
Output of Variant 1, Variant 2 and Variant 3
All the variants, i.e.,
Special Cases
- WhenSUMfunction is applied on non-numeric data types like strings, date, time, datetime etc.,SQLiteDBMS returns0.0andPostgreSQLDBMS returnsNone.
Aggregate functions on strings and their outputs
Aggregate Functions Output MIN, MAX Based on lexicographic ordering SUM, AVG 0 (depends on DBMS) COUNT Default behavior - NULLvalues are ignored while computing the aggregation values
When aggregate functions are applied on only
NULLvalues:Aggregate Functions Output MIN NULL MAX NULL SUM NULL COUNT 0 AVG NULL
Alias
Using the keyword
Syntax
Examples
- Get all the names of players with column name as "player_name".
Output
player_name |
---|
Ram |
Joseph |
--- |
- Get the average score of players as "avg_score".
Output
avg_score |
---|
60 |
Try it Yourself!
- Get the average score of "Ram" in the year 2011.
- Get the least score among all the matches.
- Get the highest score among the scores of all players in 2014.
- Get the total number of sixes hit as sixes_hit
name | match | score | fours | sixes | year |
---|---|---|---|---|---|
Ram | RR vs SRH | 62 | 2 | 7 | 2011 |
Joseph | SRH vs CSK | 44 | 2 | 4 | 2012 |
Lokesh | DC vs DD | 99 | 2 | 13 | 2013 |
David | SRH vs CSK | 96 | 1 | 13 | 2014 |
Joseph | SRH vs CSK | 2012 | |||
Viraj | RCB vs RR | 53 | 2 | 5 | 2010 |
Shyam | MI vs RCB | 75 | 2 | 9 | 2011 |
Stark | MI vs DC | 75 | 2 | 9 | 2012 |
Stark | MI vs SRH | 2012 | |||
Ram | RR vs MI | 84 | 1 | 11 | 2013 |
Joseph | SRH vs RR | 42 | 1 | 4 | 2014 |
David | SRH vs MI | 2014 | |||
Ramesh | CSK vs RR | 9 | 0 | 0 | 2010 |
Ram | RR vs DC | 75 | 2 | 9 | 2011 |
Joseph | SRH vs MI | 30 | 5 | 0 | 2012 |
Lokesh | DC vs RR | 87 | 2 | 11 | 2013 |
Ram | RR vs SRH | 2011 | |||
David | SRH vs MI | 9 | 0 | 0 | 2014 |