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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:47:58+00:00 2026-05-23T01:47:58+00:00

Suppose we have a table Users that has only one column UserId which is

  • 0

Suppose we have a table Users that has only one column UserId which is the primary key.

We have a second table Events that has three columns, EventId, UserId and Status. A Status is a BOOL NOT NULL. UserId is indexed but not unique.

The table Status has the foreign key constraint UserId on Users.UserId.

Now I want to query all the UserIds for which there is no row in Events such that Event.Status = TRUE. Is there a way to do this with JOIN? (One statement only preferred)

For example:

Users
--------
1
2
3

Events
--------
EventId   UserId  Status
1         1       FALSE
2         1       TRUE
3         2       FALSE
4         2       FALSE

Then the query I’m looking for should return:

UserId
-------
2
3

Thanks!

  • 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-23T01:47:58+00:00Added an answer on May 23, 2026 at 1:47 am

    Surprisingly, a subselect is a reasonably efficient way to do this on MySQL. So:

    SELECT UserId
    FROM   Users
    WHERE  UserId NOT IN (
           SELECT DISTINCT UserID
           FROM   Events
           WHERE  Status = TRUE
           )
    

    Note that I’m using DISTINCT there, which may not be a perfect fit for the referenced article. Mind you, you may be fine with DISTINCT, or with leaving it off (but I don’t know whether that would result in a problematic interim data set, or if MySQL is smart about it).

    Or you can do the LEFT JOIN / IS NULL version, which doesn’t have the DISTINCT issue:

    SELECT    Users.UserId
    FROM      Users
    LEFT JOIN Events
              ON Events.UserId = Users.UserId AND Events.Status = TRUE
    WHERE     Events.UserId IS NULL
    

    See the link above for a discussion, but MySQL provides basically the same performance in both cases (whereas using NOT EXISTS rather than NOT IN would be markedly less efficient).

    I’d try both (or all three, if you try both with and without DISTINCT on the first one) and see what performs best with your real-life data.

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

Sidebar

Related Questions

Suppose I have a table called Companies that has a DepartmentID column. There's also
Suppose I have a database table that has a timedate column of the last
Suppose there is a table A which has a column AccessRights which is multivalued(
Suppose I have a table Books with columns Book_ID (Primary Key), ISBN (Unique), and
suppose that I have this RDBM table ( Entity-attribute-value_model ): col1: entityID col2: attributeName
Suppose that I have 2 tables: [User] - UserID - Username [Task] - TaskID
Suppose that I have users, tasks and each task belongs to a batch. Each
Suppose that we have 3 related table in this manner in MySQL: Table 1:
Suppose we have a table A: itemid mark 1 5 2 3 and table
Suppose I have a table . Now, I'm interested in Getting Useful Data Easily.

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.