Q1Which of the following correctly describes IIF(condition, X, Y)?
Functions ④ — IF / IIF for Single-Condition Branching
The 4th SQL functions article. Branching a value on one true/false condition with IF / IIF, how it differs from WHERE, combining with LIKE and AND, and stacking ORDER BY / LIMIT — using CSV staff data.
Data Used in This Article — the staff Table
We have covered functions in three articles so far; this one and the next two deal with switching what is displayed based on a condition. From the same column value, you can attach classification labels like "High" or "Normal" depending on whether a condition holds, or fold several values into a single result column.
IF / IIF — Switch a Value on One Condition
The most basic function for switching the displayed value based on a condition is IF / IIF. The syntax is below, and the two are exactly the same in meaning.
- MySQL: IF(condition, value if true, value if false)
- This course's console / SQL Server: IIF(condition, value if true, value if false)
This course's console has no IF function, so the exercises use IIF. Unlike WHERE, it does not filter rows — it only changes the value of the result column, so you use it when you want to "keep every row but change the display label per column value".
-- 1) Quick check with literals
SELECT IIF(100 < 200, 'T', 'F'); -- 'T'
SELECT IIF(500 > 1000, 'T', 'F'); -- 'F'
-- 2) Using a column — 'HQ' if city is Tokyo, otherwise 'Branch'
SELECT name, city,
IIF(city = 'Tokyo', 'HQ', 'Branch') AS office
FROM staff;
-- Reference: in MySQL, use IF()
-- SELECT IF(city = 'Tokyo', 'HQ', 'Branch') FROM staff;
The Difference Between WHERE and IF — Removing Rows vs Changing Values
WHERE and IF both take a condition, but the effect on the result is completely different.
- `WHERE`: keeps only the rows where the condition is TRUE, and removes FALSE / NULL rows
- `IF` / `IIF`: keeps every row and only swaps the column value based on the condition
The two combine. A typical pattern is "show only staff based in Tokyo, and split each person's pay rank into High / Normal": first narrow rows with WHERE, then transform the display with IIF in the SELECT column list.
-- Combine WHERE and IIF
-- 1) Use WHERE to keep only staff based in Osaka
-- 2) Use IIF to classify salary of 6,000,000 or more as 'Senior'
SELECT name, salary,
IIF(salary >= 6000000, 'Senior', 'General') AS grade
FROM staff
WHERE city = 'Osaka';
Combining with LIKE and AND, and Stacking ORDER BY / LIMIT
Because IIF's condition accepts the same expressions as WHERE, you can combine it with LIKE (pattern matching) or AND / OR (compound conditions). Branches like "label as Tanaka surname if the name contains Tanaka" or "ace if 30+ and based in Tokyo" fit on one line.
The AS alias on the result column can also be a target of `ORDER BY`, so sorting by the label column produced by IIF and then limiting rows with LIMIT is a basic pattern for report output in practice.
-- 1) IIF + LIKE: label 'Tanaka' if the surname is Tanaka
SELECT name,
IIF(name LIKE '%Tanaka', 'Tanaka', 'Other') AS surname_check
FROM staff;
-- 2) IIF + AND: compound condition on age-equivalent + city
SELECT name, birthday, city,
IIF(strftime('%Y', birthday) <= '1989' AND city = 'Osaka',
'Osaka senior', 'Other') AS tag
FROM staff;
Knowledge Check
Answer each question one by one.
Q2Which is the correct difference between WHERE and IIF?
Q3What is the correct rewrite when running MySQL's IF(salary >= 5000000, 'High', 'Normal') in this course's console?