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 298283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T06:43:02+00:00 2026-05-12T06:43:02+00:00

I have a table which has no primary key and I can’t add one

  • 0

I have a table which has no primary key and I can’t add one – the relevant columns from it are:

Department   | Category  | 
-------------+-----------+
0001         | A         |
0002         | D         |
0003         | A         | 
0003         | A         |
0003         | C         |
0004         | B         |

I want to retrieve a single row for each Department, which gives me the department code and the Category which appears most frequently in the table, i.e.

Department   | Category  | 
-------------+-----------+
0001         | A         |
0002         | D         |
0003         | A         | 
0004         | B         |

What is the best way to achieve this? My current attempt involves a Count(Category) in a subquery from which the Max(CountofCategory) is then taken, but including the Category field at this stage means too many rows at returned (since GROUP BY is applied at Category level as well as Department). In the case of a tie, I’d just select the min/max of the category arbitrarily. Ideally this should be database-agnostic, but is likely to run on either Oracle or MySQL.

  • 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-12T06:43:03+00:00Added an answer on May 12, 2026 at 6:43 am

    Works in both Oracle and SQL Server, I believe is all standard SQL, from later standards:

    with T_with_RN as
        (select Department
            , Category
            , row_number() over (partition by Department order by count(*) Desc) as RN
        from T
        group by Department, Category)
    select Department, Category
    from T_with_RN
    where RN = 1
    

    EDIT I don’t know why I used the WITH, the solution is probably easier to read using an inline view:

    select Department, Category
    from (select Department
        , Category
        , row_number() over (partition by Department order by count(*) Desc) as RN
        from T
        group by Department, Category) T_with_RN
    where RN = 1
    

    END EDIT

    Test cases:

    create table T (
        Department varchar(10) null,
        Category varchar(10) null
    );
    
    -- Original test case
    insert into T values ('0001', 'A');
    insert into T values ('0002', 'D');
    insert into T values ('0003', 'A');
    insert into T values ('0003', 'A');
    insert into T values ('0003', 'C');
    insert into T values ('0004', 'B');
    -- Null Test cases:
    insert into T values (null, 'A');
    insert into T values (null, 'B');
    insert into T values (null, 'B');
    insert into T values ('0005', null);
    insert into T values ('0005', null);
    insert into T values ('0005', 'X');
    -- Tie Test case
    insert into T values ('0006', 'O');
    insert into T values ('0006', 'P');
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this table in an Oracle DB which has a primary key defined
I have a logging table which has three columns. One column is a unique
i have a table which has a primary key (UserID) this is of type
I have a SqlServer 2008 table which has a Primary Key (IsIdentity=Yes) and three
I have a table y Which has two columns a and b Entries are:
I have a table in SQL Server 2005 which has three columns: id (int),
I have a Table in which i don't want to specify any primary key,
I generated a Members table and a MembersType table which has a primary key
I have a table which has two varchar(Max) columns Column 1 Column 2 -----------------------
Hi have a table named category which has two foreign keys to the table

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.