The SQL Server GROUP BY proviso is utilized as a team with the SELECT assertion to organize indistinguishable information into gatherings.
The GROUP BY condition follows the WHERE provision in a SELECT explanation and goes before the ORDER BY proviso.
Syntax
Following is the fundamental language structure of GROUP BY provision. The GROUP BY proviso should follow the conditions in the WHERE statement and should go before the ORDER BY condition on the off chance that one is utilized.
SELECT column1, column2
FROM table_name
WHERE [ conditions ]
GROUP BY column1, column2
ORDER BY column1, column2
Example
Consider the CUSTOMERS table is having the accompanying records −
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
On the off chance that you need to know the aggregate sum of pay on every client, at that point following will be the GROUP BY inquiry.
SELECT NAME, SUM(SALARY) as [sum of salary] FROM CUSTOMERS
GROUP BY NAME;
The above order will create the accompanying yield.
NAME sum of salary
Chaitali 6500.00
Hardik 8500.00
kaushik 2000.00
Khilan 1500.00
Komal 4500.00
Muffy 10000.00
Ramesh 2000.00
Allow us presently to consider the accompanying CUSTOMERS table having the accompanying records with copy names.
ID NAME AGE ADDRESS SALARY
1 Ramesh 32 Ahmedabad 2000.00
2 Khilan 25 Delhi 1500.00
3 kaushik 23 Kota 2000.00
4 Chaitali 25 Mumbai 6500.00
5 Hardik 27 Bhopal 8500.00
6 Komal 22 MP 4500.00
7 Muffy 24 Indore 10000.00
On the off chance that we need to know the aggregate sum of pay on every client, at that point following will be GROUP BY question.
SELECT NAME, SUM(SALARY) as [sum of salary] FROM CUSTOMERS
GROUP BY NAME
The above order will create the accompanying yield.
NAME sum of salary
Hardik 8500.00
kaushik 8500.00
Komal 4500.00
Muffy 10000.00
Ramesh 3500.00