Geek Slack

My SQL Tutorial
    About Lesson


    MySQL CASE Statement


    MySQL CASE Statement

    The CASE statement in MySQL is used to perform conditional logic within a query. It allows you to evaluate a series of conditions and return a value based on the first condition that is true.

    Example:

    SELECT employee_name,
        CASE
            WHEN salary >= 10000 THEN 'High'
            WHEN salary >= 5000 THEN 'Medium'
            ELSE 'Low'
        END AS salary_category
    FROM employees;

    This query categorizes employees based on their salary into ‘High’, ‘Medium’, or ‘Low’ categories using the CASE statement.

    The CASE statement is versatile and can be used in various scenarios to perform conditional transformations or aggregations in MySQL queries.