Case statement in SQL Server

The SQL Server CASE statement is a conditional statement that returns a single value based on the evaluation of a statement. CASE expressions can be used in SQL anywhere an expression can be used like SELECT list, WHERE clause, HAVING clause, IN list, DELETE and UPDATE statements. CASE statement can also be nested. This provides a lot of pliability for evaluating multiple expressions.

SQL Case statement can be used in 2 forms:

  1. SQL CASE statement with simple expression to compare and get results.
  2. SQL CASE statement with search or comparison expression to get results.
  1. SQL CASE statement with simple expression to compare and get results

    A simple CASE expression operates by comparing the first expression to the expression in each WHEN clause for equivalency.

    Syntax

    CASE expression
        WHEN expression1 THEN expression1
        [[WHEN expression2 THEN expression2] [...]]
        [ELSE expression N]
    END
    

    Example

    SELECT CASE [Section]
        WHEN 1 THEN 'One'
        WHEN 2 THEN 'Two'
        WHEN 3 THEN 'Three'
        WHEN 4 THEN 'Four'
        WHEN 5 THEN 'Five'
        ELSE 'Greater than Five'
    END as [Sec_Word] FROM [Table_Test]
    
  2. SQL CASE statement with search or comparison expression to get results

    A search or comparison CASE expression allows comparison operators, and the use of AND and/or OR between each Boolean expression.

    Syntax

    CASE
        WHEN Boolean_expression1 THEN expression1
        [[WHEN Boolean_expression2 THEN expression2] [...]]
        [ELSE expression N]
    END
    

    Example

    SELECT CASE
        WHEN [Section] < 5 THEN 'Less than Five'
        WHEN [Section] = 5 THEN 'Equal to Five'
        WHEN [Section] > 5 THEN 'Greater than Five'
    END as [Sec_Word] FROM [Table_Test]
    

Hope you will find this post helpful. For any suggestion or query please feel free to post your valuable comments. Thanks.

Introduction to JOINs in SQL Server

The nature of relational database design shows that we will have related data that is stored in different tables. To retrieve data from two or more tables we use JOINS.

The JOIN clause tells the database how the data in the two tables is related so that it can return a correct representation of the related data.

Before entering into JOINs concept in SQL Server first design two tables in database, which I will be using to give you a better understanding of JOINs.

Table1Table2

In this blog I am going to discuss about two JOIN type:

  1. INNER JOIN
  2. OUTER JOIN

INNER JOIN

The INNER JOIN keyword return rows when there is at least one match in both tables.

inner-join

Example of Inner Join in SQL Server

SELECT * FROM Table1 t1
INNER JOIN Table2 t2
ON t1.ID = t2.ID

Output of Inner Join in SQL Server
Inner-Join

OUTER JOIN

There are three types of OUTER JOIN’s in SQL Server

  • LEFT OUTER JOIN
  • RIGHT OUTER JOIN
  • FULL OUTER JOIN

LEFT OUTER JOIN

LEFT OUTER JOIN in SQL Server returns all the rows from the left table in conjunction with the matching rows from the right table. If there are no columns matching in the right table, it returns NULL values.

left-join

Example of Left Outer Join in SQL Server

SELECT * FROM Table1 t1
LEFT OUTER JOIN Table2 t2
ON t1.ID = t2.ID

Output of Left Outer Join in SQL Server
Left-Outer-Join

RIGHT OUTER JOIN

RIGHT OUTER JOIN returns all the rows from the right table in conjunction with the matching rows from the left table. If there are no columns matching in the left table, it returns NULL values.

right-join

Example of Right Outer Join in SQL Server

SELECT * FROM Table1 t1
RIGHT OUTER JOIN Table2 t2
ON t1.ID = t2.ID

Output of Right Outer Join in SQL Server
RIGHT-OUTER-JOIN

FULL OUTER JOIN

FULL OUTER JOIN returns rows from either table when the conditions are met and returns null value when there is no match.

outer-join

Example of Full Outer Join in SQL Server

SELECT * FROM Table1 t1
FULL OUTER JOIN Table2 t2
ON t1.ID = t2.ID

Output of Full Outer Join in SQL Server
FULL-OUTER-JOIN