0
0
SQLquery~20 mins

Why CASE expressions are needed in SQL - Challenge Your Understanding

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
CASE Expression Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
Why use CASE expressions in SQL?

Which of the following best explains why CASE expressions are needed in SQL queries?

ATo speed up query execution by skipping joins.
BTo perform conditional logic within queries, allowing different outputs based on row values.
CTo create new tables automatically without specifying columns.
DTo encrypt data stored in the database.
Attempts:
2 left
💡 Hint

Think about how you can show different results for different rows in one query.

query_result
intermediate
2:00remaining
Output of a CASE expression in SELECT

Given a table Employees with a column Salary, what is the output of this query?

SELECT Name, CASE WHEN Salary >= 5000 THEN 'High' ELSE 'Low' END AS SalaryLevel FROM Employees;

Assuming the table has rows: (Name: 'Anna', Salary: 6000), (Name: 'Bob', Salary: 4000)

ASyntax error due to missing ELSE clause
B[{"Name": "Anna", "SalaryLevel": "Low"}, {"Name": "Bob", "SalaryLevel": "High"}]
C[{"Name": "Anna", "SalaryLevel": 6000}, {"Name": "Bob", "SalaryLevel": 4000}]
D[{"Name": "Anna", "SalaryLevel": "High"}, {"Name": "Bob", "SalaryLevel": "Low"}]
Attempts:
2 left
💡 Hint

Check the condition Salary >= 5000 for each row.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in CASE expression

Which option contains a syntax error in the CASE expression?

SELECT Name, CASE Salary WHEN >= 5000 THEN 'High' ELSE 'Low' END AS SalaryLevel FROM Employees;
ACASE Salary WHEN 5000 THEN 'High' ELSE 'Low' END
BCASE WHEN Salary >= 5000 THEN 'High' ELSE 'Low' END
CCASE Salary WHEN >= 5000 THEN 'High' ELSE 'Low' END
DCASE WHEN Salary < 5000 THEN 'Low' ELSE 'High' END
Attempts:
2 left
💡 Hint

Remember the difference between simple CASE and searched CASE syntax.

optimization
advanced
2:00remaining
Optimizing queries with CASE expressions

Which option shows the best use of CASE to avoid multiple queries when categorizing data?

AUse CASE in SELECT to assign categories in one query, reducing database calls.
BRun separate queries for each category and combine results in application code.
CUse multiple JOINs to filter categories separately.
DUse subqueries for each category and UNION them.
Attempts:
2 left
💡 Hint

Think about how to get all categories in one query efficiently.

🔧 Debug
expert
3:00remaining
Debugging unexpected CASE output

Given this query:

SELECT Name, CASE WHEN Score > 90 THEN 'A' WHEN Score > 80 THEN 'B' ELSE 'C' END AS Grade FROM Students;

Why might some students with Score 100 get Grade 'C' instead of 'A'?

AThe Score column is stored as text, so comparisons behave unexpectedly.
BThe CASE expression is missing an ELSE clause.
CThe query has a syntax error causing wrong output.
DThe Score values are NULL, so CASE returns 'C' by default.
Attempts:
2 left
💡 Hint

Check the data type of Score and how comparisons work.