
Use functions like SUM, AVG, COUNT, MAX, and MIN to analyse data from a database of sales of mobile phones.
Aggregate functions perform calculations on a set of values and return a single result. Common aggregate functions include:
SUM(): Calculates the total of a numeric columnAVG(): Calculates the average valueCOUNT(): Counts the number of rowsMAX(): Finds the maximum valueMIN(): Finds the minimum valueLet's analyze mobile phone sales using aggregate functions:
SELECT SUM(price * quantity) AS total_revenue FROM salesSELECT AVG(price) AS average_price FROM salesSELECT COUNT(*) AS total_sales FROM salesSELECT model, MAX(price) AS max_price FROM salesSELECT model, MIN(price) AS min_price FROM sales
You can combine aggregate functions with GROUP BY to calculate values for different groups of data:
SELECT model, SUM(quantity) AS total_sold
FROM sales
GROUP BY model;This query calculates the total quantity sold for each phone model.
In this lesson, we've learned how to use aggregate functions to analyze and summarize data. These functions are powerful tools for extracting meaningful insights from large datasets.