EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Oracle Tutorial GROUP BY in Oracle
Secondary Sidebar
Oracle Tutorial
  • Advanced
    • Timestamp to Date in Oracle
    • Oracle Golden Gate
    • Oracle Virtual Machine
    • Oracle Describe Table
    • Oracle Clauses
    • Oracle Having Clause
    • Oracle?Primavera
    • Oracle FOREIGN Key
    • PIVOT in Oracle
    • Oracle Alter Table
    • Oracle Queries
    • Oracle Views
    • Oracle Window Functions
    • Oracle String Functions
    • Oracle Date Functions
    • Oracle Analytic Functions
    • Oracle Aggregate Functions
    • Select in Oracle
    • INSERT in Oracle
    • DISTINCT in Oracle
    • Function in Oracle
    • Oracle GROUP_CONCAT
    • Oracle INSTR()
    • Oracle CONVERT
    • Oracle LENGTH()
    • Oracle EXISTS
    • Oracle REPLACE()
    • Oracle MERGE
    • Oracle LEAD()
    • Oracle EXTRACT()
    • Oracle LISTAGG()
    • Oracle SYS_CONTEXT()
    • Oracle COALESCE
    • Oracle NVL()
    • Oracle SYSDATE()
    • Oracle?Date Format
    • Oracle SYS_GUID()
    • Oracle WILDCARDS
    • Oracle Synonyms
    • Oracle Subquery
    • BETWEEN in Oracle
    • FETCH in Oracle
    • Oracle Index
    • Oracle Function-based Index
    • Oracle UNIQUE Index
    • Oracle Bitmap Index
    • Oracle Column
    • Oracle Triggers
    • Oracle Procedures
    • Sample Database for Oracle
    • Oracle LIKE Operator
    • ORDER BY in Oracle
    • Oracle ORDER BY DESC
    • GROUP BY in Oracle
    • Oracle GROUP BY HAVING
    • Oracle Aliases
    • Table in Oracle
    • Oracle Temporary Table
    • Oracle? Table Partition
    • Oracle rename table
    • Oracle CTE
    • Cursor in Oracle
    • Oracle LOCK TABLE
    • Oracle Tablespace
    • Oracle CARDINALITY
    • Oracle REGEXP
    • Oracle REGEXP_REPLACE
    • Oracle to_date
    • JSON in Oracle
    • Oracle COMMIT
    • Oracle GRANT
    • Oracle MD5
    • Oracle ROLLBACK
    • Oracle Users
    • Oracle TIMESTAMP
    • IF THEN ELSE in Oracle
    • Oracle While Loop
    • Oracle Clone Database
    • Oracle Backup Database
    • Oracle? XML
    • Oracle XMLAGG
    • Oracle XMLTABLE
    • Oracle Performance Tuning
    • Oracle B Tree Index
    • Oracle fusion
    • Oracle ebs
    • Oracle GRC
    • Oracle ERP
    • Oracle ASM
    • Oracle Cloud
    • Oracle HCM Cloud
    • Oracle Integration Cloud
    • Oracle Jinitiator
    • Oracle pathfinder
    • Oracle VirtualBox
    • Oracle Weblogic Server
    • Oracle decode
    • Oracle Exadata
    • Oracle ZFS
    • Oracle? utilities
    • JDBC Driver for Oracle
    • Oracle? DBA Versions
    • Oracle DBA Salary
  • Basic
    • Oracle Marketing Cloud
    • What is Oracle?
    • Career in Oracle
    • How to Install Oracle
    • Oracle Versions
    • What Is Oracle Database
    • Oracle Data Warehousing
    • Oracle Warehouse Builder
    • Career In Oracle Database Administrator
    • Career In Oracle DBA
    • What is Oracle RAC
    • Oracle DBA
    • Oracle? Vanderbilt
    • What is RMAN Oracle
    • Oracle Database Administration
    • Oracle Operators
    • Oracle Constraints
    • Oracle number
    • Oracle Data Types
    • Oracle UNIQUE Constraint
    • Oracle Check Constraint
  • Joins
    • Joins in Oracle
    • Inner Join in Oracle
    • Oracle Cross Join
    • Left Join in Oracle
    • OUTER Join in Oracle
    • Oracle Full Outer Join
    • Natural Join in Oracle
    • Oracle Self Join
    • Oracle hash join
    • Oracle? Update with Join
  • Oracle SET Operators
    • UNION in Oracle
    • Oracle UNION ALL
    • INTERSECT in Oracle
    • MINUS in Oracle
  • Interview Questions
    • Oracle Interview Questions
    • Oracle Apps Interview Questions
    • Oracle Apps Technical Interview Questions
    • Oracle Database Interview Questions
    • Oracle Forms Interview Questions
    • Oracle PL/SQL Interview Questions
    • Oracle RAC Interview Questions
    • Oracle SOA Interview Questions

Related Courses

Oracle Course Training

Oracle DBA Certification Course

MongoDB Certification Training

GROUP BY in Oracle

By Priya PedamkarPriya Pedamkar

GROUP BY in Oracle

Introduction to GROUP BY in Oracle

The GROUP BY Oracle clause is used to decide the rows in a table into groups. It is used in the SELECT statement for grouping the rows by values of column or expression. The GROUP BY clause groups the results by one or more columns value.

Points of Concentration

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

  • If the group function is included in a SELECT statement then individual result column(s) cannot be used without GROUP BY clause.
  • The extra non-group functional columns should be declared in the GROUP BY clause.
  • Rows can be pre excluded before dividing them into groups by using the WHERE clause.
  • Column ALIAS cannot be used in the GROUP BY clause.
  • By default, rows are sorted by ascending order of the column(s) included in the GROUP BY list.
  • The columns applied upon the GROUP BY clause need not be part of the SELECT list.
  • If the group function is included with the non-group functional column in a SELECT statement then GROUP BY clause must be used.

Syntax

1. Syntax without Group Function

All in One Data Science Bundle(360+ Courses, 50+ projects)
Python TutorialMachine LearningAWSArtificial Intelligence
TableauR ProgrammingPowerBIDeep Learning
Price
View Courses
360+ Online Courses | 50+ projects | 1500+ Hours | Verifiable Certificates | Lifetime Access
4.7 (85,992 ratings)

SELECT Column_1, Column_2,..., Column_N FROM Table_Name WHERE condition(s)
GROUP BY Column_Name(s) ORDER BY Column(S);

2. Syntax with Group Function

SELECT Column_1, Column_2,..., GROUP_FUN(Column) FROM Table_Name
WHERE condition(s) GROUP BY Column_Name(s) ORDER BY Column(S);

Description:

  • Col_1/2/n: The column(s) or calculation as per your requirement.
  • Table_Name: As per your requirement
  • WHERE: It’s optional, depends on your requirement
  • GROUP_FUN: AVG, SUM, MIN, MAX

Example: Without GROUP Function

SQL>   SELECT Job FROM Emp GROUP BY Job;

Output:

GROUP BY Oracle

Example: With GROUP Function

SQL>   SELECT Deptno, AVG (Sal) FROM Emp GROUP BY Deptno;

Output:

GROUP BY Oracle

Explanation: The above two examples (without and with ALIAS name) clearly shows that how GROUP BY clause groups the rows based on condition.

Implementations of Oracle GROUP BY Clause with Examples

In this section, we’ll see the implementation of the Oracle GROUP BY Clause and its behavior. For that, we will use the below sample table (Emp) with 14 records to understand the Oracle GROUP BY Clause behavior.

SQL> SELECT * from Emp;

Output:

Implementation

Example #1

GROUP BY clause with Non-GROUP Functional columns

SQL > SELECT Empno, Ename, Job, Deptno FROM Emp GROUP BY Deptno;

Output:

Example 1

The above SELECT statement throws an error because there are four Non GROUP Functional columns included in the SELECT statement but only one column is included in the GROUP BY clause.

To prevent this error all non-group functional columns should be included in the GROUP BY clause. Example below:

SQL > SELECT Empno, Ename, Job, Deptno FROM Emp GROUP BY Empno, Ename,Job, Deptno;

Output:

SELECT Empno

Example #2

Column ALIAS name in GROUP BY Clause

SQL> SELECT TO_CHAR (Hiredate, 'YYYY') Year FROM Emp GROUP BY Year;

Output:

GROUP BY Oracle

The above SELECT statement throwing an invalid identifier error because column ALIAS name (Year) being used in GROUP BY Clause. As per rule Column ALIAS cannot be used in the GROUP BY clause. The correct SELECT statement is below:

SQL> SELECT TO_CHAR (Hiredate, 'YYYY') Year FROM Emp GROUP BY
TO_CHAR (Hiredate, 'YYYY');

Output:

SELECT TO_CHAR

Example #3

GROUP BY Clause with GROUP Function

SQL> SELECT Deptno, AVG (Sal) FROM Emp GROUP BY Deptno;

Output:

GROUP BY Oracle

In the above example, Output showing an Average salary of each Deptno after grouping the Deptno.

SQL> SELECT Deptno, Job, SUM (Sal) FROM Emp GROUP BY Deptno;

Output:

GROUP BY Oracle

But the above SELECT statement throwing an error because there are two Non GROUP Functional columns included with GROUP functional column in the SELECT statement but only one Non-GROUP Functional column included in the GROUP BY clause.

To prevent this error all non-group functional columns should be declared in the GROUP BY clause. Example below:

SQL> SELECT Deptno, Job, SUM (Sal) FROM Emp GROUP BY Deptno, Job;

Output:

SELECT Deptno

Example #4

GROUP FUNCTIONAL column and Non-GROUP Functional column without GROUP BY Clause

SQL> SELECT Deptno, AVG (Sal) FROM Emp;

Output:

GROUP BY Oracle

The above SELECT statement pop-ups an error because GROUP FUNCTIONAL column is being used with the Non-GROUP Functional column. As per rule Non-GROUP, the Functional column must be included in the GROUP BY clause if the GROUP FUNCTIONAL column is being used.

See below the correct SELECT statement for the above scenario:

SQL> SELECT Deptno, AVG (Sal) FROM Emp GROUP BY Deptno;

Output:

GROUP BY Oracle

Example #5

Few more examples for Group-wise Summaries

SQL> SELECT Deptno, AVG (Sal) FROM Emp GROUP BY Deptno ORDER BY
AVG (Sal);

Output:

Group-wise Summaries

SQL> SELECT Deptno, MIN (Sal), MAX (Sal) FROM Emp GROUP BY deptno;

Output:

GROUP BY Oracle

SQL> SELECT Deptno, MIN (Sal), MAX (Sal) FROM Emp WHERE Job=’CLERK’
GROUP BY deptno;

Output:

GROUP BY Oracle

Note: The above example shows that the column which exists in the Table but not included in the SELECT statement, can be used in the WHERE clause to filter the rows.

TIPS:

  • Do not need to use the GROUP BY clause if only the GROUP FUNCTIONAL column is being used.
  • GROUP FUNCTIONAL column ALIAS name can be used in the ORDER BY clause but not in the GROUP BY clause.
  • GROUP FUNCTIONAL column always returns a single row result.

Conclusion

Oracle GROUP BY Clause is an expression or keyword which groups a result set into subsets that have matching values for one or more columns. To get the result set in groups or need to apply aggregate or GROUP function with the Non-GROUP Functional column, Oracle GROUP BY Clause is a good option.

Recommended Articles

This is a guide to GROUP BY in Oracle. Here we discuss the introduction, points of concentration and implementations of Oracle GROUP BY Clause with examples. You can also go through our other related articles to learn more–

  1. INTERSECT in Oracle
  2. GROUP BY clause in SQL
  3. Cursor in MySQL
  4. UNION in Oracle
  5. Examples of Table in Oracle
  6. Guide to Cursor in Oracle with Example
Popular Course in this category
Oracle Training (14 Courses, 8+ Projects)
  14 Online Courses |  8 Hands-on Projects |  120+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Oracle DBA Database Management System Training (2 Courses)4.9
All in One Financial Analyst Bundle- 250+ Courses, 40+ Projects4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2022 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

SPSS, Data visualization with Python, Matplotlib Library, Seaborn Package

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more