Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

bookCASE WHEN in WHERE Clauses

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 3. Kapittel 1
some-alt