Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6541925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:09:51+00:00 2026-05-25T11:09:51+00:00

is GROUP BY used only with aggregate functions ? Can anyone give an example

  • 0

is GROUP BY used only with aggregate functions ?
Can anyone give an example where it is used not in conjunction with aggregate functions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:09:51+00:00Added an answer on May 25, 2026 at 11:09 am

    SELECT with GROUP BY can be used as an alternative to SELECT DISTINCT. Pranay Rana’s example is essentially equivalent to

    SELECT DISTINCT UserName, Departmentid FROM user
    

    If you want an example where the preference of GROUP BY over DISTINCT is justified, here’s one. Say, you want to return items that only occur once in a table:

    SELECT Item
    FROM atable
    GROUP BY Item
    HAVING COUNT(*) = 1
    

    As you can see, the query only returns a non-aggregated column.

    It still uses an aggregate function, though, even if not in the SELECT clause. There may be situations where you must prefer GROUP BY to DISTINCT while not needing to use aggregate functions at all, but I’ll have to be platform specific at this point.

    In particular, if you are using SQL Server, you will find a difference between the results of the following two statements:

    #1:

    WITH data (f) AS (
      SELECT 'abc' UNION ALL
      SELECT 'def' UNION ALL
      SELECT 'abc'
    )
    SELECT f, ROW_NUMBER() OVER (ORDER BY f) FROM data GROUP BY f
    

    #2:

    WITH data (f) AS (
      SELECT 'abc' UNION ALL
      SELECT 'def' UNION ALL
      SELECT 'abc'
    )
    SELECT DISTINCT f, ROW_NUMBER() OVER (ORDER BY f) FROM data
    

    (If you aren’t, you can still see the results for yourself using Stack Exchange Data Explorer.)

    Both statements are meant to return distinct items ranked. However, only the first statement (with GROUP BY) returns the results as expected, while the latter (with DISTINCT) returns all items. Obviously, ranking functions in SQL Server are evaluated after GROUP BY, but before DISTINCT The reason is, the SELECT clause in SQL Server is evaluated after GROUP BY, but before DISTINCT, which makes you prefer GROUP BY over DISTINCT in this case. (Thanks @ypercube for nudging me in the right direction.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Clearly, when GROUP BY clause used, columns that are not aggregate function should be
Could someone tell me how the field Task Group is used in standard SharePoint
I've worked on several desktop applications that used tables to group information from a
I've seen (and used) on various projects this layout, with a group of fields
Given : Group hasMany Persons but the relationship is independent (ie. Persons can exist
How can HQL be used to select specific objects that meet a certain criteria?
Assuming I have the following aggregate functions: AGG1 AGG2 AGG3 AGG4 Is it possible
I have on occasion used a header file purely to import a group of
Is it possible to omit an aggregate column in a query? As an example:
I'm working on a database design for groups hierarchy used as the foundation of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.