SQL Tutorial: In CASE things get more complex
Skills:
SQL Analytics90%
Key Takeaways
This video tutorial covers the use of SQL CASE statements with multiple logical tests, including the use of AND and OR operators, and how to filter query results using the CASE statement in the WHERE clause.
Full Transcript
now that you understand the basics of case statements let's set up some more complex logical tests previously we covered case statements with one logical test in a when statement returning outcomes based on whether that test is true or false this example tests whether home or away goals were higher and identifies them as wins for the team that had a higher score everything else is categorized as a type the resulting table has one column identifying matches as one of three possible outcomes if you want to test multiple logical conditions in a case statement you can use and inside your win Clause for example let's see if each match was played and won by the team Chelsea let's see the case statement in this query each when Clause contains two logical tests the first tests if a home team ID identifies Chelsea and then it tests if the home team scored higher than the away team if both conditions are true the new column output returns the phrase Chelsea home win the opposite set set of conditions are included in a second when statement if the away team ID belongs to Chelsea and scored higher then the output returns Chelsea away win all other matches are categorized as a loss or a tie here's the resulting table when testing logical conditions it's important to carefully consider which rows of your data are part of your else Clause and if they're categorized correctly here's the same case statement from the previous slide but the where filter has been removed without this filter your else Klaus will categorize all matches played by anyone who don't meet these first two conditions as loss or tie here are the results of this query a quick look at it shows that the first few matches are all categorized as loss or tie but neither the home team ID or away team ID belong to Chelsea the easiest way to correct for this is to ensure you add specific filters in the where clause that exclude all teams where Chelsea did not play here we specify this by using an or statement in where which retrieves only the results where ID 8 4 5 5 is present in the home team ID or away team ID columns the resulting table from earlier with the team IDs in bold here clearly specifies whether Chelsea was home or away team it's also important to consider what your else Clause is doing these two queries here are identical except for the else no statement specified in the second they both return identical results a table with quite a few null results but what if you want to exclude them let's say we're only interested in viewing the results of games or Chelsea won and we don't care if they lose or tie just like in the previous example simply removing the else clause will still retrieve those results and a lot of null values to correct for this you can treat the entire case statement as a column to filter by in your where clause just like any other column in order to filter a query by a case statement you include the entire case statement except it's alias in where then specify what you want to include or exclude for this query I want to keep all rows where the case statement is not know my resulting table now only includes Chelsea's home-and-away wins and I don't need to filter by their team ID anymore okay let's practice some more complex case statements
Original Description
Want to learn more? Take the full course at https://learn.datacamp.com/courses/intermediate-sql at your own pace. More than a video, you'll learn hands-on coding & quickly apply skills to your daily work.
---
Now that you understand the basics of CASE statements, let's set up some more complex logical tests.
Previously, we covered CASE statements with one logical test in a WHEN statement, returning outcomes based on whether that test is TRUE or FALSE. This example tests whether home or away goals were higher, and identifies them as wins for the team that had a higher score. Everything ELSE is categorized as a tie.
The resulting table has one column identifying matches as one of 3 possible outcomes.
If you want to test multiple logical conditions in a CASE statement, you can use AND inside your WHEN clause. For example, let's see if each match was played, and won, by the team Chelsea.
Let's see the CASE statement in this query. Each WHEN clause contains two logical tests -- the first tests if a hometead_id identifies Chelsea, AND then it tests if the home team scored higher than the away team. If both conditions are TRUE, the new column output returns the phrase "Chelsea home win!".
The opposite set of conditions are included in a second when statement -- if the awayteam_id belongs to Chelsea, AND scored higher, then the output returns "Chelsea away win!". All other matches are categorized as a loss or tie.
Here's the resulting table.
When testing logical conditions, it's important to carefully consider which rows of your data are part of your ELSE clause, and if they're categorized correctly.
Here's the same CASE statement from the previous slide, but the WHERE filter has been removed. Without this filter, your ELSE clause will categorize ALL matches played by anyone, who don't meet these first two conditions, as "Loss or tie :(".
Here are the results of this query. A quick look at it shows that the first few matches are all categorized as "Loss or tie
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from DataCamp · DataCamp · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
SQL Server Tutorial: Date manipulation
DataCamp
R Tutorial: Intermediate Interactive Data Visualization with plotly in R
DataCamp
R Tutorial: Adding aesthetics to represent a variable
DataCamp
R Tutorial: Moving Beyond Simple Interactivity
DataCamp
Python Tutorial: Why use ML for marketing? Strategies and use cases
DataCamp
Python Tutorial: Preparation for modeling
DataCamp
Python Tutorial: Machine Learning modeling steps
DataCamp
R Tutorial: The prior model
DataCamp
R Tutorial: Data & the likelihood
DataCamp
R Tutorial: The posterior model
DataCamp
R Tutorial: An Introduction to plotly
DataCamp
R Tutorial: Plotting a single variable
DataCamp
R Tutorial: Bivariate graphics
DataCamp
Python Tutorial: Customer Segmentation in Python
DataCamp
Python Tutorial: Time cohorts
DataCamp
Python Tutorial: Calculate cohort metrics
DataCamp
Python Tutorial: Cohort analysis visualization
DataCamp
R Tutorial: Building Dashboards with flexdashboard
DataCamp
R Tutorial: Anatomy of a flexdashboard
DataCamp
R Tutorial: Layout basics
DataCamp
R Tutorial: Advanced layouts
DataCamp
Python Tutorial: Time Series Analysis in Python
DataCamp
Python Tutorial: Correlation of Two Time Series
DataCamp
Python Tutorial: Simple Linear Regressions
DataCamp
Python Tutorial: Autocorrelation
DataCamp
R Tutorial: The gapminder dataset
DataCamp
R Tutorial: The filter verb
DataCamp
R Tutorial: The arrange verb
DataCamp
R Tutorial: The mutate verb
DataCamp
R Tutorial: What is cluster analysis?
DataCamp
R Tutorial: Distance between two observations
DataCamp
R Tutorial: The importance of scale
DataCamp
R Tutorial: Measuring distance for categorical data
DataCamp
Python Tutorial: Plotting multiple graphs
DataCamp
Python Tutorial: Customizing axes
DataCamp
Python Tutorial: Legends, annotations, & styles
DataCamp
Python Tutorial: Introduction to iterators
DataCamp
Python Tutorial: Playing with iterators
DataCamp
Python Tutorial: Using iterators to load large files into memory
DataCamp
SQL Tutorial: Introduction to Relational Databases in SQL
DataCamp
SQL Tutorial: Tables: At the core of every database
DataCamp
SQL Tutorial: Update your database as the structure changes
DataCamp
Python Tutorial: Classification-Tree Learning
DataCamp
Python Tutorial: Decision-Tree for Classification
DataCamp
Python Tutorial: Decision-Tree for Regression
DataCamp
Python Tutorial: Census Subject Tables
DataCamp
Python Tutorial: Census Geography
DataCamp
Python Tutorial: Using the Census API
DataCamp
R Tutorial: A/B Testing in R
DataCamp
R Tutorial: Baseline Conversion Rates
DataCamp
R Tutorial: Designing an Experiment - Power Analysis
DataCamp
R Tutorial: Introduction to qualitative data
DataCamp
R Tutorial: Understanding your qualitative variables
DataCamp
R Tutorial: Making Better Plots
DataCamp
SQL Tutorial: OLTP and OLAP
DataCamp
SQL Tutorial: Storing data
DataCamp
SQL Tutorial: Database design
DataCamp
Python Tutorial: Introduction to spaCy
DataCamp
Python Tutorial: Statistical Models
DataCamp
Python Tutorial: Rule-based Matching
DataCamp
More on: SQL Analytics
View skill →Related AI Lessons
⚡
⚡
⚡
⚡
X now offers an MCP server to make its platform easier for AI tools to use
TechCrunch AI
n8n Automation Repurpose Video Content: The 2025 Production Guide
Dev.to AI
You’re Still Paying $200/Month for AI Tools You Could Replace With a Free Local Setup Tonight
Medium · Data Science
Top 10 AI Tools Every College Student Should Know in 2026
Medium · AI
🎓
Tutor Explanation
DeepCamp AI