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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T20:40:55+00:00 2026-05-31T20:40:55+00:00

I have a MySQL table structured somewhat like this: type name value ===================== 1

  • 0

I have a MySQL table structured somewhat like this:

type    name    value
=====================
1       item1    1
1       item2    20
2       item3    0
3       item4    5
3       item5    2
3       item6    50

I need to write a query that returns the lowest valued item of each type, sorted by the value. So the result should be:

type    name    value
=====================
2       item3    0
1       item1    1
3       item5    2

I can get this to work, but it’s looking really, really ugly right now. Is there an elegant way to do this?

Thanks so much!

  • 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-31T20:40:56+00:00Added an answer on May 31, 2026 at 8:40 pm

    This is called a “greatest-n-per-group” query (under that tag on SO you will find many similar questions). (I know you want the “lowest-n-per-group”, but it’s the same problem).

    Usually, you would be able to do:

    SELECT type, MIN(value)
    FROM mytable
    GROUP BY type
    

    But this won’t work if you also want the name corresponding to the MIN(value).

    To retrieve the minimum value per type and also the corresponding row, you join your table to itself within type (the GROUP BY) variable, and with a sorting condition on value (the MIN) variable:

    SELECT t1.type, t1.name, t1.value
    FROM mytable t1
    LEFT JOIN mytable t2 ON t1.type = t2.type AND t1.value > t2.value
    WHERE t2.value IS NULL
    

    Note :

    • we LEFT JOIN mytable to itself, restricting the join such that all the types are the same. This will produce a table with every combination of values for each type.
    • we add a condition to the LEFT JOIN to restrict the combinations of values such that t1.value > t2.value. So now we have a table with every combination of values within each type, but t1’s is bigger than t2’s
    • since this is a LEFT JOIN, if there is a t1.value for which there is no smaller t2.value, the corresponding t2 columns will be NULL. But this is precisely the smallest t1.value for that type!
    • Add a WHERE t2.value IS NULL condition in to pick out exactly these rows.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a messages table that is structured somewhat like this: from | to
Let's say I have a mySQL table whose structure is like this: mysql> select
This is the MySQL table structure that I have: itemID (BIGINT) userID (BIGINT) wasAdded
I have mysql database structure like below: CREATE TABLE test ( id int(11) NOT
I have two tables, structured like so: table A: A_ID varchar(32) PRIMARY KEY field1
I have a mysql table jobs . This is the basic structure of jobs
I have a MySQL table with just a single text type column with lots
I have a DB structured like this: CATEGORIES > SUBCATEGORIES > PRODUCTS In just
i have a mysql table structure : emails( id , name , email )
i have a mysql table structured as per the example below: POSTAL_CODE_ID|PostalCode|City|Province|ProvinceCode|CityType|Latitude|Longitude 7|A0N 2J0|Ramea|Newfoundland|NL|D|48.625599999999999|-58.9758

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.