We help IT Professionals succeed at work.
Get Started

MySql - select top 3 numbers

Rick asked
Last Modified: 2012-05-06

I have the following table:

_code          _quantity

004              30.91
045              11.24
049              11.24
043              2
045              5
004              6.94
042              15.36
043              20.81
043              3.16
045              19.43
046              2.78
048              1.33
049              13.88

I want to sum all quantities for all codes (if that makes sense: sum all 004's, all 049's etc). Then I wan to display the top 3 numbers.

If I run this query, I get what I need, but I want them in order (highest to lowest).

SELECT _code, sum(_quantity)
FROM tbl_table
GROUP BY _code
ORDER BY sum(_quantity)

My query results:

_code          _sum(_quantity)
045              35.67
043              25.97
004              37.85

Desired output:

_code          _sum(_quantity)
004              37.85
045              35.67
043              25.97

Thank you for your help.
Watch Question
Data Engineer
This problem has been solved!
Unlock 2 Answers and 8 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE