O'Reilly logo

Oracle SQL, A Beginner's Tutorial, Second Edition by Djoni Darmawikarta

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Chapter 4

Grouping

A group is a set of rows having the same value on specific columns. In Chapter 3, �Query Output� you learned how to apply aggregate functions on all output rows. In this chapter you will learn how to create groups and apply aggregate functions on those groups.

The GROUP BY Clause

In a query the GROUP BY clause appears after the WHERE clause and before the ORDER clause, if any. Here is the syntax for a SELECT statement with the WHERE, GROUP BY, and ORDER BY clauses.

SELECT columns,
  aggregate_function(group_columns)
FROM table(s)
WHERE condition
GROUP BY group_columns
ORDER BY column(s);

As an example, the query in Listing 4.1 groups the output from the product table by the launch date.

Listing 4.1: Grouping on one column

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required