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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:42:50+00:00 2026-05-20T23:42:50+00:00

Imagine an auction (ebay auction, for example). You create an auction, set the start

  • 0

Imagine an auction (ebay auction, for example). You create an auction, set the start bidding value, let’s say, 5 dollars. This gets stored as a minimal bid value to the auctions table.At this point, the current bid value of this auction is 5 dollars.

Now, if someone bids to your auction, let’s say, 10 dollars, this gets stored to the bids table.At this point, the current bid value of this auction is 10 dollars.

Now let’s imagine you want to retrieve 5 cheapest auctions. You will write a query like this:

SELECT
`auction_id`,
`auction_startPrice`,
MAX(bids.bid_price) as `bid_price`
FROM
`auctions`
LEFT JOIN `bids` ON `auctions`.`auction_id`=`bids`.`bid_belongs_to_auction`
GROUP BY `auction_id`
LIMIT 5

Pretty simple, and it works! But now you need to add an ORDER BY clause to the query. The problem is, however, that we want to ORDER BY either by auctions.auction_startPrice or by bid_price, depending on whichever of this is higher, as explained in the first paragraphs.

Can this be understood? I know how to do this using 2 queries, but I am hoping it can be done with 1 query.

Thanks!

EDIT: Just a further explanation to help you imagine the problem. If I set ORDER BY auction_startPrice ASC, then I will get 5 auctions with their lowest initial bid price, but what if there are already bids placed on those auctions? Then their current lowest price is equal to those bids, NOT to the start price, therefore my query is wrong.

  • 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-20T23:42:50+00:00Added an answer on May 20, 2026 at 11:42 pm
    SELECT
      `auction_id`,
      `auction_startPrice`,
      `bid_price`
    FROM
    (
        SELECT
            `auction_id`,
            `auction_startPrice`,
            MAX(bids.bid_price) as `bid_price`,
            IF(MAX(bids.bid_price)>`auction_startPrice`,
               MAX(bids.bid_price),
               `auction_startPrice`) higherPrice
        FROM
        `auctions`
        LEFT JOIN `bids` ON `auctions`.`auction_id`=`bids`.`bid_belongs_to_auction`
        GROUP BY `auction_id`
    ) X
    order by higherPrice desc
    LIMIT 5;
    

    Note:

    1. In the inner query, an extra column is created, named ‘higherPrice’
    2. The IF function compares the MAX(bid_price) column against the startprice, and only if the Max-bid is not null (implicitly required in comparison) and greater than start price, then the Max-bid becomes the value in the higherPrice column. Otherwise, it will contain the start price.
    3. The outer query merely makes use of the columns from the inner query, ordering by the higherPrice
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to follow this example but I can't understand this part: Imagine
imagine this html on a page <div id=hpl_content_wrap> <p class=foobar>this is one word and
Imagine I want to draw a pyramid made of triangles. -Should I create a
Imagine a very big gwt project of application. This is just a some form
Imagine to have a table defined as CREATE TABLE [dbo].[Price]( [ID] [int] NOT NULL,
Let's imagine we have an (x,y) plane where a robot can move. Now we
I need the most simple auction script you can imagine. I dont need categories,
Imagine a script is running in these 2 sets of conditions: live action, set
Imagine this controller: class exampleController{ def action1 = {} def action2 = {} def
imagine that you had a task that finished in, say 10 seconds. Now, after

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.