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

  • Home
  • SEARCH
  • 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 588155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:17:20+00:00 2026-05-13T15:17:20+00:00

I have an SQL question, related to this and this question (but different). Basically

  • 0

I have an SQL question, related to this and this question (but different). Basically I want to know how I can avoid a nested query.

Let’s say I have a huge table of jobs (jobs) executed by a company in their history. These jobs are characterized by year, month, location and the code belonging to the tool used for the job. Additionally I have a table of tools (tools), translating tool codes to tool descriptions and further data about the tool. Now they want a website where they can select year, month, location and tool using a dropdown box, after which the matching jobs will be displayed. I want to fill the last dropdown with only the relevant tools matching the before selection of year, month and location, so I write the following nested query:

SELECT c.tool_code, t.tool_description
FROM (
 SELECT DISTINCT j.tool_code
 FROM jobs AS j
 WHERE j.year = ....
        AND j.month = ....
 AND j.location = ....
) AS c
LEFT JOIN tools as t
ON c.tool_code = t.tool_code
ORDER BY c.tool_code ASC

I resorted to this nested query because it was much faster than performing a JOIN on the complete database and selecting from that. It got my query time down a lot. But as I have recently read that MySQL nested queries should be avoided at all cost, I am wondering whether I am wrong in this approach. Should I rewrite my query differently? And how?

  • 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-13T15:17:20+00:00Added an answer on May 13, 2026 at 3:17 pm

    No, you shouldn’t, your query is fine.

    Just create an index on jobs (year, month, location, tool_code) and tools (tool_code) so that the INDEX FOR GROUP-BY can be used.

    The article your provided describes the subquery predicates (IN (SELECT ...)), not the nested queries (SELECT FROM (SELECT ...)).

    Even with the subqueries, the article is wrong: while MySQL is not able to optimize all subqueries, it deals with IN (SELECT …) predicates just fine.

    I don’t know why the author chose to put DISTINCT here:

    SELECT  id, name, price
    FROM    widgets
    WHERE   id IN
            (
            SELECT  DISTINCT widgetId
            FROM    widgetOrders
            )
    

    and why do they think this will help to improve performance, but given that widgetID is indexed, MySQL will just transform this query:

    SELECT  id, name, price
    FROM    widgets
    WHERE   id IN
            (
            SELECT  widgetId
            FROM    widgetOrders
            )
    

    into an index_subquery

    Essentially, this is just like EXISTS clause: the inner subquery will be executed once per widgets row with the additional predicate added:

    SELECT  NULL
    FROM    widgetOrders
    WHERE   widgetId = widgets.id
    

    and stop on the first match in widgetOrders.

    This query:

    SELECT  DISTINCT w.id,w.name,w.price
    FROM    widgets w
    INNER JOIN
            widgetOrders o
    ON      w.id = o.widgetId
    

    will have to use temporary to get rid of the duplicates and will be much slower.

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

Sidebar

Ask A Question

Stats

  • Questions 354k
  • Answers 354k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Yes, set up a double-click handler on the ListView itself.… May 14, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer You can rewrite the public field to a property: public… May 14, 2026 at 8:20 am
  • Editorial Team
    Editorial Team added an answer Well, FTP creates a new socket for each file transfer.… May 14, 2026 at 8:20 am

Related Questions

Background: I am aware of this SO question about Transactional NTFS (TxF) and this
when i have a many-to.many relation with nhibernate and let nhibernate generate my db
I've written my own custom data layer to persist to a specific file and
I'm using DDD and NHibernate to persist my domain object. In my database every
Scenario: A WCF service receives an XDocument from clients, processes it and inserts a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.