SQL Case

SQL Case

Certainly! SQL CASE is a conditional statement used in SQL queries to perform different actions based on specified conditions. It allows you to define multiple conditions and their corresponding actions within a single query. The general syntax for the SQL CASE statement is as follows:

sql
CASE
    WHEN condition_1 THEN result_1
    WHEN condition_2 THEN result_2
    ...
    ELSE result_n
END

Here’s a breakdown of the different parts:

  • CASE: Begins the CASE statement.
  • WHEN condition_1 THEN result_1: Checks if condition_1 is true, and if so, returns result_1.
  • WHEN condition_2 THEN result_2: Checks if condition_2 is true, and if so, returns result_2.
  • ELSE result_n: Optional. If none of the conditions are true, returns result_n.
  • END: Ends the CASE statement.

Here’s an example that demonstrates the usage of SQL CASE:

sql
SELECT product_name, 
       unit_price,
       CASE
           WHEN unit_price > 50 THEN 'Expensive'
           WHEN unit_price > 20 THEN 'Moderate'
           ELSE 'Cheap'
       END AS price_category
FROM products;

In this example, the query selects the product_name and unit_price columns from the products table. The CASE statement checks the unit_price for each product and assigns a price_category based on the conditions specified. If the unit_price is greater than 50, it will be categorized as ‘Expensive’. If it’s between 20 and 50, it will be categorized as ‘Moderate’. Otherwise, it will be categorized as ‘Cheap’. The result of the CASE statement is aliased as price_category.

The SQL CASE statement is a powerful tool for performing conditional operations in SQL queries, allowing you to customize the output based on specific conditions.

Leave a Reply

Your email address will not be published. Required fields are marked *

GLOTRU Footer
Popular Features
Popular Services/

Website Development & Design

App Development & Design

Graphic Design

Digital Marketing

SEO (Search Engine Optimization)

SMM (Social Media Marketing)

Cyber Security

Company

GLOTRU Founder & CEO : __Azam

Registared : Trade,MSME,etc

Board of Director

Team

About Us

Contact Us

Privacy Policy

Return & Refund Policy

Abuse Policy

Copyright Policy

Cookie Policy

Terms & Conditions

Universal Terms of Service

Disclaimer

Legal

Sponsorships

Investor

Press Releases

Our Investments

Brands

Newsroom

Business

...

_

Digital Millennium Copyright Act
DMCA.com Protection Status

_

Content similarity detection
Protected by Copyscape

_

***ANTI-PIRACY WARNING***

...................................................................................

Follow Us :

...................................................................................

SECURE SERVER : [Legal] [Privacy Policy] [Universal Terms of Service] [Do not sell my personal information]

SITE HOSTED : GLOTRU SECURE SERVER Asian Data Centre [You can host your site][Click Here]

SSL : Server Type : [Cloudflare] Certificate Issued By : [Let's Encrypt] Signature Algorithm : [ECDSA with SHA-384]

SITE BUILD SOFTWARE : Content Management System (CMS) Softwere

_

_

_