The SQL GROUP BY clause groups data from a table into groups based on a specified column. It is used with aggregate functions like COUNT, MAX, AVG, MIN, and SUM to perform calculations on groupped data. Examples show using GROUP BY with single and multiple columns to get results like number of customers by age, maximum salary by age, average salary by address, and more.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
9 views2 pages
Group by
The SQL GROUP BY clause groups data from a table into groups based on a specified column. It is used with aggregate functions like COUNT, MAX, AVG, MIN, and SUM to perform calculations on groupped data. Examples show using GROUP BY with single and multiple columns to get results like number of customers by age, maximum salary by age, average salary by address, and more.
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2
The SQL GROUP BY clause is used in conjunction with the SELECT statement to arrange
identical data into groups.
This clause follows the WHERE clause in a SELECT statement and precedes the ORDER BY and HAVING clauses (if they exist). 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 Hyderabad 4500.00 7 Muffy 24 Indore 10000.00
SELECT AGE, COUNT(Name) FROM CUSTOMERS GROUP BY AGE;
AGE COUNT(Name) 32 1 25 2 23 1 27 1 22 1 24 1 SELECT AGE, MAX(salary) AS MAX_SALARY FROM CUSTOMERS GROUP BY AGE; AGE MAX_SALARY 32 2000.00 25 6500.00 23 2000.00 27 8500.00 22 4500.00 24 10000.00
SELECT ADDRESS, AVG(SALARY) as AVG_SALARY FROM CUSTOMERS GROUP BY
ADDRESS; ADDRESS AVG_SALARY Ahmedabad 2000.000000 Delhi 1500.000000 Kota 2000.000000 Mumbai 6500.000000 Bhopal 8500.000000 Hyderabad 4500.000000 Indore 10000.000000 GROUP BY Clause with Multiple Columns SELECT ADDRESS, AGE, SUM(SALARY) AS TOTAL_SALARY FROM CUSTOMERS GROUP BY ADDRESS, AGE;
1. SELECT AGE, MIN(SALARY) AS MIN_SALARY FROM CUSTOMERS GROUP BY AGE ORDER BY MIN_SALARY DESC; AGE MIN_SALARY 24 10000.00 27 8500.00 22 4500.00 32 2000.00 23 2000.00 25 1500.00 2. SELECT ADDRESS, AGE, MIN(SALARY) AS MIN_SUM FROM CUSTOMERS GROUP BY ADDRESS, AGE HAVING AGE>24;