ITSourceCode.com

We Exist to Provide 100% Free Source Code and Tutorials

Custom Search

SQL GROUP BY Statement

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

Check Your Domain Ranking

Leave a Reply

Your email address will not be published. Required fields are marked *

ITSourceCode.com © 2016 Frontier Theme

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!