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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T14:03:33+00:00 2026-05-30T14:03:33+00:00

I’ve been doing some SQL for an exam I have on thursday and I

  • 0

I’ve been doing some SQL for an exam I have on thursday and I have doubts if I’m using the EXISTS statement correctly.

So, here I have a DB with 2 tables

    Machines                Maintenance
    ============            ==============
PK  ID_Machine          PK  ID_Machine  FK
    Name                PK  ID_Task     FK
    Date_bought             Date

So, the query they want me to write says “Show all data from the oldest machine that has not received any maintenance in 2011”

The way I did it is the following:

SELECT M.ID_MACHINE, M.NAME, M.DATE_BOUGHT
FROM MACHINES M
WHERE NOT EXISTS (SELECT MA.*
                  FROM MAINTENANCE MA
                  WHERE MA.ID_MACHINE = M.ID_MACHINE
                  AND YEAR(MA.DATE) = 2011)                  
AND EXISTS (SELECT MIN(M2.DATE_BOUGHT)
              FROM MACHINE M2
              WHERE M2.ID_MACHINE = M.ID_MACHINE)

Is this a correct way to do this query? does it makes sense that I use SELECT MIN() inside a EXISTS statement?

Thanks in advance to everyone!

  • 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-30T14:03:34+00:00Added an answer on May 30, 2026 at 2:03 pm

    When you use exists, it only validates that a piece of data is returned for the filters (joins and where). Often, you will see exists queries with select 1 from…. This is because the actual return values are not used.

    This was a novel idea, and one I had to test out personally. However, as I stated above, the return data is ignored for the most part. It only cares that the join and where filters match and does not care about the MIN, even though it does seem to be a filter in itself. It is more of an aggregation, so the underlying data remains, it seems. The first exists is valid, but the next part does need work. I have updated it below for what I would do.

    SELECT M.ID_MACHINE, M.NAME, M.DATE_BOUGHT
    FROM MACHINES M
    WHERE NOT EXISTS (SELECT 1
                  FROM MAINTENANCE MA
                  WHERE MA.ID_MACHINE = M.ID_MACHINE
                  AND YEAR(MA.DATE) = 2011)                  
        AND M.ID_MACHINE = (SELECT TOP 1 M2.ID_MACHINE
              FROM MACHINE M2
              WHERE M2.ID_MACHINE = M.ID_MACHINE
              ORDER BY M2.DATE_BOUGHT)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a jquery bug and I've been looking for hours now, I can't
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have thousands of HTML files to process using Groovy/Java and I need to
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.