Infogoal Logo
GOAL DIRECTED LEARNING
Master SQL

SQL TUTORIAL HOME

SQL OVERVIEW
SQL SYNTAX
SQL BOOKREVIEWS

SQL BASICS

SQL SELECT
SQL WHERE
SQL INSERT
SQL UPDATE
SQL DELETE

SQL ADMINISTRATION

SQL CREATE DATABASE
SQL DROP DATABASE
SQL CREATE TABLE
SQL ALTER TABLE
SQL DROP TABLE
SQL CREATE INDEX
SQL DROP INDEX
SQL ADD FOREIGN KEY
SQL DROP FOREIGN KEY
SQL CREATE VIEW
SQL DROP VIEW

SQL ADVANCED

SQL CONCAT
SQL SUBSTRING
SQL TRIM
SQL AND & OR
SQL IN
SQL BETWEEN
SQL LIKE
SQL DISTINCT
SQL GROUP BY
SQL AGGREGATE
SQL HAVING
SQL ORDER BY
SQL JOIN
SQL OUTER JOIN

SQL GROUP BY

Previous | Next

What is SQL GROUP BY?

The SQL GROUP BY is a clause enables SQL aggregate functions for a grouping of information. For example, it could support subtotaling by region number in a sales table.

GROUP BY supports dividing data into sets so that aggregate functions like SUM, AVG and COUNT can be performed.

Why Use SQL GROUP BY?

The SQL GROUP BY clause is used whenever aggregate functions by group are required. This is an aid to understanding and analyzing information.

How To Use SQL GROUP BY

SQL GROUP BY is used as follows. It must follow the FROM and WHERE clauses. The columns in a SELECT clause must be either group by columns or aggregate function columns.

SQL GROUP BY Syntax

SELECT <column_name1>, <column_name2> <aggregate_function>
FROM <table_name>
GROUP BY <column_name1>, <column_name2>

SQL GROUP BY Example

The following example produces a count of branches by region_nbr from the branch table.

Here are the contents of the table:

Table: BRANCH
branch_nbrbranch_nameregion_nbremployee_count
108New York10010
110Boston1006
212Chicago2005
404San Diego4006
415San Jose4003

This SQL Statement with GROUP BY is executed:

SELECT region_nbr, count(*)
FROM branch
GROUP BY region_nbr
ORDER BY region_nbr

Here is the result.

region_nbrcount(*)
1002
2001
4002

Previous | Next


Advertisements

Advertisements:


Infogoal.com is organized to help you gain mastery.
Examples may be simplified to facilitate learning.
Content is reviewed for errors but is not warranted to be 100% correct.
In order to use this site, you must read and agree to the terms of use, privacy policy and cookie policy.
Copyright 2006-2020 by Infogoal, LLC. All Rights Reserved.

Infogoal Logo