You are on page 1of 2

SQL Group by Clause

The GROUP BY clause is a SQL command that is used to group rows that have the same
values. The GROUP BY clause is used in the SELECT statement. Optionally it is used in
conjunction with aggregate functions to produce summary reports from the database.

The queries that contain the GROUP BY clause are called grouped queries and only return a
single row for every grouped item.

Syntax
SELECT statements... GROUP BY column_name1[,column_name2,...] [HAVING condition];

 SELECT statements…” is the standard SQL SELECT command query.


 “GROUP BY column_name1” is the clause that performs the grouping based on
column_name1.
 “[,column_name2,…]” is optional; represents other column names when the
grouping is done on more than one column.
 “[HAVING condition]” is optional; it is used to restrict the rows affected by the
GROUP BY clause. It is similar to the WHERE clause.

Grouping using a Single Column

SELECT `gender` FROM `members` ;

we want to get the unique values for genders.

SELECT `gender` FROM `members` GROUP BY `gender`;

Grouping using multiple columns


SELECT `category_id`,`year_released` FROM `movies` ;

Eliminate duplicate’s:

SELECT `category_id`,`year_released` FROM `movies` GROUP BY


`category_id`,`year_released`;

Restricting query results using the HAVING clause

SELECT * FROM `movies` GROUP BY `category_id`,`year_released` HAVING `category_id` = 8;


Summary

 The GROUP BY Clause SQL is used to group rows with same values.
 The GROUP BY Clause is used together with the SQL SELECT statement.
 The SELECT statement used in the GROUP BY clause can only be used contain column
names, aggregate functions, constants and expressions.
 SQL Having Clause is used to restrict the results returned by the GROUP BY clause.
 MYSQL GROUP BY Clause is used to collect data from multiple records and returned
record set by one or more columns.

1. Find the name and the age of the youngest sailor.


SELECT S.sname, S.age FROM Sailors S
WHERE S.age = (SELECT MIN(S2.age) FROM Sailors S2 )

2. Find the average age of sailors for each rating level

SELECT S.rating, AVG(S.age)


AS avg_age FROM Sailors S GROUP BY S.rating

3. Find the average age of sailors for each rating level that has at least two sailors.

SELECT S.rating, AVG(S.age)


AS avg_age FROM Sailors S GROUP BY S.rating HAVING COUNT(*) > 1

4. An example shows difference between WHERE and HAVING

SELECT S.rating, AVG(S.age) as avg_age


FROM Sailors S WHERE S.age >=40 GROUP BY S.rating

SELECT S.rating, AVG(S.age) as avg_age


FROM Sailors S GROUP BY S.rating HAVING AVG(S.age) >= 4

5. Find the age of the youngest sailor for each rating level.
select s1.rating,min(s1.age) from sailors s1 group by s1.rating ;

6. Find the age of the youngest sailor who is eligible to vote (i.e., is at least 18 years
old) for each rating level with at least two such sailors.

select s1.rating,min(s1.age) from sailors s1 where s1.age>18 group by s1.rating


having count(*)>=2 ;
7. For each red boat, find the number of reservations for this boat.

select b.bid,count(*) as NoOfReservations from reserves r,boats b where r.bid=b.bid


and b.color='red' group by b.bid

You might also like