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

  • SEARCH
  • Home
  • 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 6545897
In Process

The Archive Base Latest Questions

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

I need a SQL statement, the requirement is: there is a table, which has

  • 0

I need a SQL statement, the requirement is: there is a table, which has two columns: ID, Owner_ID; I inserted several records, for example:

ID    Owner_ID
0         0
1         0
2         1
3         1
4         2
5         3
6         3
7         3
8         3
9         0

Now I need a SQL statement, which returns a ID list sorted by the number of rows owned by different user, from largest to smallest. In this example, owner 3 has four rows; owner 0 has three rows, owner 1 has two rows; and owner 2 has one rows. the result should be

ID    Owner_ID
5         3
6         3
7         3
8         3
0         0
1         0
9         0
2         1
3         1
4         2

I think I should use the aggregate function count, does anybody have an idea?

I am using HSQLDB.

Thanks in advance!!

  • 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:38:32+00:00Added an answer on May 25, 2026 at 11:38 am

    This will work with most SQL DBMS, but shows the count value.

    SELECT ID, Owner_ID, Owner_Count
      FROM AnonymousTable AS A
      JOIN (SELECT Owner_ID, COUNT(*) AS Owner_Count
              FROM AnonymousTable
             GROUP BY Owner_ID
           ) AS B ON B.Owner_ID = A.Owner_ID
     ORDER BY Owner_Count DESC, Owner_ID ASC, ID ASC;
    

    This will work with some, but not necessarily all, DBMS; it orders by a column that is not shown in the result list:

    SELECT ID, Owner_ID
      FROM AnonymousTable AS A
      JOIN (SELECT Owner_ID, COUNT(*) AS Owner_Count
              FROM AnonymousTable
             GROUP BY Owner_ID
           ) AS B ON B.Owner_ID = A.Owner_ID
     ORDER BY Owner_Count DESC, Owner_ID ASC, ID ASC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This SQL statement example is very close to what I think I need... update
I an SQL SELECT statement I need to extract the name of two teams,
I need a SQL statement to retrieve records where it key (or any column)
I need some help writing a SQL statement. I have two tables in a
There is a complex query which generates a report. The query has several sub
I have about 50 or so records in a table, I need a sql
I need a sql statement, to insert a new row in one database table
I need a SQL statement which fills the null values from the second column
In my sql statement, I need to retrieve rows which datediff more than 3
I need to transform an Oracle SQL statement into a Stored Procedure therefore users

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.