The CASE command is employed to generate various outputs based on different conditions. The provided SQL evaluates multiple conditions and returns a value when a specified condition is satisfied.
| SELECT OrderID, Quantity, CASE WHEN Quantity > 30 THEN ‘The quantity is greater than 30’ WHEN Quantity = 30 THEN ‘The quantity is 30’ ELSE ‘The quantity is under 30’ END FROM OrderDetails; |
The following SQL statement arranges customers by their city. However, if the city field is NULL, it will instead order them by country.
| SELECT CustomerName, City, Country FROM Customers ORDER BY (CASE WHEN City IS NULL THEN Country ELSE City END); |