sql statement case_Learn about the SQL CASE statement

sql statement case

Hey, folks! Hope you all are doing well. In this article, we will be understanding the working of SQL CASE Statement.

Hey guy! Hope all is well. In this article, we will understand the working of SQL CASE statement .



What is SQL CASE statement? ( What is SQL CASE Statement? )

Every programming language, such as Java, Python, C, C++, etc, contains various forms of conditional-statements to validate and choose the actions depending upon the conditions mentioned.

Every programming language such as Java, Python, C, C++, etc. contains various forms of conditional statements to verify and select actions based on the mentioned conditions.

Let us understand this through an example.

Let us understand this with an example.

In a retail store, various customers visit to buy different necessary food items and accessories. The retailer decides that he wants a software that would work on the below condition in an automated manner–

In a retail store, various customers come to buy different essential food items and accessories. The retailer decides that he wants a software that can run in an automated fashion under the following conditions –

“Every customer will be granted a discount of 5% on the bought items if and only if the total cost of the items they are about to buy, exceeds 20$.”

"Each customer will receive a 5% discount on purchases only and if the total cost of the items they are about to purchase exceeds $20."

So, the programmer or the developer would use the Case statement according to the preferred language and set the condition that if the total price exceeds 20$, then apply 5% discount on the bought items.

So a programmer or developer would use a Case statement based on the preferred language and set a condition to apply a 5% discount on the purchased item if the total price exceeds $20.

So, from the above example, you all must have got an idea about Case statement.

So, from the above example, you must all have an understanding of the Case statement.

Yes, you have guessed it right!

Yes, you guessed it!

SQL CASE statement helps us apply and set some conditions on the data and then provide the results/actions based upon the fulfillment of the condition. The CASE statement returns the value or performs the action that is mentioned against the fulfillment of the particular condition/conditions.

The SQL CASE statement helps us to apply and set some conditions on the data and then provide results/actions based on the satisfaction of the conditions. The CASE statement returns the value or performs the mentioned action for the satisfaction of a specific condition.

Now, let us understand the working of SQL CASE statement in the below section.

Now, let us understand how the SQL CASE statement works in the following section.



Syntax of CASE statement

SQL CASE statementreturns a particular value if the first condition is met, else it checks for the next condition and the process continues.

If the first condition is met, SQL CASE statementit will return a specific value, otherwise it will check the next condition and continue the process.

Syntax:

syntax:


CASE
    WHEN condition-1 THEN result-1
    WHEN condition-2 THEN result-2
    .
    .
    .
    WHEN condition-N THEN result-N
    ELSE default-result
END;
  • If no condition is true, the CASE statement returns the value specified in the ELSE portion.

    If no condition is true, the CASE statement returns the value specified in the ELSE section.
  • Moreover, if we do not provide any ELSE condition and if none of the conditions is TRUE, then the CASE statement returns a NULL value.

    Also, if we don't provide any ELSE conditions, and none of the conditions are TRUE, the CASE statement will return a NULL value.


Implementing SQL Case Statement through examples

Here, we will implement the SQL Case statement with various SQL queries and clauses.

Here, we will use various SQL queries and clauses to implement the SQL Case statement.

Let us first create a table using SQL Create query and insert values into the table through SQL Insert query.

Let us first create a table using SQL Create query and then insert values ​​into that table via SQL Insert query .


create table Info(id integer, Cost integer, city varchar(200));
insert into Info(id, Cost,city) values(1, 100,"Pune");
insert into Info(id, Cost,city) values(2, 50, "Satara");
insert into Info(id, Cost,city) values(3, 65,"Pune");
insert into Info(id, Cost,city) values(4, 97,"Mumbai");
insert into Info(id, Cost,city) values(5, 12,"USA");

Thus, we have created a table — ‘Info’ and added values to the columns created as above.

So we create a table "Info" and add the values ​​to the columns created as above.



SQL SELECT query with CASE statement

We execute the SQL SELECT query to display the data values depending upon certain conditions stated in the CASE statement as mentioned in the below piece of code.

We execute an SQL SELECT query to display data values ​​based on certain conditions described in the CASE statement as described in the following code.

Example:

example:


SELECT city, Cost,
CASE
    WHEN Cost < 20 THEN 'Displaying price less than 20.'
    ELSE 'Price rates above 20'
END AS Price_Rate
FROM Info;

As seen above, when we execute the query, it will display the column values of ‘city’ and ‘Cost’. It creates a new column ‘Price_Rate’ to display the results of the conditions met.

As shown above, when we execute the query, it will display the column values ​​of "city" and "Cost". It creates a new column "Price_Rate" to display results that meet the condition.

Output:

output:

Case Statement Example 1
Case Statement Example


Use CASE statement SQL Update query ( SQL Update query with CASE statement )

Now, let us try executing SQL Update query with CASE statement.

Now, let's try to execute SQL Update query using CASE statement .

Example:

example:

In the below example, we have passed an UPDATE query to alter the names of the cities passed in the CASE condition to a specified value.

In the example below, we have passed an UPDATE query to change the name of the city passed in the CASE condition to the specified value.


UPDATE Info 
SET city  = CASE city
 WHEN 'Pune' THEN 'POONA' 
 WHEN 'USA' THEN 'UNITED NAIONS' 
 ELSE  'INDIA'
END;

Output:

output:

Case Statement Example 2
Case Statement Example 2

After having clubbed CASE statement with the SQL queries, now let us try to execute it with some SQL Clauses.

After combining the CASE statement with the SQL query, now let's try to execute it with some SQL clauses.



SQL ORDER BY clause with CASE statement ( SQL ORDER BY clause with CASE statement )

Here, we have executed ORDER BY clause with CASE statement.

Here, we have executed the ORDER BY clause using the CASE statement .

We select all the data values from the table – ‘Info’ using the SELECT query. Further, we add an ORDER BY clause with a CASE statement that states the following conditions.

We use a SELECT query to select all data values ​​- "info" from the table. Additionally, we added an ORDER BY clause with a CASE statement that declares the following conditions.

  • Display the data values in an ascending order of ‘Cost’ only if the city value is ‘Pune’.

    The data values ​​are displayed in ascending order of Cost only if the city value is "Pune".

Example:

example:


Select *
 from Info
 ORDER BY  CASE city
WHEN 'Pune' THEN Cost End;

Output:

output:

Case Statement Example 3
Case Statement Example 3


Conclusion ( Conclusion )

That’s all for this topic. I recommend the readers to implement CASE Statement with GROUP BY clause, HAVING clause, DELETE query, etc.

That's all for this topic. I recommend readers to implement CASE statement using GROUP BY clause , HAVING clause , DELETE query , etc.

Feel free to comment below, in case you come across any doubt.

If you have any questions, please comment below.

For more such posts related to SQL, please do visit SQL JournalDev.

For more such posts related to SQL, visit SQL JournalDev .



References _ _ _

Translated from: https://www.journaldev.com/41782/sql-case-statement

sql statement case

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=324031197&siteId=291194637