惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
WordPress大学
WordPress大学
Apple Machine Learning Research
Apple Machine Learning Research
小众软件
小众软件
雷峰网
雷峰网
博客园 - 聂微东
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 三生石上(FineUI控件)
博客园_首页
博客园 - 司徒正美
博客园 - 叶小钗
IT之家
IT之家
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
博客园 - 【当耐特】
宝玉的分享
宝玉的分享
有赞技术团队
有赞技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
T
Tailwind CSS Blog
V
V2EX
The Cloudflare Blog

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
SQL Pattern Series #1: The Presence Pattern
Baldwin Apps · 2026-05-30 · via DEV Community
Cover image for SQL Pattern Series #1: The Presence Pattern

Baldwin Apps

Thinking in terms of existence instead of lists

SQL Pattern Series #1 of 21

A collection of practical SQL patterns that help developers recognize common solutions to recurring database problems.

What You'll Learn

In this article you'll learn:

  • When EXISTS and IN solve the same problem
  • The difference between set membership and existence
  • Why the underlying mental model matters
  • When I typically reach for EXISTS

Most SQL developers write a query like this at some point:

SELECT c.CustomerID,
       c.CustomerName
FROM Customers c
WHERE c.CustomerID IN (
    SELECT o.CustomerID
    FROM Orders o
);

And it works.

But sometimes it isn't the best way to think about the problem.


The Question Behind the Query

Many SQL problems can be framed in two different ways.

Set Membership

Is this value in a set?

WHERE CustomerID IN (...)

Existence

Does at least one matching row exist?

WHERE EXISTS (...)

Both approaches often return the same result.

But they represent different mental models.


The Presence Pattern

The Presence Pattern is useful when you do not actually care about the values being returned from a related table.

You only care whether a matching row exists.

For example:

  • Customers who have placed an order
  • Users who have logged in
  • Employees assigned to a project
  • Products that have sales

In these cases, the question is often:

Does a related row exist?

rather than:

What values are contained in this list?

The image above summarizes the core idea:

  • EXISTS asks whether at least one matching row exists
  • IN checks whether a value belongs to a set
  • Both often return the same result
  • The difference is usually the mental model

Example Using EXISTS

SELECT c.CustomerID,
       c.CustomerName
FROM Customers c
WHERE EXISTS (
    SELECT 1
    FROM Orders o
    WHERE o.CustomerID = c.CustomerID
);

The subquery is correlated to the outer query.

Conceptually, SQL asks:

For this customer, does at least one matching order exist?

As soon as the answer becomes true, the condition is satisfied.


Why This Pattern Matters

Many SQL developers initially learn syntax.

Over time, they discover that query writing is really about choosing the right mental model.

The Presence Pattern encourages you to think in terms of:

  • existence
  • relationships
  • matching rows

instead of building lists unnecessarily.

That shift often makes queries easier to reason about.


A Note on Performance

Modern database optimizers are extremely sophisticated.

In many systems, IN and EXISTS may be rewritten into similar execution plans.

As a result:

The same result does not necessarily mean the same execution strategy.

And the same syntax does not necessarily mean different performance.

Always validate assumptions with execution plans and real-world testing.

The value of this pattern is primarily conceptual:

  • existence vs. membership
  • relationship vs. list
  • presence vs. values

When I Reach for This Pattern

I typically consider EXISTS when:

  • I only need to know whether related data exists
  • The subquery may return many rows
  • The relationship itself is the focus of the query
  • I want the query to communicate intent clearly

Examples include:

  • customers with orders
  • users with activity
  • products with transactions
  • accounts with associated records

Key Takeaway

Many SQL problems become easier when you ask:

Do I need the values?

or:

Do I simply need to know whether they exist?

That small distinction changes how you think about the query.

And sometimes, changing how you think about the problem is more important than changing the syntax.


SQL Pattern Series

This article is part of the SQL Pattern Series, a collection of practical SQL patterns that help developers recognize common problem-solving approaches found in reporting, analytics, and application development.


SQL Bubble Pop

If you are learning SQL or helping others learn SQL, I created SQL Bubble Pop, a mobile game that teaches SQL concepts through quick, interactive challenges and pattern recognition exercises.

The goal is simple:

Learn SQL by recognizing patterns instead of memorizing syntax.