SQL – Group By
Table Of Contents:
- What Is SQL Group By ?
- Syntax Of SQL Group By.
- Examples Of SQL Group By.
(1) What Is SQL Group By?
- The
GROUP BY
statement groups rows that have the same values into summary rows, like “find the number of customers in each country”. - The
GROUP BY
statement is often used with aggregate functions (COUNT()
,MAX()
,MIN()
,SUM()
,AVG()
) to group the result set by one or more columns.
(2) Syntax Of SQL Group By?
SELECT column_name(s)
FROM table_name
WHERE condition
GROUP BY column_name(s)
ORDER BY column_name(s);
(3) Examples Of SQL Group By?
Example-1
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country;
Example-2
SELECT COUNT(CustomerID), Country
FROM Customers
GROUP BY Country
ORDER BY COUNT(CustomerID) DESC;