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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T16:32:11+00:00 2026-06-10T16:32:11+00:00

I’m wondering which way to get data from a MySQL database has better performance

  • 0

I’m wondering which way to get data from a MySQL database has better performance characteristics.

Using subqueries within one main query:

SELECT
(SELECT SUM(`number`) FROM `table`) as `number_sum`,
(SELECT MAX(`number`) FROM `table`) as `number_max`,
(SELECT MIN(`number`) FROM `table`) as `number_min`

Or, 3 distinct SELECT statements retrieving the same data.

Thanks in advance!

  • 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-10T16:32:13+00:00Added an answer on June 10, 2026 at 4:32 pm

    Since these three aggregates come from the same table with the same WHERE conditions, you have no need for subselects. All three of the aggregates are operating on the same row grouping (no GROUP BY specified, so one row for the whole table), so they can all exist in the SELECT list directly.

    SELECT
      SUM(number) AS number_sum,
      MAX(number) AS number_max,
      MIN(number) AS number_min
    FROM `table`
    

    If any of the aggregates needs to be based on different conditions you would filter in a WHERE clause, then you will need to either use a subselect for the differing condition, or do a cartesian join. This subselect and the following LEFT JOIN method should be equivalent, performance-wise for aggregates returning only one row:

    SELECT
      /* Unique filtering condition - must be done in a subselect */
      (SELECT SUM(number) FROM `table` WHERE `somecolumn` = `somevalue`) AS number_sum,
      MAX(number) AS number_max,
      MIN(number) AS number_min
    FROM `table`
    

    Or equivalent to the query above, you can LEFT JOIN against a subquery with no ON clause. This should only be done in situations when you know the subquery will return only one row. Otherwise, you will end up with a cartesian product — as many rows as returned by one side of the join multiplied by the number of rows returned by the other side.

    This is handy if you need to return a few columns with one set of WHERE clause conditions and a few columns with a different set of WHERE conditions, but only one row from each side of the JOIN. In this case, it should be faster to JOIN than to do two subselects with the same WHERE clause.

    This should be faster….

    SELECT
      /* this one has two aggregates sharing a WHERE condition */
      subq.number_sum_filtered,
      subq.number_max_filtered,
      /* ...and two aggregates on the main table with no WHERE clause filtering */
      MAX(`table`.number) AS number_max,
      MIN(`table`.number) AS number_min
    FROM
      `table`
      LEFT JOIN (
        SELECT 
           SUM(number) AS number_sum_filtered,
           MAX(number) AS number_max_filtered
        FROM `table`
        WHERE `somecolumn = `somevalue`
      ) subq /* No ON clause here since there's no common column to join on... */
    

    Than this…

    SELECT
      /* Two different subselects each over the same filtered set */
      (SELECT SUM(number) FROM `table` WHERE `somecolumn` = `somevalue`) AS number_sum_filtered,
      (SELECT MAX(number) FROM `table` WHERE `somecolumn` = `somevalue`) AS number_max_filtered,
      MAX(`table`.number) AS number_max,
      MIN(`table`.number) AS number_min
    FROM
      `table`
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a view passing on information from a database: def serve_article(request, id): served_article
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
Basically, what I'm trying to create is a page of div tags, each has

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.