O'Reilly logo

Joe Celko's SQL for Smarties, 4th Edition by Joe Celko

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

18

CASE Expressions

The CASE expression is probably the most useful addition in SQL-92. This is a quick overview of how to use the expression, but you will find other tricks spread throughout the book.

• The reason it is so important is that it works with any data type.

• It allows the programmer to avoid procedural code by replacing IF-THEN-ELSE control flow with CASE expression inside the query. This helps the optimizer.

• It makes SQL statements equivalent to primitive recursive functions. Only a math major cares about this, but it is important.

You can look up what that means in a book on the theory of computation, but it is a nice mathematical property that guarantees certain kinds of problems can be solved.

18.1 The CASE Expression

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required