MySQL Simple CASE Expression with Examples

Joshua Otwell
Level Up Coding
Published in
4 min readApr 28, 2021

--

Programming logic is foundational in any application or piece of software. Without it, software wouldn’t really do much of anything. Everything happens off of choice. In the end, some truthy or falsy value is what makes stuff work. For IF/THEN/ELSE logic in standard SQL, there is the CASE expression. There are 2 variations of the CASE Expression: Simple and Searched. In this post, I cover the Simple MySQL CASE expression with example queries.

Image by Nika Akin from Pixabay

OS and DB used:

  • Linux Mint 20 “Ulyana”
  • MySQL 8.0.23

Self-Promotion:

If you enjoy the content written here, by all means, share this blog and your favorite post(s) with others who may benefit from or like it as well. Since coffee is my favorite drink, you can even buy me one if you would like!

How does the MySQL Simple CASE Expression Work?

A Simple CASE expression is essentially an equality test. Specify a column or expression immediately following the CASE keyword, and then a series of WHEN/THEN clauses. In each WHEN/THEN clause, a specified value is tested against the column or expression that follows CASE. The THEN clause portion maintains what value is returned when there is a match between the listed value in the WHEN clause and the Simple CASE

--

--

SQL | PHP | Photography. Sign-up for my free developer newsletter, OpenLampTech, here: openlamptech.substack.com