From the course: SQL for Non-Programmers

Unlock the full course today

Join today to access over 23,100 courses taught by industry experts.

Categorize data with CASE

Categorize data with CASE - SQL Tutorial

From the course: SQL for Non-Programmers

Categorize data with CASE

Welcome to the advanced part of our course. Are you ready to take your skills to the next level? We are going to cover advanced features that can be used to give you more control of your data. Let's start with the CASE statement. A CASE statement is used to create categories using the grouping of values. It is placed within the SELECT statement. CASE statements use for main keywords: CASE, WHEN, THEN, and END, as well as an optional keyword ELSE. Using these keywords, you can apply a logic to say that when certain values appear or conditions are met, the data is categorized or labeled a certain way. For example, imagine that we are working with a table of customers and the table includes their age. With a CASE statement, we could group the customers into different age brackets and then count how many customers fell into each group. For example, we could write a query that says, if the customer is between 18 and 30 years old, label them as young adults. If the customer is between 31…

Contents