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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T12:24:05+00:00 2026-06-15T12:24:05+00:00

I was just asking myself, if this makes sense: I have a query that

  • 0

I was just asking myself, if this makes sense:

I have a query that joins with a couple of tables, groups the result (to use GROUP_CONCAT on some columns), and then filters out some results with HAVING, before sorting them with ORDER BY

The LIMIT, is the last statement here – so basically, the limit only takes effect when HAVING is going through the results, right?

I was asking myself – what if I already Limit the FROM part, by doing a SUBSELECT with the same LIMIT? But it’s not that simple, it gets a bit more complicated:

It’s hard to come up with an example that makes sense, I only have a bad one:

A car has always 4 wheels, every wheel has an own table row. So it doesnt make sense to search past the first 4 wheels – I know that, MySQL probably doesnt. So having LIMIT 4 after the HAVING, would mean that mysql searches trough the whole table (listing millions of wheels) even if it could have stopped at the 4th.

Basically, my question is – does a SUBSELECT make sense in query below?

without SUBSELECT:

SELECT    *
FROM      carwheel
LEFT JOIN ... ON ...
LEFT JOIN ... ON ...
LEFT JOIN ... ON ...
WHERE     ...
GROUP BY  ...
HAVING    ...
ORDER BY  ...
LIMIT     4

with SUBSELECT:

SELECT   *
FROM     (
             SELECT *
             FROM   carwheel
             WHERE  ... -- (first, simple filter)
             LIMIT  4
         ) AS carwheel
LEFT JOIN ... ON ...
LEFT JOIN ... ON ...
LEFT JOIN ... ON ...
WHERE     ...
GROUP BY  ...
HAVING    ...
ORDER BY  ...
LIMIT     4 -- (does it make sense, to do it twice?)

I’ve read that even adding LIMIT 1 when you expect a single row, makes a lot of sense since MySQL stops to look for more rows after finding the first one. That’s the whole reason I’m thinking about the subselect, since MySQL will look trough the whole carwheel table, instead of stopping at the 4th, if the LIMIT takes only effect after HAVING, right?

  • 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-15T12:24:06+00:00Added an answer on June 15, 2026 at 12:24 pm

    You get different results, because the order is undefined when you limit in the from subselect. You get four rows in both cases, but in one case you might get rows 7, 14, 23, 8 and in another case you might get rows 1, 2, 88, 17. So it makes sense to limit the result set in order to minimize traffic, for example, but keep in mind that, depending on where you limit, you get different rows.

    Edit:

    After your clarification, it makes sense to pre-filter the rows in your from clause, because then the join has less rows to process. The second limit is useful too, because with the left joins you can get more than four rows in your result set.

    But to be sure, which one is better, do an explain select ... with both queries. Then, you can compare both selects and see which one is more efficient.

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

Sidebar

Related Questions

The result is still a day before, I'm just asking myself why, because the
that is a question I have been asking myself for a while. Giving a
Just asking if I have the right understanding System.out.println(); System is the package out
It seems to me that using unanchored namespaces is just asking for trouble later
I hope this question is not 'controversial' - I'm just basically asking - has
Just out of curiosity, asking this Like the expression one below a = (condition)
Before asking this i did A LOT of searching on the net. I just
[edit]: I'm terribly sorry for my timing on asking this question. I've just discovered
I was just asking myself a little question, and I'm not sure if I
I have inherited code that looks like this: Stored Procedure UpdateSomeStuff Update Table1 Set

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.