Monday, 29 May 2017

DBMS Group BY clause ~ GNIITHELP

Group By Clause

Group by clause is used to group the results of a SELECT query based on one or more columns. It is also used with SQL functions to group the result from one or more tables.
Syntax for using Group by in a statement.
SELECT column_name, function(column_name)
FROM table_name 
WHERE condition 
GROUP BY column_name

Example of Group by in a Statement

Consider the following Emp table.
eidnameagesalary
401Anu229000
402Shane298000
403Rohan346000
404Scott449000
405Tiger358000
Here we want to find name and age of employees grouped by their salaries
SQL query for the above requirement will be,
SELECT name, age 
from Emp group by salary
Result will be,
nameage
Rohan34
shane29
anu22

Example of Group by in a Statement with WHERE clause

Consider the following Emp table
eidnameagesalary
401Anu229000
402Shane298000
403Rohan346000
404Scott449000
405Tiger358000
SQL query will be,
select name, salary 
from Emp 
where age > 25
group by salary
Result will be.
namesalary
Rohan6000
Shane8000
Scott9000
You must remember that Group By clause will always come at the end, just like the Order by clause.

No comments:

Post a Comment