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

推荐订阅源

Google DeepMind News
Google DeepMind News
博客园 - 司徒正美
WordPress大学
WordPress大学
爱范儿
爱范儿
小众软件
小众软件
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
罗磊的独立博客
博客园_首页
V
V2EX
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
T
Tailwind CSS Blog
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog
MyScale Blog
MyScale Blog
IT之家
IT之家
H
Help Net Security
Blog — PlanetScale
Blog — PlanetScale
Microsoft Security Blog
Microsoft Security Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Recent Announcements
Recent Announcements
F
Fortinet All Blogs
The GitHub Blog
The GitHub Blog
Y
Y Combinator 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 #2: The Match Pattern
Baldwin Apps · 2026-06-03 · via DEV Community

Choosing between exact matches and pattern matches

SQL Pattern Series #2 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:

  • The difference between exact matching and pattern matching
  • When to use =
  • When to use LIKE
  • Why a small operator change can change the meaning of a query

Most SQL developers run into this distinction eventually.

= and LIKE solve different problems.

= checks for an exact value.

LIKE checks whether a value fits a pattern.

That difference matters when the question changes from:

Is this exactly Admin?

to:

Does this start with Admin?


The image above summarizes the core idea:

  • = asks whether a value is exactly equal.
  • LIKE asks whether a value matches a pattern.
  • Both are useful, but they answer different questions.

Exact Match Example

Use = when the value must match exactly.

SELECT *
FROM Users
WHERE Role = 'Admin';

Enter fullscreen mode Exit fullscreen mode

This query asks:

Is the role exactly Admin?

It will match:

Admin

Enter fullscreen mode Exit fullscreen mode

But it will not match:

AdminAssistant
Administrator
SuperAdmin

Enter fullscreen mode Exit fullscreen mode

That is the point.

The = operator is precise.


Pattern Match

Use LIKE when the value may vary but still follows a recognizable pattern.

SELECT *
FROM Users
WHERE Role LIKE 'Admin%';

Enter fullscreen mode Exit fullscreen mode

This query asks:

Does the role start with Admin?

It may match values like:

Admin
AdminAssistant
Administrator

Enter fullscreen mode Exit fullscreen mode

The % wildcard means:

Any number of characters may appear here.

So Admin% means:

Starts with Admin.


The Match Pattern

The Match Pattern is about choosing the right kind of comparison for the question being asked.

Sometimes you need precision.

Sometimes you need flexibility.

The pattern is simple:

  • Use = for exact matches
  • Use LIKE for pattern-based matches

But the effect can be significant.

A query that is too strict may miss valid rows.

A query that is too broad may return rows you did not intend to include.


Why This Pattern Matters

This distinction shows up in many real-world queries.

For example:

  • Find one exact status
  • Find names that start with a prefix
  • Find emails from a specific domain
  • Find product codes with a shared pattern
  • Find log messages containing a phrase

Each of these questions requires a different kind of match.


Common Examples

Exact status match

SELECT *
FROM Orders
WHERE Status = 'Pending';

Enter fullscreen mode Exit fullscreen mode

This asks for orders where the status is exactly Pending.


Prefix match

SELECT *
FROM Users
WHERE Username LIKE 'test%';

Enter fullscreen mode Exit fullscreen mode

This asks for usernames that start with test.


Contains match

SELECT *
FROM Products
WHERE ProductName LIKE '%keyboard%';

Enter fullscreen mode Exit fullscreen mode

This asks for product names that contain keyboard anywhere in the value.


Suffix match

SELECT *
FROM Customers
WHERE Email LIKE '%@example.com';

Enter fullscreen mode Exit fullscreen mode

This asks for email addresses that end with @example.com.


A Note on Performance

Pattern matching can be powerful, but it can also affect performance.

For example:

WHERE Username LIKE 'Admin%'

Enter fullscreen mode Exit fullscreen mode

may be easier for a database to optimize because the pattern has a fixed beginning.

But this:

WHERE Username LIKE '%Admin%'

Enter fullscreen mode Exit fullscreen mode

can be more expensive because the database may need to search inside the value rather than starting from the beginning.

The exact behavior depends on:

  • the database system
  • indexes
  • collation settings
  • data size
  • query plan

As always, validate assumptions with real execution plans and real workloads.


When I Reach for This Pattern

I typically use = when:

  • I need one exact value
  • The column contains controlled values
  • The comparison should be strict
  • The query should not include partial matches

I typically use LIKE when:

  • I need prefix matching
  • I need suffix matching
  • I need contains matching
  • The data is text-based and variable
  • I am intentionally searching for a pattern

Key Takeaway

Small operator difference.

Very different behavior.

When writing SQL, ask:

Am I looking for an exact value?

or:

Am I looking for a pattern?

That question determines whether the query should use = or LIKE.

The better you understand the match you need, the easier it becomes to write queries that return the right rows.


SQL Pattern Series

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

Previous article:

  • SQL Pattern Series #1: The Presence Pattern

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.