DISCLAIMER: Before starting this tutorial, it's highly recommended that you download an RDBMS to follow along.
We recommend using either PostgreSQL, MS SQL Server, or MySQL.
When conducting data analysis tasks, you will be working with two types of variables: discrete/categorical and
continuous/scalar. In this tutorial, we will be focusing on how to create categorical variables through the
use of a conditional column. This tutorial will cover the following learning objectives:
SELECT [column_name], [column_name],
CASE
WHEN [condition1] THEN [value]
WHEN [condition2] THEN [value]
ELSE [value]
END AS [new_column_name]
FROM [table_name];
SELECT [column_name], [column_name],
COUNT(CASE
WHEN [condition1] THEN [integer]
WHEN [condition2] THEN [integer]
ELSE [integer] END) AS [new_column_name]
FROM [table_name]
GROUP BY [column_name], [column_name];
SELECT [column_name], [column_name],
SUM(CASE
WHEN [condition1] THEN [column_name]
WHEN [condition2] THEN [column_name]
ELSE [integer] END) AS [new_column_name]
FROM [table_name]
GROUP BY [column_name], [column_name];
Congratulations! You just completed the CASE Statements Tutorial! To help test your knowledge, let's
use the "lego" database to run some queries with CASE statements. This exercise is based on the exercise of
the "Joins" tutorial.
**It's highly recommended that you
complete the exercise outlined in the previous tutorial before beginning this exercise.**
The Database used in this exercise includes data on hundreds of Lego sets and their associated parts, colors, and themes.
Have any issues with the above exercise? Post your question on Discord!