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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:05:25+00:00 2026-06-18T04:05:25+00:00

I was wondering how this query works: SELECT empname FROM Employee WHERE not exists

  • 0

I was wondering how this query works:

SELECT empname FROM Employee WHERE not exists (
    SELECT projid FROM Project WHERE not exists (
        SELECT empid, projid FROM Assigned WHERE empid = Employee.empid and projid = Project.projid
    )
)

It is supposed to return names of all employees who are assigned to every project and it does work however I am getting confused as to how/why it works correctly.

Schema is:

Employee(empID INT,empName VARCHAR(100),job VARCHAR(100),deptID INT,salary INT);
Assigned(empID INT,projID INT,role VARCHAR(100));
Project(projID INT,title VARCHAR(100),budget INT,funds INT);

I am new to SQL so a detailed/simple explanation would be appreciated.

  • 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-18T04:05:26+00:00Added an answer on June 18, 2026 at 4:05 am

    When I need to try to understand what’s going on, I look for the inner-most query and work my way outwards. In your case, let’s start with:

    SELECT empid, projid 
    FROM Assigned 
    WHERE empid = Employee.empid and projid = Project.projid
    

    This is matching all records in the Assigned table where the empid and projid are in the previous tables (hence the Employee.empid and Project.projid).

    Assume there are 5 projects in the Projects table and Employee1 is assigned to each. That would return 5 records. Also assume Employee2 is assigned to 1 of those projects, thus returning 1 record.

    Next look at:

    SELECT projid FROM Project WHERE not exists (
            ...
        )
    

    Now this says for those found records in the previous query (Employee1 with 5 projects and Employee2 with 1 project), select any projid from the Project table where there aren’t any matches (not exists) from the previous query. In other words, Employee1 would return no projects from this query but Employee2 would return 4 projects.

    Finally, look at

     SELECT empname FROM Employee WHERE not exists (
            ...
        )
    

    Just as with the 2nd query, for any records found in the previous query (no records to match those employees with all projects such as Employee1 and some records if the employee isn’t assigned to every project such as Employee2), select any employee from the Employee table where there aren’t any matches (again, not exists). In other words, Employee1 would return since no projects were returned from the previous query, and Employee2 would not return, since 1 or more projects were returned from the previous query.

    Hope this helps. Here’s some additional information about EXISTS:

    http://dev.mysql.com/doc/refman/5.0/en/exists-and-not-exists-subqueries.html

    And from that article:

    What kind of store is present in all cities?

    SELECT DISTINCT store_type FROM stores s1   WHERE NOT EXISTS (
        SELECT * FROM cities WHERE NOT EXISTS (
          SELECT * FROM cities_stores
           WHERE cities_stores.city = cities.city AND cities_stores.store_type = stores.store_type));
    

    The last example is a double-nested NOT EXISTS query. That is, it has
    a NOT EXISTS clause within a NOT EXISTS clause. Formally, it answers
    the question “does a city exist with a store that is not in Stores”?
    But it is easier to say that a nested NOT EXISTS answers the question
    “is x TRUE for all y?”

    Good luck.

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

Sidebar

Related Questions

I have this query and it works great (not sure) about performance SELECT ssp.product_id,
From my understanding, IN works like this: $arrayName = array(1, 2, 3); SELECT *
Is there a better way of doing a query like this: SELECT COUNT(*) FROM
I have this query which works... select t.date, sum(gk.d) as d, sum(gk.p) as p,
For example the following query works fine: SELECT * FROM quotes WHERE expires_at <=
3I was just wondering if this type of query is even possible. If so,
I have this query: @listings = Listing.where('city != ?', 'SommerVille') I was wondering if
I was just wondering this the other day. I am not exactly sure how
I am wondering why this is not working please help me. Here is my
I was wondering if this was possible? I'm not familiar with using windows command

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.