SQL Server Interview Question #56
What is CASE in SQL Server?
SELECT, Filtering, Sorting & Built-in Functions Mid-Level Intermediate
Detailed Explanation
CASE is a conditional expression that returns a value based on one or more conditions. It can be used in SELECT, ORDER BY, aggregates, updates, and other expressions.
SQL Server supports searched CASE, which evaluates Boolean conditions, and simple CASE, which compares one expression against multiple values.
Code Example
SELECT Name,
Price,
CASE
WHEN Price >= 1000 THEN 'Premium'
WHEN Price >= 500 THEN 'Mid Range'
ELSE 'Budget'
END AS PriceCategory
FROM dbo.Products;