All the course material is stored in the SQL Crash Course repository.
Hi everyone! Josep and Cornellius Yudha Wijaya from Non-Brand Data here 👋🏻
As promised, today we are publishing the next two issues of our SQL Crash Course – From Zero to Hero! 🚀
I am sure you are here to continue our SQL Crash Course Journey!📚
If this is your first time or you’ve forgotten what we’ll cover, we will examine seven key SQL topics, each divided into multiple posts across Non-Brand Data and DataBites.
📚 Previously in SQL Basics…
Remember last Thursday we already saw
📌 #7 – JOINS (INNER, LEFT, RIGHT, FULL) by me in
📌 #8 – UNION & UNION ALL by in .
Today, two fresh issues just dropped:
📌 #9 – Case Expressions – the one you're reading right now is all about combining understanding CASE expressions in SQL.
📌 #10 – Functions (String, Date, Numeric) – where Cornellius explains how to work with SQL functions.
So don’t miss out—let’s keep the SQL momentum going!
🎯 Why CASE expressions matter
Sometimes you want your query to behave differently depending on values in your data — like showing “High”, “Medium”, or “Low” based on a score or defining a concrete category according to a numerical value.
Instead of running multiple queries or post-processing in Python or Excel…
you can do it right in SQL using a CASE
expression.
🧱 What is a CASE expression?
Think of it as an IF / ELSE IF / ELSE block in SQL.
You check conditions one by one, and return a value when the first match is found.
It works inside SELECT
, WHERE
, ORDER BY
, even GROUP BY
.
The most common structure:
SELECT
CASE
WHEN condition1 THEN result1
WHEN condition2 THEN result2
ELSE default_result
END AS alias
FROM your_table;
CASE: Begins the conditional logic block.
WHEN condition THEN result: If condition is TRUE, return result.
Multiple WHEN clauses: You can include as many WHEN clauses as needed, each testing a different condition.
ELSE default_result: Returned if no WHEN conditions are true.
END: Closes the CASE block.
AS alias: Names the resulting column.
🔍 Example 1 – Categorizing Interaction Volume
Let’s use our POSTS
and INTERACTIONS
tables.
We’ll count how many interactions each post received and categorize them:
SELECT
P.name AS post_name,
COUNT(I.id) AS num_interactions,
CASE
WHEN COUNT(I.id) = 0 THEN '🟥 No Interactions'
WHEN COUNT(I.id) BETWEEN 1 AND 3 THEN '🟨 Low'
WHEN COUNT(I.id) BETWEEN 4 AND 6 THEN '🟧 Medium'
ELSE '🟩 High'
END AS interaction_level
FROM POSTS P
LEFT JOIN INTERACTIONS I
ON P.id = I.post_id
GROUP BY P.name;
🧠 This is great for building dashboards or summaries that are easier to understand at a glance.
🔍 Example 2 – Conditional Filtering in WHERE
Want to only show interactions that are likes or comments, and group all other types as “Other”? Use CASE
in a WHERE
clause:
SELECT
I.id,
I.type_of_interaction,
CASE
WHEN I.type_of_interaction = 'like' THEN 'Like'
WHEN I.type_of_interaction = 'comment' THEN 'Comment'
ELSE 'Other'
END AS interaction_category
FROM INTERACTIONS I;
🧠 This helps you simplify messy or inconsistent data directly in your query logic.
🔄 Bonus: CASE in ORDER BY
You can also use CASE
to custom sort results. For example, prioritize posts with no interactions:
SELECT
P.name,
COUNT(I.id) AS num_interactions
FROM POSTS P
LEFT JOIN INTERACTIONS I
ON P.id = I.post_id
GROUP BY P.name
ORDER BY
CASE
WHEN COUNT(I.id) = 0 THEN 0
ELSE 1
END,
P.name;
🧠 Super helpful when you want certain categories to float to the top — without changing your database.
✅ Summary: Use CASE When You Want To…
Add custom labels or categorical buckets to your query output
Transform values based on conditions
Simulate IF/ELSE logic directly in SQL
Clean or normalize data on the fly
Control sorting, filtering, and grouping dynamically
👉🏻 SQL playground with all CASE expressions examples
How to Get Started 🚀
Over the coming weeks, we’ll guide you through:
✅ SQL Fundamentals
✅ Intermediate SQL
✅ Advanced SQL
✅ Database Operations
✅ Writing Efficient Queries
Once you grasp the basics, practice is key! Start working on real-world projects and solving hands-on data problems.
What’s Next? ➡️
This is the first of many posts about the upcoming SQL Courses. It will only explain what SQL is in its crude form.
To get the full experience and fully immersed in the learning:
👉 Subscribe to Databites.tech (By Josep)
👉 Subscribe to Non-Brand Data (By Cornellius)
👉 Check out the SQL Crash Course GitHub repo
👉 Share with your friend and whoever needs it!
🗓️ Every Thursday, you will have two new issues in your inbox!
Let’s dive in and make SQL less scary, more fun, and way more useful! 🚀
Josep & Cornellius
🎉 DataBites just hit 8,000 subs & #76 in Rising Tech on Substack — thanks to you!
To celebrate, get 20% off for life on the paid plan until April 13.
👉🏻 Grab it here if you want full access to guides, cheatsheets, and more.