Case Statements
As we build SQL queries and statements, often, we'll need a way to examine a set of conditions and then return a value based on one of those conditions. The MySQL and SQL Server CASE
statement does exactly this. For example, we may need a SELECT
query column that shows different values based on different, specific values or conditions that have been found in a specific database table column. We'll see an illustration of this in the next paragraph. A SQL CASE
statement will step through a list of conditions and then return a result based on the first matching condition it finds. The CASE
statements work in the SELECT
, UPDATE
, DELETE
, WHERE
, and HAVING
clauses, and they operate a lot like simple IF…ELSE
statements. Note that the CASE
statement can't control execution flow in a SQL function or stored procedure. We'll look at functions and stored procedures in more detail later.
We can get basic product information from the Products
table,...