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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T22:09:08+00:00 2026-06-01T22:09:08+00:00

I have two tables (listed only fields important for the question): t_groups INT groupId

  • 0

I have two tables (listed only fields important for the question):

t_groups

  • INT groupId PRIMARY
  • VARCHAR(255) grname

t_goods

  • INT goodId PRIMARY
  • INT groupId
  • INT price
  • VARCHAR(255) name

Now I need a query, which selects group names and name of the cheapest good in each group. Tried doing it this way:

SELECT gr.groupId, grname, g.name
FROM t_groups AS gr
LEFT JOIN (SELECT * FROM t_goods ORDER BY PRICE ASC LIMIT 1) AS g
ON g.groupId = gr.groupId

but it doesn’t work — returns NULLs in g.name field. It could be easily explained:

SELECT within JOIN statement selects cheapest good first, and then tries to “filter it” by groupId. Obviously, it’ll only work for the group cheapest good belongs to.

How do I solve the task?

  • 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-06-01T22:09:09+00:00Added an answer on June 1, 2026 at 10:09 pm

    Why your query does not work

    SELECT gr.groupId, grname, g.name
    FROM t_groups AS gr
    LEFT JOIN (SELECT * FROM t_goods ORDER BY PRICE ASC LIMIT 1) AS g
    ON g.groupId = gr.groupId
    

    The inner query selects the absolutely cheapest good (irrespective of group) in your database. Therefore, when you LEFT JOIN the groups to this result set, only the group which actually includes the universally cheapest good has a matching row (that group should get the g.name column filled properly). However, due to the way LEFT JOIN works all other groups will get NULL as the value of all columns in g.

    The correct solution

    First, you need to select the cheapest price in each group. This is easy:

    SELECT groupId, MIN(price) AS minPrice FROM t_goods GROUP BY (groupId)
    

    However the cheapest price is not useful without the associated goodId. The problem is that it’s not meaningful to write something like:

    /* does not make sense, although MySql has historically allowed it */
    SELECT goodId, groupId, MIN(price) AS minPrice FROM t_goods GROUP BY (groupId)
    

    The reason is that you cannot select a non-grouped column (i.e. goodId) unless you wrap it in an aggregate function (such as MIN): we don’t know which goodId you want from among those that share the same groupId.

    The correct, portable way to get the goodId of the cheapest goods in each group is

    SELECT goodId, temp.groupId, temp.minPrice
    FROM (SELECT groupId, MIN(price) AS minPrice FROM t_goods GROUP BY groupId) temp
          JOIN t_goods ON temp.groupId = t_goods.groupId AND temp.minPrice = t_goods.price)
    

    The above query first finds out the cheapest price per group, and then joins to the goods table again to find the goodIds of the goods having that price inside that group.

    Important: if multiple goods have an equal cheapest price in a group, this query will return all of them. If you only want one result per group you have to specify the tiebreaker, for example:

    SELECT MIN(goodId), temp.groupId, MIN(temp.minPrice)
    FROM (SELECT groupId, MIN(price) AS minPrice FROM t_goods GROUP BY groupId) temp
          JOIN t_goods ON temp.groupId = t_goods.groupId AND temp.minPrice = t_goods.price)
    GROUP BY temp.groupId
    

    With this query in hand, you can then find the name and price of the single cheapest good in each group (lowest goodId will be used as tiebreaker):

    SELECT groupId, grname, gd.name, t3.minPrice
    FROM t_groups AS gr
    LEFT JOIN (SELECT MIN(goodId) AS goodId, t1.groupId, MIN(t1.minPrice) AS minPrice
               FROM (SELECT groupId, MIN(price) AS minPrice FROM t_goods GROUP BY groupId) t1
                     JOIN t_goods ON t1.groupId = t_goods.groupId AND t1.minPrice = t_goods.price
               ) t2
    ) t3 ON gr.groupId = t3.groupId
    LEFT JOIN t_goods gd ON t3.goodId = gd.goodId
    

    This final query performs two joins at its “outer” level:

    • joins groups with the “goodId and cheapest price for each group” table to get the goodId and cheapest price
    • then joins with the goods table to get the name of the good with this goodId

    It will produce only one good per group, even if multiple goods are tied for cheapest.

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

Sidebar

Related Questions

i have two tables both are related with primary-foreign key ralation and i have
I have two tables, the first has a primary key that is an identity,
If I have two tables as listed below, The first which shows the Raw
I have two tables Settlement and Violations.SettlementID is primary Key in Settlements table and
Here's my simple SQL question... I have two tables: Books ------------------------------------------------------- | book_id |
i have two tables products and reviews each product has several reviews linked by
I have two tables : teachers (teacher_id,teacher_name) courses (teacher_id,course_id) And I need to display
I have two tables on a webform. On a button click I want to
I have two tables: Videos -------------- VideoID VideoGroupID CreatorUserID and VideoTags -------------- VideoID TagID
I have two tables, one for routes and one for airports. Routes contains just

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.