Which sql tool considers one or more conditions, then returns a value as soon as a condition is met?

SQL Server CASE statement is equivalent to the IF-THEN statement in Excel.

The CASE statement is used to implement the logic where you want to set the value of one column depending upon the values in other columns.

The SQL Server CASE Statement consists of at least one pair of WHEN and THEN statements. The WHEN statement specifies the condition to be tested. The THEN statement specifies the action if the WHEN condition returns TRUE.

The ELSE statement is optional and executes when none of the WHEN conditions return true. The CASE statement ends with an END keyword.

In this article, we will take a look at a number of different examples of the CASE statement. But before we do that, we’ll create some dummy data to work with.

Creating dummy data

Execute the following script to create the dummy data:

CREATEDatabaseShowRoom;

GO

USEShowRoom;

CREATETABLECars

(

    idINT,

    name VARCHAR(50)NOTNULL,

    companyVARCHAR(50)NOTNULL,

    powerINTNOT NULL,

    colorVARCHAR(50)NOTNULL,

    modelINTNOTNULL,

    condition VARCHAR(50)NOTNULL

)

The script above has created a dummy database called ShowRoom with one Table in it called Cars. The Cars table has seven columns: id, name, company, power, color, model, and condition.

Now let’s insert some dummy data into the Cars table. Execute the following script:

USEShowRoom

INSERTINTOCars

VALUES

(1,'Corrolla','Toyota',1800,'red',1995, 'X'),

(2,'City','Honda',1500,'black',2015,'X'),

(3,'C200', 'Mercedez',2000,'white',1992,'X'),

(4,'Vitz','Toyota',1300, 'blue',2007,'X'),

(5,'Baleno','Suzuki',1500,'white',2012,'X'),

(6, 'C500','Mercedez',5000,'grey',1994,'X'),

(7,'800','BMW',8000, 'blue',2016,'X'),

(8,'Mustang','Ford',5000,'red',1997,'X'),

(9, '208','Peugeot',5400,'black',1999,'X'),

(10,'Prius','Toyota',3200, 'red',2003,'X')

Let’s check how our dataset looks, execute the following script:

The output looks like this:

Which sql tool considers one or more conditions, then returns a value as soon as a condition is met?

You can see that the condition column contains an X in each row at the moment. We will set the value of the condition column, depending on the model column, using the CASE statement so that you can see clearly what is going on.

The syntax of the CASE statement is pretty straight forward:

SELECTcolumn1,

             column2,

               CASEWHENCONDITIONTHEN'Value1'

               ELSE'Value2'ENDAScolumnX

  FROMCars

The CASE statement has to be included inside the SELECT Statement. It starts with the CASE keyword followed by the WHEN keyword and then the CONDITION.

The condition can be any valid SQL Server expression which returns a boolean value. For instance, the condition can be model > 2000, the THEN clause is used after the CONDITION. If the CONDITION returns true the value that follows the THEN clause is stored in columnX. Else, the value after the ELSE clause, will also be stored in columnX. The SQL Server CASE statement ends with the END clause.

CASE statement examples

Let’s now see the CASE statement in action.

In a previous section, we created a table named Cars inside the ShowRoom database.

The condition column had the value X for all rows. We will use the SQL Server CASE statement to set the value of the condition column to “New” if the model column has a value greater than 2000, otherwise the value for the condition column will be set to “Old”.

Look at the following script:

SELECTname,

          model,

          CASEWHENmodel>2000THEN'New'

             ELSE'Old'ENDAScondition

  FROMCars

The above script displays the name, model and condition columns from the Cars table. The output of the script above looks like this:

Which sql tool considers one or more conditions, then returns a value as soon as a condition is met?

You can see that the value of X in the condition column has been replaced by “New” and “Old” depending upon the model of the car.

Multiple conditions in CASE statement

You can evaluate multiple conditions in the CASE statement.

Let’s write a SQL Server CASE statement which sets the value of the condition column to “New” if the value in the model column is greater than 2010, to ‘Average’ if the value in the model column is greater than 2000, and to ‘Old’ if the value in the model column is greater than 1990.

Look at the following script:

SELECTname,

       model,

       CASEWHENmodel>2010THEN'New'

      WHEN model>2000THEN'Average'

      WHENmodel>1990THEN'Old'

                ELSE 'Old'ENDAScondition

  FROMCars

The output of the script above looks like this:

Which sql tool considers one or more conditions, then returns a value as soon as a condition is met?

In the script above, we assigned three different values to the condition column depending on the value in the model column.

However, in the above script, the conditions are overlapping as. the model with a value greater than 2010 also has a value greater than 2000 and 1990. A better way to implement multiple conditions is to use logical operators like AND, OR, NOT, etc. Look at the following script:

SELECTname,

       model,

       CASEWHENmodel>2010THEN'New'

      WHEN model>2000ANDmodel<2010THEN'Average'

      WHENmodel>1990ANDmodel <2000THEN'Old'

            ELSE'Old'ENDAScondition

  FROMCars

The output of the script above looks like this:

Which sql tool considers one or more conditions, then returns a value as soon as a condition is met?

We can also evaluate multiple conditions from different columns using the SQL Server CASE statement. In the following example, we will assign the value of “New White” to the condition column where the model is greater than 2010 and the color is white. Look at the following script:

SELECTname,

     color,

       model,

       CASEWHENmodel>2010AND color='white'THEN'New White'

      WHENmodel>2010THEN'New'

      WHENmodel >2000ANDmodel<2010THEN'Average'

      WHENmodel>1990ANDmodel<2000 THEN'Old'

            ELSE'Old'ENDAScondition

  FROMCars

The output looks like this:

Which sql tool considers one or more conditions, then returns a value as soon as a condition is met?

You can see from row 5 that since the color is white and the model is greater than 2010, value for the condition column has been set to “New White” in the output.

Using GROUP BY with SQL Server CASE statement

The CASE statement can also be used in conjunction with the GROUP BY statement in order to apply aggregate functions.

For example, if we want to count the number of new (model number greater than 2000) and old (model number less than 2000) vehicles, we can use the GROUP BY clause with the CASE statement as follows:

SELECT

       CASEWHENmodel>2000THEN'New'

            ELSE'Old'END AScondition,

      COUNT(1)AScount

  FROMCars

  GROUPBYCASEWHENmodel >2000THEN'New'

            ELSE'Old'END

In the script above we use the COUNT aggregate function with the CASE statement.

The SQL Server CASE statement sets the value of the condition column to “New” or “Old”. Inside the GROUP BY clause, we specify that the corresponding count for “New” is incremented by 1, whenever a model value of greater than 2000 is encountered. The Else section means that we increase the count for “Old” by 1 if the value of the model is 2000 or less. The output of the script above looks like this:

Which sql tool considers one or more conditions, then returns a value as soon as a condition is met?

Since our dataset has five vehicles with a value for ‘model’ of greater than 2000, you can see a 5 in the count column for “New” vehicles. Similarly, we had 5 old vehicles and hence we can see 5 for the count column of “Old” vehicles.

Similarly, we can GROUP BY more than two values. Look at the following script:

SELECT

       CASEWHENmodel>2010THEN'New'

      WHENmodel>2000 THEN'Average'

      WHENmodel>1990THEN'Old'

            ELSE'Old'ENDAS condition,

      COUNT(1)AScount

  FROMCars

  GROUPBYCASEWHENmodel> 2010THEN'New'

      WHENmodel>2000THEN'Average'

      WHENmodel>1990 THEN'Old'

            ELSE'Old'END

In the script above, we grouped the data into three categories: “New”, “Average” and “Old”.

The output looks like this:

Which sql tool considers one or more conditions, then returns a value as soon as a condition is met?

You can see the count for “New”, “Average” and “Old” condition cars.

Conclusion

The CASE statement comes in handy when you want to implement IF-THEN logic in SQL Server. In this article, we saw what CASE statement is along with its syntax. We also saw different examples of CASE statement along with its usage with the GROUP BY clause.

Other great articles from Ben

  • Understanding SQL Server query plan cache
  • What is the difference between Clustered and Non-Clustered Indexes in SQL Server?
  • How to use window functions
  • Querying data using SQL Server CASE statement

  • Author
  • Recent Posts

Which sql tool considers one or more conditions, then returns a value as soon as a condition is met?

Ben Richardson runs Acuity Training a leading provider of SQL training the UK. It offers a full range of SQL training from introductory courses through to advanced administration and data warehouse training – see here for more details. Acuity has offices in London and Guildford, Surrey. He also blogs occasionally on Acuity’s blog

View all posts by Ben Richardson

Which sql tool considers one or more conditions, then returns a value as soon as a condition is met?

How do I add an IF condition in SQL query?

MySQL IF() Function.
Return "YES" if the condition is TRUE, or "NO" if the condition is FALSE: ... .
Return 5 if the condition is TRUE, or 10 if the condition is FALSE: ... .
Test whether two strings are the same and return "YES" if they are, or "NO" if not:.

Can we use if condition in SQL query?

Any T-SQL statement can be executed conditionally using IF… ELSE. If the condition evaluates to True, then T-SQL statements followed by IF condition in SQL server will be executed. If the condition evaluates to False, then T-SQL statements followed by ELSE keyword will be executed.

How do you check for multiple values in SQL?

The IN operator allows you to specify multiple values in a WHERE clause. The IN operator is a shorthand for multiple OR conditions.

What are the conditions in a query?

A query condition is an expression you apply to a column that limits the results based on values you tell the query to search for. When you create a query condition, you identify the column and then refine the search of the column's data by choosing a specific property or content that satisfies the condition.