Wednesday, August 12, 2015

Aggregate Functions

SQL AVG Function

In SQL Server, AVG Function is used to calculate the Average of total records (or rows) selected by the  SELECT Statement. For example, If you want to find the Average price of a products present in your Store or you want to find the Average Cost of a Black color products present in your Store, you can simply use the Avg function.
TIP: AVG Function will only work on Numeric Columns.
SQL Avg Function Syntax
In SQL Server, The basic syntax of the SQL AVG() Function is:
In this article we will show you, How to write Avg Function in SQL Server 2014 with example. For this, We are going to use the below shown data
SQL AVG FUNCTION

SQL AVG() Example

Avg () simply returns the Average of a total number of records present in the specified column. For example, The following query will calculate the Average of total records present in the [Yearly Income] column from Customers table.
T-SQL CODE
OUTPUT
SQL AVG FUNCTION 1

SQL AVG Function with Group By Clause

In most cases we usually check for Average product price that belongs to particular category or color etc. In these situations we use GROUP BY Clause to group the products by color or category and then use the Avg Function to calculate the average of a products present in each group. Let us see the Example
T-SQL CODE
Above SQL Query will find, the Customers associated with the particular Department and finds the average income of each department
OUTPUT
SQL AVG FUNCTION 2

SQL AVG Function with Distinct

AVG (DISTINCT Column Name) simply calculate the average of the Unique number of records present in the table whose values are NOT NULL (Ignores the NULL Records).
TIP: In SQL, DISTINCT Keyword is used to remove the Duplicates from the specified column Name.
T-SQL CODE
Above SQL Query will find the Unique number of records (by removing duplicates) present in the [Yearly Income] column whose values are NOT NULL and then calculates the average of them
OUTPUT
SQL AVG FUNCTION 3

SQL Avg Function in SubQuery

When we are retrieving the data, In some cases we usually check for the conditions against the aggregated data. In these situations we have to use Sub queries in Where Clause. For example, The following query will return all the Customers present in the customers table whose [Yearly Income] is greater than the average yearly income.
T-SQL CODE
From the first example you can observe that, Average Yearly Income is 67000. So above query will display all the customers from the Customers table whose yearly income is greater than 67000
OUTPUT
SQL AVG FUNCTION 4
Thank You for Visiting Our Blog


SQL COUNT Function

In SQL Server, COUNT Function is used to Count the number of records (or rows) selected by the  SELECT Statement. For example, If you want to find the total Number of products present in your Store or you want to find the Black color products present in your Store. In these situations you can simply use the COUNT function.
In this article we will show you, How to write COUNT Function in SQL Server 2014 with example. For this, We are going to use the below shown data
SQL COUNT Function

SQL COUNT (*) Example

COUNT(*) simply returns the total number of records present in the table. Let us see one example to understand it better
T-SQL CODE
Above SQL Query will count the total number of records present in the Employee table
OUTPUT
SQL COUNT Function 1
NOTE: The COUNT(*) counts NULL values and Duplicate records as well.

SQL COUNT (Column Name) Example

COUNT(Column Name) simply returns the total number of records present in the table whose values are NOT NULL (Ignores the NULL Records). Let us see one example
NOTE: The COUNT(Column Name) counts the Duplicate records.
T-SQL CODE
OUTPUT
SQL COUNT Function 2
We got the same result as COUNT(*) Query because there are no NULL Values in the [FirstName] Column. Let us use another Column
T-SQL CODE
Above SQL Query will count the total number of records present in the [DepartID] whose values are NOT NULL
OUTPUT
SQL COUNT Function 3

SQL COUNT DISTINCT Example

COUNT(DISTINCT Column Name) simply returns the Unique number of records present in the table whose values are NOT NULL (Ignores the NULL Records).
TIP: In SQL, DISTINCT Keyword is used to remove the Duplicates from the specified column Name.
T-SQL CODE
Above SQL Query will count the Unique number of records (by removing duplicates) present in the [DepartID] whose values are NOT NULL
OUTPUT
SQL COUNT DISTINCT Function 1

SQL COUNT along with GROUP BY Clause

In most cases we usually check for number of products belongs to particular category or color etc. In these situations we use GROUP BY clause to group the products by color or category and then use the COUNT Function to count the number of products present in that group. Let us see the Example
T-SQL CODE
Above SQL Query will find, How many Employees are associated with the particular Department ID.
OUTPUT
SQL COUNT Function 4
From the above screenshot you can observe that, SQL Query is grouping the NULL records and count them as well. Let us use the WHERE Clause with NOT NULL and check again the result
OUTPUT
SQL COUNT Function 5
TIP: Use NOT NULL to avoid the NULL records in the query
Thank You for Visiting Our Blog

SQL Max Function

In SQL Server, MAX Function is used to find the Maximum value from the total records (or rows) selected by the SELECT Statement.
TIP: MAX Function will ignore Null values.
SQL Max Function Syntax
In SQL Server, The basic syntax of the SQL Max() Function is:
In this article we will show you, How to write Max Function in SQL Server 2014 with example. For this, We are going to use the below shown data
SQL MAX FUNCTION

SQL MAX() Example

Max() simply returns the Maximum value from the total records present in the specified column. For example, The following query will find the Maximum yearly income present in the [Yearly Income] column from Customers table.
T-SQL CODE
OUTPUT
SQL MAX FUNCTION 1

SQL MAX Function with Group By Clause

In most cases we usually check maximum product price (Highest pricing product) belongs to particular category or color etc. In these situations we use GROUP BY Clause to group the products by color or category and then use the Max Function to find the Highest pricing product present in each group. Let us see the Example
T-SQL CODE
Above SQL Query will find, the Customers associated with the particular Department and finds the maximum income of a customer present in each department
OUTPUT
SQL MAX FUNCTION 2

SQL Max Function with Having Clause

When we are grouping the data, In some cases we usually check for the conditions against the aggregated data. In these situations we use HAVING Clause along with Group By Statement. For example, The following query will group the Customers by their Occupation and then finds the Maximum yearly Income present in each group.
T-SQL CODE
Below lines of code will check whether the aggregated amount (Maximum Yearly income of each individual Group) is greater than 50000 or not. If this is True then corresponding records will be displayed.
OUTPUT
SQL MAX FUNCTION 3

SQL Max Function in Sub Query

When we are retrieving the data, In some cases we usually check for the conditions against the aggregated data. In these situations we have to use Sub queries in Where Clause. For example, The following query will return all the Customers present in the customers table whose [Yearly Income] is exactly equal to the Maximum yearly income.
T-SQL CODE
From the first example you can observe that, Maximum Yearly Income is 90000. So above query will display all the customers from the Customers table whose yearly income is equal to 90000
OUTPUT
SQL MAX FUNCTION 4
Thank You for Visiting Our Blog

SQL Min Function

In SQL Server, MIN Function is used to find the Minimum value from the total records (or rows) selected by the SELECT Statement.
TIP: MIN Function will ignore Null values.
SQL Min Function Syntax
In SQL Server, The basic syntax of the SQL MIN() Function is:
In this article we will show you, How to write Min Function in SQL Server 2014 with example. For this, We are going to use the below shown data
SQL MIN FUNCTION

SQL MIN() Example

Min() simply returns the Minimum value from the total records present in the specified column. For example, The following query will find the Minimum yearly income present in the [Yearly Income] column from Customers table.
T-SQL CODE
OUTPUT
SQL MIN FUNCTION 1

SQL MIN Function with Group By Clause

In most cases we usually check minimum product price (Lowest pricing product) belongs to particular category or color etc. In these situations we use GROUP BY Clause to group the products by color or category and then use the Min Function to find the Lowest pricing product present in each group. Let us see the Example
T-SQL CODE
Above SQL Query will find, the Customers associated with the particular Department and finds the minimum income of a customer present in each department
OUTPUT
SQL MIN FUNCTION 2

SQL Min Function with Having Clause

When we are grouping the data, In some cases we usually check for the conditions against the aggregated data. In these situations we use HAVING Clause along with Group By Statement. For example, The following query will group the Customers by their Occupation and then finds the Minimum yearly Income present in each group.
T-SQL CODE
Below lines of code will check whether the aggregated amount (Minimum Yearly income of each individual Group) is greater than 50000 or not. If this is True then corresponding records will be displayed.
OUTPUT
SQL MIN FUNCTION 3

SQL Min Function in Sub Query

When we are retrieving the data, In some cases we usually check for the conditions against the aggregated data. In these situations we have to use Sub queries in Where Clause. For example, The following query will return all the Customers present in the customers table whose [Yearly Income] is greater than the Minimum yearly income.
T-SQL CODE
From the first example you can observe that, Minimum Yearly Income is 50000. So above query will display all the customers from the Customers table whose yearly income is greater than 50000
OUTPUT
SQL MIN FUNCTION 4
Thank You for Visiting Our Blog

SQL STDEV Function

In SQL Server, STDEV Function is used to calculate the standard Deviation of total records (or rows) selected by the SELECT Statement.
TIP: STDEV Function will only work on Numeric Columns and it ignores Nulls.
SQL STDEV Function Formula
SQL STDEV Function Syntax
In SQL Server, The basic syntax of the SQL STDEV() Function is:
In this article we will show you, How to write STDEV Function in SQL Server 2014 to calculate the Standard Deviation with example. For this, We are going to use the below shown data
SQL STDEV FUNCTION

SQL STDEV() Example

STDEV () simply returns the Standard Deviation of total number of records present in the specified column. For example, The following query will calculate the Standard Deviation of total records present in the [Yearly Income] column from Customers table.
T-SQL CODE
OUTPUT
SQL STDEV FUNCTION 1

SQL STDEV Function with Group By Clause

In most cases we usually calculate the Standard Deviation of a products belongs to particular category or color etc. In these situations we use GROUP BY Clause to group the products by color or category and then use the STDEV Function to calculate the Standard Deviation of products present in each group. Let us see the Example
T-SQL CODE
Above SQL Query will find, the Customers associated with the particular Department and calculates their Standard Deviation
OUTPUT
SQL STDEV FUNCTION 2
Analysis
It is too big to show the calculations for every thing so, We are taking Skilled Manual profession and show you the output.

SQL STDEV Function in Having Clause

When we are grouping the data, In some cases we usually check for the conditions against the aggregated data. In these situations we use HAVING Clause along with Group By Statement. For example, The following query will group the Customers by their Occupation and then calculates the Standard Deviation of each individual group.
T-SQL CODE
Below lines of code will check whether the aggregated amount (Standard Deviation of Yearly income for each individual Group) is greater than 0 or not. If this is True then corresponding records will be displayed.
OUTPUT
SQL STDEV FUNCTION 3

SQL STDEV Function in Sub Query

When we are retrieving the data, In some cases we usually check for the conditions against the aggregated data. In these situations we have to use Sub queries in Where Clause. For example, The following query will return all the Customers present in the customers table whose [Yearly Income] is greater than five times yearly incomes Standard Deviation.
T-SQL CODE
From the first example you can observe that, Standard Deviation of a Yearly Income is 12516. So above query will display all the customers from the Customers table whose yearly income is greater than 62580 approximately
OUTPUT
SQL STDEV Function 4
Thank You for Visiting Our Blog

SQL STDEVP Function

In SQL Server, STDEVP Function is used to calculate the standard Deviation for population of total records (or rows) selected by the SELECT Statement.
TIP: STDEVP Function will only work on Numeric Columns and it ignores Nulls.
SQL STDEVP Function Formula
SQL STDEVP Function Syntax
In SQL Server, The basic syntax of the STDEVP() Function is:
In this article we will show you, How to write STDEVP Function in SQL Server 2014 to calculate the Standard Deviation for population with example. For this, We are going to use the below shown data
SQL STDEVP FUNCTION

SQL STDEVP() Example

STDEVP () simply returns the Standard Deviation for population of total number of records present in the specified column. For example, The following query will calculate the Standard Deviation for population of total records present in the [Yearly Income] column from Customers table.
T-SQL CODE
OUTPUT
SQL STDEVP FUNCTION 1

SQL STDEVP Function with Group By Clause

In most cases we usually calculate the Standard Deviation for population of a products belongs to particular category or color etc. In these situations we use GROUP BY Clause to group the products by color or category and then use the STDEV Function to calculate the Standard Deviation of products present in each group. Let us see the Example
T-SQL CODE
Above SQL Query will find, the Customers associated with the particular Department and calculates their Standard Deviation for population.
OUTPUT
SQL STDEVP FUNCTION
Analysis
It is too big to show the calculations for every thing so, We are taking Skilled Manual profession and show you the output.

SQL STDEVP Function in Having Clause

When we are grouping the data, In some cases we usually check for the conditions against the aggregated data. In these situations we use HAVING Clause along with Group By Statement. For example, The following query will group the Customers by their Occupation and then calculates the Standard Deviation for population of each individual group.
T-SQL CODE
Below lines of code will check whether the aggregated amount (Standard Deviation of Yearly income for each individual Group) is greater than 0 or not. If this is True then corresponding records will be displayed.
OUTPUT
SQL STDEVP FUNCTION 3

SQL STDEVP Function in Sub Query

When we are retrieving the data, In some cases we usually check for the conditions against the aggregated data. In these situations we have to use Sub queries in Where Clause. For example, The following query will return all the Customers present in the customers table whose [Yearly Income] is greater than five times yearly incomes Standard Deviation for population.
T-SQL CODE
From the first example you can observe that, Standard Deviation of a Yearly Income is 11874. So above query will display all the customers from the Customers table whose yearly income is greater than 71244 approximately
OUTPUT
SQL STDEVP FUNCTION 4
Thank You for Visiting Our Blog


SQL SUM Function

In SQL Server, SUM Function is used to calculate the total or Sum of records (or rows) selected by the  SELECT Statement. For example, If you want to find the total price of a products present in your Store or you want to find the total price of a Black color products present in your Store you can simply use the Sum function.
TIP: SUM Function will only work on Numeric Columns.
SQL Sum Function Syntax
In SQL Server, The basic syntax of the SQL Sum() Function is:
In this article we will show you, How to write Sum Function in SQL Server 2014 with example. For this, We are going to use the below shown data
SQL SUM FUNCTION

SQL SUM() Example

Sum () simply returns the sum of total number of records present in the specified column. For example, The following query will calculate the sum of total records present in the [Yearly Income] column from Customers table.
T-SQL CODE
OUTPUT
SQL SUM FUNCTION 1

SQL SUM Function with Group By Clause

In most cases we usually Total the products belongs to particular category or color etc. In these situations we use GROUP BY Clause to group the products by color or category and then use the Sum Function to Sum the products present in each group. Let us see the Example
T-SQL CODE
Above SQL Query will find, the Customers associated with the particular Department and total their income
OUTPUT
SQL SUM FUNCTION 2

SQL SUM Function with Distinct

SUM (DISTINCT Column Name) simply calculate the sum of the Unique number of records present in the table whose values are NOT NULL (Ignores the NULL Records).
TIP: In SQL, DISTINCT Keyword is used to remove the Duplicates from the specified column Name.
T-SQL CODE
Above SQL Query will Sum the Unique number of records (by removing duplicates) present in the [Yearly Income] column whose values are NOT NULL
OUTPUT
SQL SUM FUNCTION 3

SQL Sum Function with Having Clause

When we are grouping the data, In some cases we usually check for the conditions against the aggregated data. In these situations we use HAVING Clause along with Group By Statement. For example, The following query will group the Customers by their Occupation and then finds the Total Income.
T-SQL CODE
Below lines of code will check whether the aggregated amount (Sum of Yearly income of each individual Group) is greater than or equal to 200000 or not. If this is True then corresponding records will be displayed.
OUTPUT
SQL SUM FUNCTION 4
Thank You for Visiting Our Blog


SQL VAR Function

In SQL Server, VAR Function is used to calculate the statistical Variance of total records (or rows) selected by the  SELECT Statement.
TIP: VAR Function will only work on Numeric Columns and it ignores Nulls.
SQL Var Function Formula
SQL Var Function Syntax
In SQL Server, The basic syntax of the SQL Var() Function is:
In this article we will show you, How to write Var Function in SQL Server 2014 with example. For this, We are going to use the below shown data
SQL VAR FUNCTION

SQL VAR() Example

Var () simply returns the Variance of total number of records present in the specified column. For example, The following query will calculate the variance of total records present in the [Yearly Income] column from Customers table.
T-SQL CODE
OUTPUT
SQL VAR FUNCTION 1

SQL Var Function with Group By Clause

In most cases we usually calculate the variance of a products belongs to particular category or color etc. In these situations we use GROUP BY Clause to group the products by color or category and then use the Var Function to calculate the variance of products present in each group. Let us see the Example
T-SQL CODE
Above SQL Query will find, the Customers associated with the particular Department and calculates their income variance
OUTPUT
SQL VAR FUNCTION 2
Analysis
It is too big to show the calculations for every thing so, We are taking Skilled Manual profession and show you the output.

SQL Var Function in Having Clause

When we are grouping the data, In some cases we usually check for the conditions against the aggregated data. In these situations we use HAVING Clause along with Group By Statement. For example, The following query will group the Customers by their Occupation and then finds the Income variance of each individual group.
T-SQL CODE
Below lines of code will check whether the aggregated amount (Variance of Yearly income for each individual Group) is greater than 0 or not. If this is True then corresponding records will be displayed.
OUTPUT
SQL VAR FUNCTION 3
Thank You for Visiting Our Blog

SQL VARP Function

In SQL Server, VARP Function is used to calculate the statistical Variance for the population of total records (or rows) selected by the  SELECT Statement.
TIP: VARP Function will only work on Numeric Columns and it ignores Nulls.
SQL VARP Function Formula
SQL VARP Function Syntax
In SQL Server, The basic syntax of the SQL VARP() Function is:
In this article we will show you, How to write VARP Function in SQL Server 2014 with example. For this, We are going to use the below shown data
SQL VARP FUNCTION

SQL VARP() Example

VARP() simply returns the Variance  for the population of total number of records present in the specified column. For example, The following query will calculate the variance of total records present in the [Yearly Income] column from Customers table.
T-SQL CODE
OUTPUT
SQL VARP FUNCTION 1

SQL VARP Function with Group By Clause

In most cases we usually calculate the variance of a products belongs to particular category or color etc. In these situations we use GROUP BY Clause to group the products by color or category and then use the VARP Function to calculate the variance for population of products present in each group. Let us see the Example
T-SQL CODE
Above SQL Query will find, the Customers associated with the particular Department and calculates their income variance
OUTPUT
SQL VARP FUNCTION 2
Analysis
It is too big to show the calculations for every thing so, We are taking Skilled Manual profession and show you the output.

SQL VARP Function in Having Clause

When we are grouping the data, In some cases we usually check for the conditions against the aggregated data. In these situations we use HAVING Clause along with Group By Statement. For example, The following query will group the Customers by their Occupation and then finds the Income variance of each individual group.
T-SQL CODE
Below lines of code will check whether the aggregated amount (Variance for population of Yearly income for each individual Group) is greater than 0 or not. If this is True then corresponding records will be displayed.
OUTPUT
SQL VARP FUNCTION 3
Thank You for Visiting Our Blog