Introduction
In this article i will explain about what is Group By and how to use this in Sql Server.
What Is
Group By clause is used for grouping the records of the database table(s). This clause creates a single row for each group and this process is called aggregation. The GROUP BY statement is used in conjunction with the aggregate functions to group the result-set by one or more columns.To use group by clause we have to use at least one aggregate function in Select statement. We can use group by clause without where clause.
Practice
Here i am writing few sql queries by using Group By clause.
Synatax :
SELECT column_name, aggregate_function(column_name)
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
FROM table_name
WHERE column_name operator value
GROUP BY column_name;
Aggregate Function
This is an aggregate function such as the SUM, COUNT, MIN, MAX, or AVG functions.
using group By with Aggregate Functions :
Using SUM
SELECT DepId, SUM(Salary) AS Total_Salaries
FROM Employees
GROUP BY DepId ;
FROM Employees
GROUP BY DepId ;
Using COUNT
SELECT DepId, COUNT(*) AS TotalEmployees
FROM Employees
GROUP BY DepId ;
FROM Employees
GROUP BY DepId ;
Using MAX
SELECT DepId, MAX(Salary) AS Max_Salaries
FROM Employees
GROUP BY DepId ;
FROM Employees
GROUP BY DepId ;
Using MIN
SELECT DepId, MIN(Salary) AS Min_Salaries
FROM Employees
GROUP BY DepId ;
FROM Employees
GROUP BY DepId ;
0 comments:
Post a Comment