Aggregate Functions - SQL Query

Aggregate functions

SQL aggregate functions returns single value, calculated from values present in specific column.

Lets understand the following aggregate functions with an example.

Consider the following table titled 'Items'.

IDNameQuantityPrice
1Laptop270000
2Desktop360000
3Speakers515000
4UPS1020000

i) Display the sum of 'Price' columns from table items.

SELECT SUM(Price) AS Total Price FROM Items

In the above table SUM() function performs a summation of column 'Price' and Alice (AS) is used to display Total Price.

Output:

Total Price
165000

ii) Display the average of 'Price' column from table 'Items'

SELECT AVG(Price) AS Average Price FROM Items

In the above statement AVG() function calculates average of column 'Price' and Alice (AS) is used to display Average Price.

Output:

Average Price
41250

Iii) Display the total records present in the table Items.

SELECT COUNT(*) FROM table Items

The COUNT(*) function returns the total number of records present in the table Items.

Output:

COUNT(*)
4

iv) Display the total records present in column 'ID'  from table Items.

SELECT COUNT(ID) AS Item_ID FROM Items

In the above statement the function COUNT() calculates the total 'ID' as Item_ID.

Output:

Item_ID
4
  
v) Display the highest value in column 'Price' from table 'Items'.

SELECT MAX(Price) AS Highest_Price FROM Items

The MAX(Price) function returns the highest value present in the column and Alice(AS) is used to display the Highest_Price present in the column.

Output:

Highest_Price
70000

vi) Display the smallest value present in the column 'Price' from table 'Items'.

SELECT MIN(Price) AS Smallest_Price FROM Table Items

The MIN(Price) function returns the smallest value present the column 'Price'.

Smallest_Price
15000