• Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
MYTAT
  • Home
  • Jobs
  • Courses
  • Certifications
  • Companies
  • Online IDE
  • Login
  • Signup
SQL
  • Introduction To SQL
  • SQL Syntax
  • SQL SELECT Statement
  • SQL SELECT DISTINCT Statement
  • SQL WHERE Clause
  • SQL AND, OR And NOT Operators
  • SQL ORDER BY Keyword
  • SQL INSERT INTO Statement
  • SQL NULL Values
  • SQL UPDATE Statement
  • SQL DELETE Statement
  • SQL TOP, LIMIT, FETCH FIRST Or ROWNUM Clause
  • SQL MIN() And MAX() Functions
  • SQL COUNT(), AVG() And SUM() Functions
  • SQL LIKE Operator
  • SQL Wildcards
  • SQL IN Operator
  • SQL BETWEEN Operator
  • SQL Aliases
  • SQL Joins
  • SQL INNER JOIN Keyword
  • SQL LEFT JOIN Keyword
  • SQL RIGHT JOIN Keyword
  • SQL FULL OUTER JOIN Keyword
  • SQL Self Join
  • SQL UNION Operator
  • SQL GROUP BY Statement
  • SQL HAVING Clause
  • SQL EXISTS Operator
  • SQL ANY And ALL Operators
  • SQL SELECT INTO Statement
  • SQL INSERT INTO SELECT Statement
  • SQL CASE Statement
  • SQL NULL Functions
  • SQL Stored Procedures For SQL Server
  • SQL Comments
  • SQL Operators
  • SQL CREATE DATABASE Statement
  • SQL DROP DATABASE Statement
  • SQL BACKUP DATABASE For SQL Server
  • SQL CREATE TABLE Statement
  • SQL DROP TABLE Statement
  • SQL ALTER TABLE Statement
  • SQL Constraints
  • SQL NOT NULL Constraint
  • SQL UNIQUE Constraint
  • SQL PRIMARY KEY Constraint
  • SQL FOREIGN KEY Constraint
  • ALTER TABLE Orders DROP CONSTRAINT FK_PersonOrder;
  • SQL DEFAULT Constraint
  • SQL CREATE INDEX Statement
  • SQL AUTO INCREMENT Field
  • SQL Working With Dates
  • SQL Views
  • SQL Injection
  • SQL Hosting
  • SQL Data Types For MySQL, SQL Server, And MS Access
  • Home
  • Courses
  • SQL
  • SQL CASE Statement

SQL CASE Statement

Previous Next

The SQL CASE Statement

The CASE statement goes through conditions and returns a value when the first condition is met (like an if-then-else statement). So, once a condition is true, it will stop reading and return the result. If no conditions are true, it returns the value in the ELSE clause.

If there is no ELSE part and no conditions are true, it returns NULL.

CASE Syntax

CASE
    WHEN condition1 THEN result1
    WHEN condition2 THEN result2
    WHEN conditionN THEN resultN
    ELSE result
END;



Practice Excercise Practice now

Demo Database

Below is a selection from the "OrderDetails" table in the Northwind sample database:

OrderDetailID OrderID ProductID Quantity
1 10248 11 12
2 10248 42 10
3 10248 72 5
4 10249 14 9
5 10249 51 40



Practice Excercise Practice now

SQL CASE Examples

The following SQL goes through conditions and returns a value when the first condition is met:

Example

SELECT OrderID, Quantity,
CASE
    WHEN Quantity > 30 THEN 'The quantity is greater than 30'
    WHEN Quantity = 30 THEN 'The quantity is 30'
    ELSE 'The quantity is under 30'
END AS QuantityText
FROM OrderDetails;

The following SQL will order the customers by City. However, if City is NULL, then order by Country:

Example

SELECT CustomerName, City, Country
FROM Customers
ORDER BY
(CASE
    WHEN City IS NULL THEN Country
    ELSE City
END);



Practice Excercise Practice now

Previous Next
COMPANY
  • About us
  • Careers
  • Contact Us
  • In Press
  • People
  • Companies List
Products
  • Features
  • Coding Assessments
  • Psychometric Assessment
  • Aptitude Assessments
  • Tech/Functional Assessments
  • Video Assessment
  • Fluency Assessment
  • Campus
 
  • Learning
  • Campus Recruitment
  • Lateral Recruitment
  • Enterprise
  • Education
  • K 12
  • Government
OTHERS
  • Blog
  • Terms of Services
  • Privacy Policy
  • Refund Policy
  • Mart Category
Partner
  • Partner Login
  • Partner Signup

Copyright © RVR Innovations LLP 2025 | All rights reserved - Mytat.co is the venture of RVR Innovations LLP