SQL Group by Clause
SQL Group by Clause
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];
Eliminate duplicate’s:
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.
3. Find the average age of sailors for each rating level that has at least two sailors.
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.