Domesticated Brain

SQL Functions

 

In this lesson we are working with following product table 

It has following fields (ProductID, Category, BrandName, ProductName, Price) 

 

SQL Aggregate functions

SQL Aggregate functions gives a single value after processing data in a column. Please Note that aggregate functions ignore null values

These are the basic aggregate functions and basic syntax in SQL

SQL Tutorials Aggregate Functions 

 

 SQL Tutorials Aggregate Functions Product Table

 As above shown the view of the tblproducts table in phpMyAdmin

 

SQL AVG() Function

As an example using following SQl query we can get the average value of the prices in Price column of the tblproducts table

SELECT AVG(Price) FROM tblproduct

SQL Tutorials AVG Function

 Using following query we are going to select the products that exceeding average price

SELECT BrandName,ProductName,Price FROM tblproducts
WHERE Price>(SELECT AVG(Price) FROM tblproducts); 

SQL Tutorials AVG Function Result

 

 

SQL COUNT() Function

Count function can be used in different ways

Following SQL query will give the number of rows in tblproducts table

SELECT COUNT(ProductName) FROM tblproducts;

 

SQL Tutorials COUNT Function

 

 

SQL MAX() Function

Max function will show the highest value in a column with numerical data

The max price 85000 will be returned after executing following query

SELECT MAX(Price) FROM tblproducts;

 

SQL Tutorials MAX Function

 

 

SQL MIN () Function

Min function is the opposite of max function

It will returns the minimum value in a column with numerical data

SELECT MIN(Price) FROM tblproducts;

 

SQL Tutorials MIN Function

 

 

SQL SUM() Function

The sum function sum up the values in a column with numerical data

The sum of the prices will be returned after executing following query

SELECT SUM(Price) FROM tblproducts;

 

SQL Tutorials SUM Function

 If you have found  what you expected please comment and like on our facebook page