DEV Community

Cover image for SQL CASE Statement Explained with Real-World Examples
DbVisualizer
DbVisualizer

Posted on

SQL CASE Statement Explained with Real-World Examples

Conditional logic is everywhere—from setting discounts to classifying users. SQL has a native way to handle this with the CASE statement.

In this quick guide, you’ll explore how to use CASE to implement branching logic in your queries. We’ll cover its two forms, show real-world use cases, and explain where and when to use it for best results.

Using SQL CASE in Practice

Simple comparison:

CASE grade
  WHEN 'A' THEN 'Excellent'
  WHEN 'B' THEN 'Good'
  ELSE 'Needs Work'
END
Enter fullscreen mode Exit fullscreen mode

Conditional logic for prices:

CASE
  WHEN category = 'Shoes' THEN price * 0.85
  WHEN category = 'Gifts' AND price <= 50 THEN price * 0.5
  ELSE price
END
Enter fullscreen mode Exit fullscreen mode

Employee bonus example:

CASE
  WHEN role = 'Manager' AND years >= 5 THEN salary * 0.1
  WHEN role = 'Developer' AND years >= 3 THEN salary * 0.08
  ELSE 0
END
Enter fullscreen mode Exit fullscreen mode

Used in WHERE clause:

WHERE
  CASE
    WHEN status = 'active' THEN 1
    ELSE 0
  END = 1
Enter fullscreen mode Exit fullscreen mode

FAQ

How does the CASE statement work in SQL?

It evaluates conditions in order and returns the result of the first match.

Can I use CASE in ORDER BY?

Yes. You can use it for sorting rows based on dynamic logic.

Is ELSE required?

No. But without it, unmatched conditions return NULL.

Can I nest CASE statements?

Yes. Nesting allows more complex logic handling in a single query.

Conclusion

The SQL CASE statement is powerful for applying conditional logic in queries—no external scripts or procedural code needed. It’s supported in all major databases and flexible enough for many use cases.

Read the full guide SQL CASE Statement: Definitive Guide.

Deploy Infra Like a Game: Spacelift Webinar

Deploy Infra Like a Game: Spacelift Webinar

Join Spacelift on Aug 6 and learn how to build a self-service portal for deploying infrastructure, inspired by Minecraft servers. Discover Blueprints, OpenTofu, and drift remediation.

Join the Webinar

Top comments (0)

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server 🏁

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

👋 Kindness is contagious

Explore this practical breakdown on DEV’s open platform, where developers from every background come together to push boundaries. No matter your experience, your viewpoint enriches the conversation.

Dropping a simple “thank you” or question in the comments goes a long way in supporting authors—your feedback helps ideas evolve.

At DEV, shared discovery drives progress and builds lasting bonds. If this post resonated, a quick nod of appreciation can make all the difference.

Okay