Skip to Main Content

SQL & PL/SQL

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

Group By clause and Group function

user10913793Apr 10 2019 — edited Apr 20 2019

hello,

I have two questions regarding 'Using group functions and GROUP BY' clause

Can we use GROUP BY clause without using group functions? Technically speaking, for a GROUP BY clause to be used, a group functions must appear  in the SELECT list.

Below example does not use a  group function but using two columns in group by clause and executes successfully

SQL> select job_id, max_salary from jobs group by max_salary,job_id;

JOB_ID     MAX_SALARY

----------      ----------

PR_REP          10500

AD_PRES         40000

MK_REP           9000

HR_REP           9000

PR

FI_ACCOUNT       9000

DBA

ST_CLERK         5000

MK_MAN          15000

HR

ST_MAN           8500

AC_ACCOUNT       9000

IT_PROG         10000

FI_MGR          16000

AC_MGR          16000

SA_REP          12000

PU_MAN          15000

PU_CLERK         5500

AD_VP           30000

AD_ASST          6000

SA_MAN          20000

SH_CLERK         5500

IT_DBA

23 rows selected.

and the below second example has no group functions but using just one column in group by clause  and throws error)

SQL> select job_id, max_salary from jobs group by max_salary;

select job_id, max_salary from jobs group by max_salary

       *

ERROR at line 1:

ORA-00979: not a GROUP BY expression

---------------------------------------------------------------------------------------------------------

Please explain the difference why the result is different in the second example

Thank you

Thank you

This post has been answered by CarlosDLG on Apr 10 2019
Jump to Answer

Comments

CarlosDLG
Answer

No, you are not forced to use aggregate functions when using the GROUP BY clause.  You will probably most of the time use them, but it is not a requirement.

Regarding the second example, that happens because anything that appears in the SELECT list, that is not an aggregate function or an expression involving an aggregate function (or USER, UID, SYSDATE), must appear in the GROUP BY clause.  Otherwise, you get that error.

Marked as Answer by user10913793 · Sep 27 2020
jaramill

Read the documentation on the "Group_By" clause --> https://docs.oracle.com/database/121/SQLRF/statements_10002.htm#i2182483

group_by_clause

Specify the GROUP BY clause if you want the database to group the selected rows based on the value of expr(s) for each row and return a single row of summary information for each group. If this clause contains CUBE or ROLLUP extensions, then the database produces superaggregate groupings in addition to the regular groupings.

Expressions in the GROUP BY clause can contain any columns of the tables, views, or materialized views in the FROM clause, regardless of whether the columns appear in the select list.

The GROUP BY clause groups rows but does not guarantee the order of the result set. To order the groupings, use the ORDER BY clause.

If the NLS_SORT parameter has a setting other than BINARY and the NLS_COMP parameter is set to LINGUISTIC, then expression values are compared linguistically according to the linguistic definition specified in NLS_SORT to decide if they are equal and therefore belong to the same group. When character values are compared linguistically for GROUPBY, they are first transformed to collation keys and then compared like RAW values. The collation keys are the same values that are returned by the function NLSSORT and are subject to the same restrictions that are described in "NLSSORT". As a result of these restrictions, two values may compare as linguistically equal and be grouped together if they do not differ in the prefix that was used to produce the collation key, even if they differ in the rest of the value.

Frank Kulash

Hi,

As you discovered (and as Carlos said) you can use GROUP BY without an aggregate function.

However, if you're ever tempted to do that, stop and think if it's the best way.  In you first example, SELECT DISTINCT might be clearer.

As for the second example (why you get the error) here's a rule, the ABC's of GROUP BY:

When you use a GROUP BY clause and/or an aggregate function, then everything in the SELECT clause must be

(A) an Aggregate function,

(B) one of the "group By" expressions,

(C) a Constant, or

(D) something that Depends entirely on the above.

vansul

You can use all the selected columns in the group by columns

Then you will find the distinct columns. It will be same if you will try use distinct.

As

select job_id, max_salary from jobs group by job_id,max_salary

Will be same as

select distinct job_id, max_salary from jobs

user10913793

Thank you, this answers my question

user10913793

Solves my question, thank you

1 - 6

Post Details

Added on Apr 10 2019
6 comments
500 views