
- SQL Tutorial
- SQL - Home
- SQL - Overview
- SQL - RDBMS Concepts
- SQL - Databases
- SQL - Syntax
- SQL - Data Types
- SQL - Operators
- SQL - Expressions
- SQL Database
- SQL - Create Database
- SQL - Drop Database
- SQL - Select Database
- SQL - Rename Database
- SQL - Show Database
- SQL - Backup Database
- SQL Table
- SQL - Create Table
- SQL - Show Tables
- SQL - Rename Table
- SQL - Truncate Table
- SQL - Clone Tables
- SQL - Temporary Tables
- SQL - Alter Tables
- SQL - Drop Table
- SQL - Delete Table
- SQL - Constraints
- SQL Queries
- SQL - Insert Query
- SQL - Select Query
- SQL - Select Into
- SQL - Insert Into Select
- SQL - Update Query
- SQL - Delete Query
- SQL - Sorting Results
- SQL Views
- SQL - Create Views
- SQL - Update Views
- SQL - Drop Views
- SQL - Rename Views
- SQL Operators and Clauses
- SQL - Where Clause
- SQL - Top Clause
- SQL - Distinct Clause
- SQL - Order By Clause
- SQL - Group By Clause
- SQL - Having Clause
- SQL - AND & OR
- SQL - BOOLEAN (BIT) Operator
- SQL - LIKE Operator
- SQL - IN Operator
- SQL - ANY, ALL Operators
- SQL - EXISTS Operator
- SQL - CASE
- SQL - NOT Operator
- SQL - NOT EQUAL
- SQL - IS NULL
- SQL - IS NOT NULL
- SQL - NOT NULL
- SQL - BETWEEN Operator
- SQL - UNION Operator
- SQL - UNION vs UNION ALL
- SQL - INTERSECT Operator
- SQL - EXCEPT Operator
- SQL - Aliases
- SQL Joins
- SQL - Using Joins
- SQL - Inner Join
- SQL - Left Join
- SQL - Right Join
- SQL - Cross Join
- SQL - Full Join
- SQL - Self Join
- SQL - Delete Join
- SQL - Update Join
- SQL - Left Join vs Right Join
- SQL - Union vs Join
- SQL Keys
- SQL - Unique Key
- SQL - Primary Key
- SQL - Foreign Key
- SQL - Composite Key
- SQL - Alternate Key
- SQL Indexes
- SQL - Indexes
- SQL - Create Index
- SQL - Drop Index
- SQL - Show Indexes
- SQL - Unique Index
- SQL - Clustered Index
- SQL - Non-Clustered Index
- Advanced SQL
- SQL - Wildcards
- SQL - Comments
- SQL - Injection
- SQL - Hosting
- SQL - Min & Max
- SQL - Null Functions
- SQL - Check Constraint
- SQL - Default Constraint
- SQL - Stored Procedures
- SQL - NULL Values
- SQL - Transactions
- SQL - Sub Queries
- SQL - Handling Duplicates
- SQL - Using Sequences
- SQL - Auto Increment
- SQL - Date & Time
- SQL - Cursors
- SQL - Common Table Expression
- SQL - Group By vs Order By
- SQL - IN vs EXISTS
- SQL - Database Tuning
- SQL Function Reference
- SQL - Date Functions
- SQL - String Functions
- SQL - Aggregate Functions
- SQL - Numeric Functions
- SQL - Text & Image Functions
- SQL - Statistical Functions
- SQL - Logical Functions
- SQL - Cursor Functions
- SQL - JSON Functions
- SQL - Conversion Functions
- SQL - Datatype Functions
- SQL Useful Resources
- SQL - Questions and Answers
- SQL - Quick Guide
- SQL - Useful Functions
- SQL - Useful Resources
- SQL - Discussion
Example of SQL query describing the conditional processing
Problem: Write a SQL query to display 2 columns. First column should have ORDER_ID, the second column should give the value as YES/NO for free shipping based on ORDER_TOTAL > 500.
Solution
The query to display ORDER_ID and free shipping result based on the ORDER_TOTAL criteria can be written as below.
Example
SELECT ORDER_ID, CASE WHEN ORDER_TOTAL > 500 THEN ‘YES’ ELSE ‘NO’ AS FREE_SHIPPING END FROM ORDERS
We will use CASE expressions through which we can implement a logic to check the ORDER_TOTAL. If the ORDER_TOTAL is greater than 500 then we will get ‘YES’ for the free shipping else, we will get ‘NO’. The query will return two columns ORDER_ID and FREE_SHIPPING.
For example, if we have below ORDERS DB2 table.
ORDER_ID | ORDER_TOTAL |
Z22345 | 342 |
Z62998 | 543 |
Z56990 | 431 |
Z56902 | 6743 |
Z99781 | 443 |
Z56112 | 889 |
Then the SQL query with CASE WHEN expression will return the following result.
ORDER_ID | FREE_SHIPPING |
Z22345 | NO |
Z62998 | YES |
Z56990 | NO |
Z56902 | YES |
Z99781 | NO |
Z56112 | YES |
- Related Articles
- Example of SQL query describing COUNT function and GROUP BY
- What are the steps of SQL Query processing in DBMS?
- SQL query describing usage of SUM aggregate function and GROUP-BY with HAVING
- What is the SQL query describing usage of MAX aggregate function and GROUP-BY with HAVING?
- Explain SQL describing COUNT aggregate and CURRENT DATE function
- How to handle unresolved conditional branch processing policies in computer architecture?
- Structured Query Language (SQL)
- Difference between SQL(Structured Query Language) and T-SQL(Transact-SQL).
- Image processing/OpenCV image erosion Java Example.
- Image processing/OpenCV image dilation Java Example.
- Best SQL Query Optimization Tools
- SQL Query to Convert Rows to Columns in SQL Server
- What is query processing and optimization in DBMS?
- Implement Conditional MySQL Query in a stored procedure?
- Basic SQL Injection and Mitigation with Example

Advertisements