Which of the following best explains why CASE expressions are needed in SQL queries?
Think about how you can show different results for different rows in one query.
CASE expressions let you add conditional logic inside SQL queries, so you can return different values depending on data in each row.
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)
Check the condition Salary >= 5000 for each row.
The CASE checks if Salary is 5000 or more; if yes, it returns 'High', else 'Low'. Anna has 6000 so 'High', Bob has 4000 so 'Low'.
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;
Remember the difference between simple CASE and searched CASE syntax.
Option C is invalid because simple CASE compares Salary to a value, but >= 5000 is a condition, not a value. It causes syntax error.
Which option shows the best use of CASE to avoid multiple queries when categorizing data?
Think about how to get all categories in one query efficiently.
Using CASE in SELECT lets you assign categories in one query, which is faster and simpler than multiple queries or joins.
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'?
Check the data type of Score and how comparisons work.
If Score is text, '100' > '90' is false because string comparison is lexicographic, causing unexpected CASE results.