Group By is used to group the row having the same value like “Group by age of person”
The GROUP BY statement is often used with aggregate functions.
Mostly used aggregate function in group by is COUNT, MAX, MIN, SUM, AVG
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
/* ............... START ............... */ SELECT col1,col2 FROM table WHERE conditions GROUP BY col1,col2 SELECT col1,count(*) FROM table WHERE conditions GROUP BY column1 /* ............... END ............... */ |
Group By Examples
Example: List the students by grouping according to their classes
1 2 3 |
select * from students order by class |
Example : List the students to be different in each questioning randomly (SQL Server)
1 2 3 |
select * from students order by newid() |
Example: List the students to be different in each questioning randomly (MySQL)
1 2 3 |
select * from students order by rand() |