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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:28:11+00:00 2026-05-12T13:28:11+00:00

How do I get the most frequently occurring category for each tag in MySQL?

  • 0

How do I get the most frequently occurring category for each tag in MySQL? Ideally, I would want to simulate an aggregate function that would calculate the mode of a column.

SELECT 
  t.tag 
  , s.category 
FROM tags t 
LEFT JOIN stuff s 
USING (id) 
ORDER BY tag;

+------------------+----------+
| tag              | category |
+------------------+----------+
| automotive       |        8 |
| ba               |        8 |
| bamboo           |        8 |
| bamboo           |        8 |
| bamboo           |        8 |
| bamboo           |        8 |
| bamboo           |        8 |
| bamboo           |       10 |
| bamboo           |        8 |
| bamboo           |        9 |
| bamboo           |        8 |
| bamboo           |       10 |
| bamboo           |        8 |
| bamboo           |        9 |
| bamboo           |        8 |
| banana tree      |        8 |
| banana tree      |        8 |
| banana tree      |        8 |
| banana tree      |        8 |
| bath             |        9 |
+-----------------------------+
  • 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-12T13:28:11+00:00Added an answer on May 12, 2026 at 1:28 pm
    SELECT t1.*
    FROM (SELECT tag, category, COUNT(*) AS count
          FROM tags INNER JOIN stuff USING (id)
          GROUP BY tag, category) t1
    LEFT OUTER JOIN 
         (SELECT tag, category, COUNT(*) AS count
          FROM tags INNER JOIN stuff USING (id)
          GROUP BY tag, category) t2
      ON (t1.tag = t2.tag AND (t1.count < t2.count 
          OR t1.count = t2.count AND t1.category < t2.category))
    WHERE t2.tag IS NULL
    ORDER BY t1.count DESC;
    

    I agree this is kind of too much for a single SQL query. Any use of GROUP BY inside a subquery makes me wince. You can make it look simpler by using views:

    CREATE VIEW count_per_category AS
        SELECT tag, category, COUNT(*) AS count
        FROM tags INNER JOIN stuff USING (id)
        GROUP BY tag, category;
    
    SELECT t1.*
    FROM count_per_category t1
    LEFT OUTER JOIN count_per_category t2
      ON (t1.tag = t2.tag AND (t1.count < t2.count 
          OR t1.count = t2.count AND t1.category < t2.category))
    WHERE t2.tag IS NULL
    ORDER BY t1.count DESC;
    

    But it’s basically doing the same work behind the scenes.

    You comment that you could do a similar operation easily in application code. So why don’t you do that? Do the simpler query to get the counts per category:

    SELECT tag, category, COUNT(*) AS count
    FROM tags INNER JOIN stuff USING (id)
    GROUP BY tag, category;
    

    And sort through the result in application code.

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

Sidebar

Related Questions

I would like to get some frequently occurring phrases with Lucene. I am getting
Is that possible to get some kind of stats from solr. E.g. Most frequently
I want to get the most recent 10,000 entries from CNN's top stories RSS
I would like to get the most out of working in cch or tcsh
I tend to get the feeling that most folks here think SVN is gold-plated
This must be the most frequently occurring issue in my life! I have to
I am trying to get the most occurring term frequencies for every particular document
I'm trying to figure out which files get edited the most frequently. Is there
What's the most efficient way in Java to get the 50 most frequent words
one of the most frequent requests I get is to create XY report for

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.