Aggregations Cheat Sheet in Aggregations and Group By SQL

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

player_match_details (
name VARCHAR(250),
match VARCHAR(10),
score INTEGER,
fours INTEGER,
sixes INTEGER,
year INTEGER
);
SQL

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 FunctionsDescription
COUNTCounts the number of values
SUMAdds all the values
MINReturns the minimum value
MAXReturns the maximum value
AVGCalculates the average of the values

Syntax

SELECT
aggregate_function(c1),
aggregate_function(c2)
FROM
TABLE;
SQL
Note
We can calculate multiple aggregate functions in a single query.

Examples

  1. Get the total runs scored by "Ram" from the
    player_match_details
    table.
SELECT
SUM(score)
FROM
player_match_details
WHERE
name = "Ram";
SQL
Output
SUM(score)
221
  1. Get the highest and least scores among all the matches that happened in the year 2011.
SELECT
MAX(score),
MIN(score)
FROM
player_match_details
WHERE
year = 2011;
SQL
Output
MAX(score)MIN(score)
7562

COUNT Variants

  • Calculate the total number of matches played in the tournament.
Variant 1
SELECT COUNT(*)
FROM player_match_details;
SQL
Variant 2
SELECT COUNT(1)
FROM player_match_details;
SQL
Variant 3
SELECT COUNT()
FROM player_match_details;
SQL
Output of Variant 1, Variant 2 and Variant 3

All the variants, i.e.,

Variant 1
,
Variant 2
and
Variant 3
give the same result:
18

Special Cases

  • When
    SUM
    function is applied on non-numeric data types like strings, date, time, datetime etc.,
    SQLite
    DBMS returns 
    0.0
    and
    PostgreSQL
    DBMS returns
    None
    .
  • Aggregate functions on strings and their outputs

    Aggregate FunctionsOutput
    MIN, MAXBased on lexicographic ordering
    SUM, AVG0 (depends on DBMS)
    COUNTDefault behavior
  • NULL
    values are ignored while computing the aggregation values
  • When aggregate functions are applied on only

    NULL
    values:

    Aggregate FunctionsOutput
    MINNULL
    MAXNULL
    SUMNULL
    COUNT0
    AVGNULL

Alias

Using the keyword

AS
, we can provide alternate temporary names to the columns in the output.

Syntax

SELECT
c1 AS a1,
c2 AS a2,
...
FROM
table_name;
SQL

Examples

  • Get all the names of players with column name as "player_name".
SELECT
name AS player_name
FROM
player_match_details;
SQL
Output
player_name
Ram
Joseph
---
  • Get the average score of players as "avg_score".
SELECT
AVG(score) AS avg_score
FROM
player_match_details;
SQL
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

namematchscorefourssixesyear
RamRR vs SRH62272011
JosephSRH vs CSK44242012
LokeshDC vs DD992132013
DavidSRH vs CSK961132014
JosephSRH vs CSK2012
VirajRCB vs RR53252010
ShyamMI vs RCB75292011
StarkMI vs DC75292012
StarkMI vs SRH2012
RamRR vs MI841112013
JosephSRH vs RR42142014
DavidSRH vs MI2014
RameshCSK vs RR9002010
RamRR vs DC75292011
JosephSRH vs MI30502012
LokeshDC vs RR872112013
RamRR vs SRH2011
DavidSRH vs MI9002014

Post a Comment

Please Select Embedded Mode To Show The Comment System.*

Previous Post Next Post

Contact Form