SQLforGeeks
  • Home
  • SQL
  • SQL Tutorial
  • SQL Syntax
  • Services
  • Product
    🏋️ GyManage 📱 More to come
Contact
  1. SQL
  2. CASE statement

Text copied!

« Previous
Next »

CASE statement

June 9, 2023, 7 p.m. under SQL

  • Hi! You should check SUBSTRING function post first.

    In SQL, CASE statement is used to perform different actions or return different values based on the specified conditions. It is very similar to the 'if-else' conditional structure.

    The CASE statement examines each condition a sequential manner and performs the related action or returns the specified value when a condition is met.

    The syntax of the CASE statement generally looks like this :
    CASE
        WHEN condition1 THEN result_expression1
        WHEN condition2 THEN result_expression2
        WHEN condition3 THEN result_expression3
        ...
        ELSE result_expressionN
    END
            

    • 'CASE' keyword indicates that the case statement body has started.

    • 'END' keyword indicates that the case statement body has ended.

    • After the 'WHEN' keyword, specify the condition that you want to evaluate.

    • After the 'THEN' keyword, specify the desired result to be executed when the condition evaluates to true.

    • After the 'ELSE' keyword, specify result that denotes the result to be executed if none of the previous conditions evaluate to true. 'ELSE' keyword is an optional, If it is not specified and none of the previous conditions evaluate to true then the result is considered as NULL.

    Here's an example of how you might use the CASE statement :

    1. Let's assume we have a table named "[Employees]".

    CASE statement

    2. Let's assume you want to categorize employees based on their salary. "High" for salaries greater than 3000(inclusive) and "Low" for salaries less than 3000.

    3. Run below SQL statement :

    SELECT Employee_Id, Employee_Name, Salary,
        CASE
            WHEN Salary >= 3000 THEN 'High'
            WHEN Salary <  3000 THEN 'Low'
            ELSE NULL
        END AS Salary_Category
    FROM [Employees];
            

    4. Above statement evaluates employee salaries from [Employees] table and assigns salary categories based on the specified conditions. Assigned an alias name of "Salary_Category" to this result.

    CASE statement

    The CASE statement is a powerful tool in SQL for decision making, simplifying the inclusion of conditional logic into queries and data manipulation operations.

    Frequently Asked Questions :

    What is CASE statement in SQL?

    The CASE statement in SQL is a conditional expression that evaluates multiple conditions and returns a value based on the first condition that is true.

    What are the 5 SQL statements?

    The 5 SQL statements are SELECT, INSERT, UPDATE, DELETE, and CREATE.

    How do you write a case condition?

    To write a case condition in SQL, you use the syntax:
    
    CASE
        WHEN condition1 THEN result1
        WHEN condition2 THEN result2
        ...
        ELSE default_result
    END

    How do I run a query in CASE statement in SQL?

    To run a query using a CASE statement in SQL, you include the CASE expression within the SELECT statement, specifying conditions and their corresponding results as needed.
    Thank You! You should check GETDATE function post next.
    « Previous
    Next »
    RELATED :

    What is SQL

    What is T-SQL

    Difference between DBMS and Data Warehouse

    Download SQL Server

    Install SQL Server

    Download SQL Server Management Studio SSMS

    SQL Server Management Studio

    SQL Database

    Download database

    Restore database

    Backup database

    Attach database

    Detach database

    Create database

    Delete database

    Rename database

    Select database

    Database offline

    Database online

    SQL Commands

    SQL Tables

    Create table

    Truncate table

    Delete table

    Rename table

    Select table

    Alter table

    SQL Data Types

    SQL Comments

    SQL Constraints

    SQL Joins

    SQL inner join

    SQL left join

    SQL right join

    SQL full join

    SQL cross join

    SQL self join

    INSERT INTO SELECT statement

    INSERT INTO statement

    SQL Clauses

    SELECT clause

    FROM clause

    WHERE clause

    GROUP BY clause

    HAVING clause

    ORDER BY clause

    JOIN clause

    UNION clause

    UNION ALL clause

    TOP clause

    DISTINCT clause

    SQL Operators

    SQL Arithmetic operators

    SQL Comparison operators

    SQL Logical operators

    UNION operator

    UNION ALL operator

    INTERSECT operator

    EXCEPT operator

    LIKE operator

    NOT LIKE operator

    IN operator

    NOT IN operator

    IS NULL operator

    IS NOT NULL operator

    EXISTS operator

    NOT EXISTS operator

    BETWEEN operator

    NOT BETWEEN operator

    SQL Functions

    SQL Built-In functions

    CHARINDEX function

    DATEADD function

    CONCAT function

    LEN function

    REPLACE function

    SUBSTRING function

    GETDATE function

    DATEPART function

    DATEDIFF function

    CAST function

    TRY_CAST function

    CONVERT function

    TRY_CONVERT function

    ISNULL function

    NULLIF function

    COALESCE function

    SQL Window functions

    ROW_NUMBER function

    RANK function

    DENSE_RANK function

    IIF function

    CHOOSE function

    SQL Store Procedure

    Store Procedure vs. Function

    SQL Subquery

    SQL Aliases

    Temp table

    SQL Error Handling

    SQL Variables

    SQL Views

    SQL Merge

    SQL CTE

    SQL WITH TIES

    Define Transaction in DBMS

    ACID properties in DBMS

    Types of Triggers in DBMS


    • Have Some Questions?
    logo

    Elevate your data experience with SQL excellence

    Quick Links
    •  Home
    •  SQL Tutorial
    •  SQL Syntax
    •  Our Services
    Our Services
    • Web Development
    • BI Development
    • Data Warehousing
    • Data Integration ETL


    Follow Us

    GST Registered: XXAXXXXXXXZX
    Mumbai, Maharashtra, India

    © 2024 Copyright | All Rights Reserved | Legal