Healthcare Data Analyst | Supporting Healthcare, NGOs, Health Tech & Public Health Organizations with Data-Driven Insights | SQL • Power BI • Excel | RN🇳🇬
NOT EXISTS
NOT EXISTS uses the same thinking approach, but asks the opposite question:
"Can I find NO matching row for this current value?"
If even a single matching row exists → NOT EXISTS is FALSE → exclude it ❌
If no matching row exists → NOT EXISTS is TRUE → keep it ✅
So the mental model becomes:
EXISTS → Can I find at least one matching row?
NOT EXISTS → Can I find no matching row?
Understanding this thinking approach will make it easier for us as we go through these lessons I've learned. 😊☺️
SQL: IN, NOT IN, EXISTS, NOT EXISTS
These four SQL concepts are commonly used when we need to filter data based on whether a value or a related row meets a particular condition.
They are especially useful when we're trying to answer questions such as:
Which customers are in this particular group?
Which customers are NOT in this group?
Does this customer have at least one transaction that meets a particular condition?
Although IN, NOT IN, EXISTS, and NOT EXISTS can sometimes be used to solve similar problems, they make you think about the problem in different ways.
EXISTS
This is where the way I think about the problem changes.
Instead of thinking about a list of values, I think about rows.
I ask:
"Does a matching row exist for this current value?"
For example, imagine I'm looking at one customer's ID.
I then ask:
"Can I find at least one row belonging to this customer that satisfies the condition I'm looking for?"
If I can find one matching row then EXISTS is TRUE.
And here's the important part:
I don't care whether I find 1 matching row or 100 matching rows.
EXISTS only needs to know:
"Does at least one matching row exist?"
@PeterFatd1 If you publish the project in power BI services. It will give you a visual while screen recording!
That's a very nice visualization BTW! Congratulations! 👏🎉
Last week, one SQL interview question forced me to revisit EXISTS, NOT EXISTS, IN, and NOT IN concept in SQL.
And when I say “revisit,” I mean really revisit. 😂
I had learned these concepts before, but while trying to solve that problem, I realized I didn’t understand them as deeply as I thought I did.
So I went back and studied them properly.
A few days later, I encountered another problem involving the same concepts.
This time, solving it was MUCH easier.
That experience reminded me of something:
Learning a concept is different from being able to apply it when solving a problem.
I’ll be sharing some of the things I learned about these 4 SQL concepts next.
Hopefully, I can compress the lessons enough to fit into a few posts. 😂
I’ve decided to start a 6-month challenge where I’ll make a post every day on this app.
I’ll be sharing my journey, the things I’m learning, projects I’m working on, and maybe even teaching/talking about some of the things I’ve learnt along the way.
So, here’s a little introduction about me 👇🏽
My name is Winifred. I’m a nurse currently trying to transition into healthcare analytics.
I’m also 6 months into my NYSC service year, serving Nigeria (against my wish though 🤣😂).
I’m hoping that by the end of this challenge, I’ll have built a community here where we can all learn, share, support each other, and grow together.
Day 1/180 🚀
Let’s see where this takes us. 🤝🏽
@ezekiel_aleke For me, the first step in any data-cleaning process should be understanding the business problem. Knowing what the data is meant to answer helps guide all the cleaning decisions that follow.
For the past few weeks, I’ve been trying to solve at least 4 SQL problems every week.
And Lately, I’ve been working through the free SQL problems on LeetCode, and mehn 😫 some of their questions deal with me mentally 😂
Somedays, I find myself staring at a question for more than an hour trying to figure out my way around it.
But one habit I have cultivated is that whenever I eventually solve a problem, I go back and study any SQL concept that was an obstacle while solving it.
So, the next time you’re stuck on a SQL problem, instead of just looking for the answer, try going back to the concept you’re struggling with and learn it again. It might make the next problem much easier.
Honestly, this approach has been helpful.