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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T09:57:52+00:00 2026-05-16T09:57:52+00:00

I have a correlated subquery that will return a list of quantities, but I

  • 0

I have a correlated subquery that will return a list of quantities, but I need the highest quantity, and only the highest. So I tried to introduce an order by and a LIMIT of 1 to achieve this, but MySQL throws an error stating it doesn’t yet support limits in subqueries. Any thoughts on how to work around this?

SELECT Product.Name, ProductOption.Name, a.Qty, a.Price, SheetSize.UpgradeCost,
        FinishType.Name, FinishOption.Name, FinishTierPrice.Qty, FinishTierPrice.Price
FROM `Product`
    JOIN `ProductOption`
        ON Product.idProduct = ProductOption.Product_idProduct
    JOIN `ProductOptionTier` AS a
        ON a.ProductOption_idProductOption = ProductOption.idProductOption
    JOIN `PaperSize`
        ON PaperSize.idPaperSize = ProductOption.PaperSize_idPaperSize
    JOIN `SheetSize`
        ON SheetSize.PaperSize_idPaperSize = PaperSize.idPaperSize
    JOIN `FinishOption`
        ON FinishOption.Product_idProduct = Product.idProduct
    JOIN `FinishType`
        ON FinishType.idFinishType = FinishOption.Finishtype_idFinishType
    JOIN `FinishTierPrice`
        ON FinishTierPrice.FinishOption_idFinishOption = FinishOption.idFinishOption
WHERE Product.idProduct = 1
    AND FinishTierPrice.idFinishTierPrice IN (SELECT FinishTierPrice.idFinishTierPrice
                                              FROM `FinishTierPrice` 
                                              WHERE FinishTierPrice.Qty <= a.Qty
                                              ORDER BY a.Qty DESC
                                              LIMIT 1)
  • 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-16T09:57:53+00:00Added an answer on May 16, 2026 at 9:57 am

    This is a variation of the greatest-n-per-group problem that comes up frequently.

    You want the single row form FinishTierPrice (call it p1), matching the FinishOption and with the greatest Qty, but still less than or equal to the Qty of the ProductOptionTier.

    One way to do this is to try to match a second row (p2) from FinishTierPrice that would have the same FinishOption and a greater Qty. If no such row exists (use an outer join and test that it’s NULL), then the row found by p1 is the greatest.

    SELECT Product.Name, ProductOption.Name, a.Qty, a.Price, SheetSize.UpgradeCost,
            FinishType.Name, FinishOption.Name, FinishTierPrice.Qty, FinishTierPrice.Price
    FROM `Product`
        JOIN `ProductOption`
            ON Product.idProduct = ProductOption.Product_idProduct
        JOIN `ProductOptionTier` AS a
            ON a.ProductOption_idProductOption = ProductOption.idProductOption
        JOIN `PaperSize`
            ON PaperSize.idPaperSize = ProductOption.PaperSize_idPaperSize
        JOIN `SheetSize`
            ON SheetSize.PaperSize_idPaperSize = PaperSize.idPaperSize
        JOIN `FinishOption`
            ON FinishOption.Product_idProduct = Product.idProduct
        JOIN `FinishType`
            ON FinishType.idFinishType = FinishOption.Finishtype_idFinishType
        JOIN `FinishTierPrice` AS p1
            ON p1.FinishOption_idFinishOption = FinishOption.idFinishOption
            AND p1.Qty <= a.Qty
        LEFT OUTER JOIN `FinishTierPrice` AS p2
            ON p2.FinishOption_idFinishOption = FinishOption.idFinishOption
            AND p2.Qty <= a.Qty AND (p2.Qty > p1.Qty OR p2.Qty = p1.Qty 
                AND p2.idFinishTierPrice > p1.idFinishTierPrice)
    WHERE Product.idProduct = 1
        AND p2.idFinishTierPrice IS NULL
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a query that is currently using a correlated subquery to return the
I have a correlated subquery in an update that used to work just fine
I have a table that has a date, item, and quantity. I need a
I have a query where I need a correlated subquery inside a join. I
I have the correlated subquery below. Each id (147,148,149) in table1 has many records
this is correlated to a question I asked earlier (question) I have a list
I have a R script I'm running now that is currently using 3 correlated
I have two disconnected sql servers that have to have correlated queries run between
Having difficulty articulating this correlated subquery. I have two tables fictitious tables, foo and
I have two tables that were built for two disparate systems. I have records

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.