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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:35:36+00:00 2026-05-26T20:35:36+00:00

My question is surely banal but i can’t set up an sql query that

  • 0

My question is surely banal but i can’t set up an sql query that allows me to make a list of top 3 countries for a sport-event summary table.
I explain me better: in a sport event i have a lot of athletes from different countries and i need to produce a summary table showing countries that won more medals.
Here is an example:

--------------------------------------------
|id |     name    |   activity  | country  |
--------------------------------------------
| 1 |  John       |   100m      |  USA     |
| 2 |  Andy       |   200m      |  CANADA  |
| 3 |  Frank      |   400m      |  USA     |
| 4 |  Ian        |   400m      |  GERMANY |
| 5 |  Anthony    |   100m      |  USA     |
| 6 |  Eric       |   400m      |  CANADA  |
| 7 |  Mike       |   200m      |  UK      |
| 8 |  Dave       |   200m      |  GERMANY |
| 9 |  Richard    |   100m      |  USA     |
| 10|  Max        |   100m      |  USA     |
| 11|  Randy      |   100m      |  USA     |
| 12|  Maurice    |   400m      |  CANADA  |
| 13|  Col        |   100m      |  UK      |
| 14|  Jim        |   400m      |  USA     |
| 15|  Adam       |   200m      |  BRAZIL  |
| 16|  Ricky      |   100m      |  UK      |
| 17|  Emily      |   400m      |  USA     |
| 18|  Serge      |   200m      |  UK      |
| 19|  Alex       |   400m      |  FRANCE  |
| 20|  Enamuel    |   100m      |  USA     |
--------------------------------------------

The summary table i wish to obtain is the following:

Top 3 countries

--------------------------------------
| position |    country    | medals  |
--------------------------------------
|     1    |     USA       |    9    |
|     2    |     UK        |    4    |
|     3    |     CANADA    |    3    |
--------------------------------------

How can build the qsl query?
Thanks in advance for your kind answer.

Mattew

  • 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-26T20:35:36+00:00Added an answer on May 26, 2026 at 8:35 pm

    Without the position column, this is quite easy. Just do the following

    SELECT Country,COUNT(*) AS medals
    FROM MyTable
    GROUP BY Country
    ORDER BY COUNT(*) DESC
    LIMIT 3;
    

    There is some more complicated code for getting the “position” column out, but unless you need it, it probably isn’t necessary, and you can just get those numbers using a counter on the processing code. If you’re interested, the code would be something like this.

    SELECT @rownum:=@rownum+1 AS Position,Country,Medals FROM
    (
    SELECT Country,COUNT(*) AS medals
    FROM Medals
    GROUP BY Country
    ORDER BY COUNT(*) DESC
    LIMIT 3
    ) AS Stats, (SELECT @rownum:=0) RowNum;
    

    The above query has been tested and appears to be working as you need it to be.

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

Sidebar

Related Questions

This surely is a noob question, but I can't find an answer in Doxygen
Question Can I build a image database/library that has an e-commerce style checkout system
Simple question here that I can't seem to find an answer for: Once a
this is surely a basic question for most of you but it still giving
surely this question has beed debated thousand of time, but I do not find
This is surely a basic question, but I'm new enough to MVC (and ASP.NET)
Surely this is a newb question as I am just learning RoR and can't
This question is surely answered here somewhere, but I couldn't find it. I've been
This is surely a stupid question, but I'm stumped. I've now read Odersky's book,
I think that this is a newbie type question but I have quite understood

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.