Skip to Main Content

Write Great SQL Office Hours

Free tips and training every month! Subscribe for reminders and more from Office Hours. FAQ

Header container

August 22, 2023

13:00 UTC   Start Times Around the World

Subscribe to be notified of changes to sessions and give us feedback!

Having trouble watching the video on this page? Open the video in your browser.


IF/THEN logic with CASE expressions in SQL
Conditional IF-THEN logic is a foundational concept in programming.

But there's no IF statement in SQL!

To do conditional logic in SQL, you use CASE expressions.

Watch the recording to learn how CASE works and use it in

* SELECT & WHERE clauses
* Aggregate and window functions
* PL/SQL code

Highlights include:

02:05 - Basic structure of CASE
04:35 - Simple vs searched CASE expressions
09:00 - Where you can use CASE in SQL
09:45 - Demo of CASE expressions
14:00 - Demo of top-to-bottom processing
20:20 - Using CASE in WHERE
22:55 - CASE in virtual columns
27:15 - Returning many values using CASE
30:40 - Dynamic WHERE clauses
33:10 - Conditional aggregation
35:55 - Demo ways to return many values
40:15 - Demo of "dynamic" where with CASE
44:50 - Demo of CASE inside COUNT, SUM, AVG
50:35 - CASE in PL/SQL
52:25 - CASE expressions vs statements in PL/SQL
55:45 - Extended CASE controls in PL/SQL in 23c
56:50 - Demo of CASE in PL/SQL

Get the scripts:
Learn SQL for free: .html

Your Experts

    Chris Saxon

    Chris Saxon   

    Chris Saxon is an Oracle Developer Advocate and quizmaster on Dev Gym. His job is to help you get the best out of the Oracle Database and have fun with SQL! He's also part of the answer team on Ask TOM and creates YouTube videos at The Magic of SQL channel.