CASE Expression
Basic Syntax The CASE expression comes in two types: Simple CASE and Searched CASE. Simple CASE The Simple CASE expression compares an expression to a set of possible values. It returns a result when it finds a match. Syntax: CASE expression WHEN value1 THEN result1 WHEN value2 THEN result2 … [ELSE default_result] END Example: SELECT employee_id, department_id, CASE department_id WHEN 10 THEN ‘HR’ WHEN 20 THEN ‘IT’ WHEN 30 THEN ‘Sales’ ELSE ‘Other’ END AS department_name FROM employees; Explanation: This example converts the department_id into a readable department name. Searched CASE The Searched CASE expression evaluates a set of Boolean expressions. It returns a result for the first expression that evaluates to TRUE. Syntax: CASE WHEN condition1 THEN result1 WHEN condition2 THEN result2 … [ELSE default_result] END Example: SELECT employee_id, salary, CASE WHEN salary < 3000 THEN ‘Low’ WHEN salary BETWEEN 3000 AND 6000 THEN ‘Medium’ ELSE ‘High’ END AS salary_range FROM employees; Explanation: This example classifies the salary into different ranges based on its value. Usage Examples and Applications Creating Computed Columns You can use CASE to create new columns with computed values based on conditions. Example: SELECT employee_id, first_name, last_name, salary, CASE WHEN salary < 5000 THEN ‘Below Average’ WHEN salary BETWEEN 5000 AND 10000 THEN ‘Average’ ELSE ‘Above Average’ END AS salary_category FROM employees; Explanation: Adds a salary_category column that classifies salaries into categories. Handling NULL Values You can use CASE to handle NULL values and provide default results. Example: SELECT employee_id, manager_id, CASE WHEN manager_id IS NULL THEN ‘No Manager’ ELSE ‘Has Manager’ END AS manager_status FROM employees; Explanation: Provides a status indicating whether an employee has a manager or not. Conditional Aggregation CASE can be used in aggregation functions to perform conditional aggregation. Example: SELECT department_id, COUNT(CASE WHEN salary > 5000 THEN 1 END) AS high_salary_count FROM employees GROUP BY department_id; Explanation: Counts the number of employees with a salary greater than 5000 per department. Best Practices Avoid Complex Nested CASE Statements While nested CASE statements are possible, they can become difficult to read and maintain. Consider breaking complex logic into simpler steps or using temporary tables. Example: SELECT employee_id, CASE WHEN department_id = 10 THEN ‘HR’ WHEN department_id = 20 THEN ‘IT’ ELSE ‘Other’ END AS department_name, CASE WHEN salary < 3000 THEN ‘Low’ WHEN salary BETWEEN 3000 AND 6000 THEN ‘Medium’ ELSE ‘High’ END AS salary_range FROM employees; Explanation: This combines multiple CASE expressions for different computations in a single query. Use ELSE Clause Wisely Always include an ELSE clause to handle unexpected values and ensure the CASE expression always returns a result. Example: SELECT employee_id, CASE department_id WHEN 10 THEN ‘HR’ WHEN 20 THEN ‘IT’ ELSE ‘Unknown Department’ END AS department_name FROM employees; Explanation: Provides a default value ‘Unknown Department’ if department_id does not match any known values. Test Your CASE Logic Ensure that your CASE expressions are well-tested to avoid logical errors. For complex conditions, test each condition separately to verify the correctness of results. Compatibility and Performance Considerations Database Compatibility: The CASE expression is supported by most SQL databases, including Oracle, SQL Server, MySQL, PostgreSQL, and SQLite. Performance: The performance impact of CASE is generally minimal, but if used excessively in large datasets, it may affect query performance. Ensure that your conditions are optimized and use indexes where applicable. Summary The CASE expression is a versatile tool in SQL that enables you to implement conditional logic directly within your queries. By understanding its two forms (Simple and Searched CASE) and applying best practices, you can efficiently manipulate and analyze your data.
CASE Expression Lire la suite »