Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Leer CASE WHEN in WHERE Clauses | Conditional Filtering and Sorting with CASE WHEN
Mastering CASE WHEN in SQL

bookCASE WHEN in WHERE Clauses

Conditional filtering allows you to apply different criteria to rows in a table depending on certain conditions. This is especially useful when you want to filter results flexibly based on column values. The CASE WHEN expression in SQL can be used within a WHERE clause to create such dynamic filtering logic. By using CASE WHEN, you can apply different filtering thresholds or rules based on the data in each row, rather than being restricted to a single condition for all rows. This technique is powerful when you need to handle complex business logic directly in your queries.

123456789101112
SELECT title, genre, rating FROM movies WHERE CASE WHEN genre = 'Action' THEN rating >= 8.5 WHEN genre = 'Drama' THEN rating >= 9.0 ELSE rating >= 8.0 END;
copy

In the query above, CASE WHEN is used inside the WHERE clause to apply different rating thresholds depending on the movie's genre. For "Action" movies, only those with a rating of at least 8.5 are selected. For "Drama" movies, the threshold is even higher at 9.0. All other genres must have a rating of at least 8.0 to be included. The CASE WHEN expression evaluates each row, and the result of the expression determines whether the row passes the filter. If the condition in the CASE block evaluates to true, the row is included in the result set. This approach allows you to implement advanced filtering logic without writing multiple queries or using complex OR and AND combinations.

1234567891011
SELECT title, release_year, box_office FROM movies WHERE CASE WHEN box_office >= 500000000 THEN release_year >= 2000 ELSE release_year < 2000 END;
copy

1. What is a benefit of using CASE WHEN in a WHERE clause?

2. Fill in the blanks to complete the WHERE clause so that it selects comedies with a rating greater than or equal to 7, or dramas with a rating greater than or equal to 8. All other genres should be excluded.

question mark

What is a benefit of using CASE WHEN in a WHERE clause?

Select the correct answer

question-icon

Fill in the blanks to complete the WHERE clause so that it selects comedies with a rating greater than or equal to 7, or dramas with a rating greater than or equal to 8. All other genres should be excluded.

WHEN genre = 'Drama' THEN rating >= ELSE FALSE END;

Click or drag`n`drop items and fill in the blanks

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 1

Vraag AI

expand

Vraag AI

ChatGPT

Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.

bookCASE WHEN in WHERE Clauses

Veeg om het menu te tonen

Conditional filtering allows you to apply different criteria to rows in a table depending on certain conditions. This is especially useful when you want to filter results flexibly based on column values. The CASE WHEN expression in SQL can be used within a WHERE clause to create such dynamic filtering logic. By using CASE WHEN, you can apply different filtering thresholds or rules based on the data in each row, rather than being restricted to a single condition for all rows. This technique is powerful when you need to handle complex business logic directly in your queries.

123456789101112
SELECT title, genre, rating FROM movies WHERE CASE WHEN genre = 'Action' THEN rating >= 8.5 WHEN genre = 'Drama' THEN rating >= 9.0 ELSE rating >= 8.0 END;
copy

In the query above, CASE WHEN is used inside the WHERE clause to apply different rating thresholds depending on the movie's genre. For "Action" movies, only those with a rating of at least 8.5 are selected. For "Drama" movies, the threshold is even higher at 9.0. All other genres must have a rating of at least 8.0 to be included. The CASE WHEN expression evaluates each row, and the result of the expression determines whether the row passes the filter. If the condition in the CASE block evaluates to true, the row is included in the result set. This approach allows you to implement advanced filtering logic without writing multiple queries or using complex OR and AND combinations.

1234567891011
SELECT title, release_year, box_office FROM movies WHERE CASE WHEN box_office >= 500000000 THEN release_year >= 2000 ELSE release_year < 2000 END;
copy

1. What is a benefit of using CASE WHEN in a WHERE clause?

2. Fill in the blanks to complete the WHERE clause so that it selects comedies with a rating greater than or equal to 7, or dramas with a rating greater than or equal to 8. All other genres should be excluded.

question mark

What is a benefit of using CASE WHEN in a WHERE clause?

Select the correct answer

question-icon

Fill in the blanks to complete the WHERE clause so that it selects comedies with a rating greater than or equal to 7, or dramas with a rating greater than or equal to 8. All other genres should be excluded.

WHEN genre = 'Drama' THEN rating >= ELSE FALSE END;

Click or drag`n`drop items and fill in the blanks

Was alles duidelijk?

Hoe kunnen we het verbeteren?

Bedankt voor je feedback!

Sectie 3. Hoofdstuk 1
some-alt