0
0
PostgreSQLquery~10 mins

CASE expression in PostgreSQL - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to return 'Adult' if age is 18 or more, otherwise 'Minor'.

PostgreSQL
SELECT name, CASE WHEN age >= [1] THEN 'Adult' ELSE 'Minor' END AS status FROM people;
Drag options to blanks, or click blank then click option'
A18
B21
C16
D20
Attempts:
3 left
💡 Hint
Common Mistakes
Using a number less than 18 will misclassify adults as minors.
Using '>' instead of '>=' excludes exactly 18 years old.
2fill in blank
medium

Complete the code to categorize scores: 90 or above as 'Excellent', else 'Good'.

PostgreSQL
SELECT student, CASE WHEN score >= [1] THEN 'Excellent' ELSE 'Good' END AS grade FROM results;
Drag options to blanks, or click blank then click option'
A80
B85
C90
D95
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing a lower number will mark too many as 'Excellent'.
Choosing a higher number excludes some who scored exactly 90.
3fill in blank
hard

Fix the error in the CASE expression to correctly assign 'Pass' if marks are 50 or more, else 'Fail'.

PostgreSQL
SELECT student, CASE WHEN marks [1] 50 THEN 'Pass' ELSE 'Fail' END AS result FROM exams;
Drag options to blanks, or click blank then click option'
A>
B>=
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' excludes students with exactly 50 marks.
Using '<' or '<=' reverses the logic.
4fill in blank
hard

Fill both blanks to classify temperature: 'Hot' if temp > 30, 'Warm' if temp > 20, else 'Cold'.

PostgreSQL
SELECT city, CASE WHEN temperature [1] 30 THEN 'Hot' WHEN temperature [2] 20 THEN 'Warm' ELSE 'Cold' END AS weather FROM forecast;
Drag options to blanks, or click blank then click option'
A>
B>=
C<=
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>=' includes boundary values incorrectly here.
Using '<=' or '<' reverses the logic.
5fill in blank
hard

Fill all three blanks to create a CASE expression that returns 'A' for score >= 90, 'B' for score >= 80, else 'C'.

PostgreSQL
SELECT student, CASE WHEN score [1] 90 THEN 'A' WHEN score [2] 80 THEN 'B' ELSE [3] END AS grade FROM tests;
Drag options to blanks, or click blank then click option'
A>=
B>
C'C'
D'B'
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' excludes boundary scores.
Not quoting the ELSE result causes syntax errors.