Skip to content
Advertisement

How to retrieve a list of results that are NOT in another list with SQL

I have a normalized SQL database. The relevant schema looks like this:

I need to get a list of users that have not been sent an email today for a specific category. Based on my understanding, I need to perform a LEFT JOIN. In an attempt to do this, I’ve tried the following:

I’m using subqueries in this question because my actual query is more complex. However, I have verified that each subquery returns the results I expected. In other words, the first subquery returns a User list. The second subquery returns an EmailLog list of emails sent today.

I’m stuck on the part where I retrieve the users that have NOT been sent an email today though. It’s like I’m trying to prove a negative. What am I missing?

Thank you!

Advertisement

Answer

I would use not exists:

I’m not sure why you are using the UTC date. I would expect the local date/time to make more sense, but this is from your sample code.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement