If you want to add an additional column, how do you display it conditionally and calculate its value?
💡 Model Answer
To add a computed column that is shown only when a condition is met, you can use a CASE expression or a computed column in the SELECT list. For example, suppose you have a table "accounts" with columns "balance" and "status". You want to show a column "overdrawn" that is 1 when balance < 0 and 0 otherwise:
SELECT
account_id,
balance,
CASE WHEN balance < 0 THEN 1 ELSE 0 END AS overdrawn
FROM accounts;
If you only want the column to appear when a specific condition holds (e.g., status = 'active'), you can wrap the CASE in a filter or use a NULL to hide it:
SELECT
account_id,
balance,
CASE WHEN status = 'active' THEN
CASE WHEN balance < 0 THEN 1 ELSE 0 END
ELSE NULLEND AS overdrawn
FROM accounts;
This approach keeps the column in the result set but returns NULL for rows that don't meet the outer condition. In many RDBMS you can also create a persisted computed column in the table definition, but for ad‑hoc queries the CASE method is most common.
This answer was generated by AI for study purposes. Use it as a starting point — personalize it with your own experience.
🎤 Get questions like this answered in real-time
Assisting AI listens to your interview, captures questions live, and gives you instant AI-powered answers — invisible to screen sharing.
Get Assisting AI — Starts at ₹500