SQL GROUP BY Statement

0
22

sqltuts

The SQL Group by statement is used to group the results of a SELECT query based on one or more columns.

 

The syntax for using Group by in a statement.

 

  1. SELECT column_name, function(column_name)
  2. FROM table_name
  3. WHERE condition
  4. GROUP BY column_name

 

Example of Group by in a Statement

Consider the following Consumer table.

Consumer id name age salary
10 rajat 20 90000
11 karan 45 85000
12 ram 37 67000
21 Shyam 41 85000
23 jon 33 90000

Here we will sort or I mean  grouped by their salaries

SQL query for the above requirement will be,

  1. SELECT name, age
  2. from Consumers group by salary

Result will be,

Name age
Rajat 20
karan 45
Ram 37

Example of Group by in a Statement with WHERE clause

Consider the following Emp table

Consumer id name age salary
10 rajat 20 90000
11 karan 18 85000
12 ram 23 67000
21 Shyam 28 85000
23 jon 33 90000

 

SQL query will be,

  1. select name, salary
  2. from Consumer
  3. where age > 28
  4. group by salary

The result will be.

name salary
jon 90000

 

Facebook Comments
(Visited 22 times, 1 visits today)
SHARE
Previous articleSQL Distinct Keyword
Next articleSQL Having Clause
Hello Itsourcecoders, welcome to itsourcecode.com. I'm Joken Villanueva, MIT a passionate Blogger, Programmer and a Hobbyist. I started Itsourcecode because I wanted to give back and Share all the learnings and knowledge I've learned in my career and I believe through this website I would be able to help and assist those newbie programmers in enhancing their skills from different programming languages. So let us all help each other by sharing our ideas!

LEAVE A REPLY

Please enter your comment!
Please enter your name here