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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T05:59:24+00:00 2026-05-25T05:59:24+00:00

Here is the table: User Name: Subject: Peter Math Mary Chinese Mary Computer Mary

  • 0

Here is the table:
User

Name:  Subject:
Peter   Math
Mary    Chinese
Mary    Computer
Mary    Hist
Mary    PE
Mary    English
Peter   Art
Chris   English
Chris   Computer
Peter   Computer
Paul    Math

I would like to get the the top appear in name, and return top 4 result should be subject name. For example, in this case top appear name is Mary, and base on the order in subject, the Chinese , Computer, English, so I would like to have the result:

Mary    Chinese
Mary    Computer
Mary    English
Mary    Hist

If Mary is not the most enough to show the result, the second people will be the follow, like, let say the table will like this:

Name:  Subject:
Peter   Math
Mary    Chinese
Mary    Computer
Mary    Hist
Peter   Art
Chris   English
Chris   Computer
Peter   Computer
Paul    Math

The result will be,

Mary    Chinese
Mary    Computer
Mary    Hist
Peter   Art

Because Mary is the most appear, so Mary will return, but Mary is not enough to fill in 4 positions, so the second most appear will take the place, in this case, we use Peter.

  • 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-25T05:59:25+00:00Added an answer on May 25, 2026 at 5:59 am
    SELECT user.name, user.subject
    FROM user
    INNER JOIN (
        SELECT name, COUNT(1) AS occurrences
        FROM user
        GROUP BY name
      ) AS user_occurrences
      ON user.name = user_occurrences.name
    ORDER BY user_occurrences.occurrences DESC, user.name ASC, user.subject ASC
    LIMIT 4
    

    edit This might perform better, depending on the RDBMS you’re using and the size of the dataset. Try both and compare.

    SELECT user.name, user.subject
    FROM user
    INNER JOIN user AS user_occurrences
      ON user.name = user_occurrences.name
    GROUP BY user.name --, user.subject Second GROUP BY not needed on MySQL, but it should logically be there
    ORDER BY COUNT(user_occurrences.subject) DESC, user.name ASC, user.subject ASC
    LIMIT 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the table structure table : user id name email category table 2 :
here is my 3 tables: table 1 -- stores user information and it has
Here is my table: id int(11) name varchar(255) description text highest_bidder int(11) value varchar(255)
Here is a table structure (e.g. test): Field Name Data Type id BIGINT (20)
Here is my mysql table scheme; status table // has statusID, userID,date, subject, date
I have a Private-Message Table and a User-Table. I connect them here: var messageUsers
For refrence look http://singgihpraditya.wordpress.com/2010/02/13/spring-3-0-and-hibernate-tutorial-part-1/ @Entity @Table(name=USER) public class User implements Serializable { private Long
Suppose I have a table as follows in MySQL: create table user(name varchar(20), join_time
Here's the scenario: I have 2 tables: CREATE TABLE dbo.API_User ( id int NOT
Replaces Question: Update multiple rows into SQL table Here's a Code Snippet to update

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.