TGViewer
SQL Programming Resources SQL Programming Resources @sqlanalyst · 76.7K subscribers
Post #2255 2.89K
✅ SQL CASE Statements and Data Cleaning Interview Questions with Answers 📊

1. Classify customers based on total spend
Table: orders (order_id, customer_id, amount)

Requirement:
- Gold if total_spend >= 100000
- Silver if total_spend between 50000 and 99999
- Bronze otherwise

Solution:
SELECT customer_id,
CASE
WHEN SUM(amount) >= 100000 THEN 'Gold'
WHEN SUM(amount) >= 50000 THEN 'Silver'
ELSE 'Bronze'
END AS customer_category
FROM orders
GROUP BY customer_id;

2. Create an age group column for users
Table: users (user_id, age)

Requirement:
- Below 18
- 18 to 30
- Above 30

Solution:
SELECT user_id,
CASE
WHEN age < 18 THEN 'Below 18'
WHEN age BETWEEN 18 AND 30 THEN '18-30'
ELSE 'Above 30'
END AS age_group
FROM users;

3. Replace NULL salary with 0
Table: employees (employee_id, salary)

Solution:
SELECT employee_id, COALESCE(salary, 0) AS salary
FROM employees;

4. Count employees with missing email IDs
Table: employees (employee_id, email)

Solution:
SELECT COUNT(*) AS missing_email_count
FROM employees
WHERE email IS NULL;

5. Remove extra spaces from customer names
Table: customers (customer_id, name)

Solution:
SELECT customer_id, TRIM(name) AS clean_name
FROM customers;

6. Extract first 3 characters from product code
Table: products (product_code)

Solution:
SELECT product_code, SUBSTRING(product_code, 1, 3) AS product_prefix
FROM products;

7. Standardize date format
Table: orders (order_date)

Requirement: Convert to YYYY-MM-DD

Solution:
SELECT CAST(order_date AS DATE) AS clean_order_date
FROM orders;

8. Mark inactive users based on last login
Table: users (user_id, last_login_date)

Requirement: Inactive if last login before 2023-01-01

Solution:
SELECT user_id,
CASE
WHEN last_login_date < '2023-01-01' THEN 'Inactive'
ELSE 'Active'
END AS user_status
FROM users;

9. Handle empty string as NULL
Table: customers (phone_number)

Solution:
SELECT NULLIF(phone_number, '') AS phone_number
FROM customers;

10. Create a clean reporting column using multiple rules
Table: transactions (amount)

Requirement:
- High if amount >= 10000
- Medium if 5000 to 9999
- Low otherwise

Solution:
SELECT amount,
CASE
WHEN amount >= 10000 THEN 'High'
WHEN amount >= 5000 THEN 'Medium'
ELSE 'Low'
END AS transaction_type
FROM transactions;

Double Tap ♥️ For More
  • ❤ 8
More from @sqlanalyst
  1. Oct 13, 2026SQL Interview Series — Part 8 📌 Question 8: Find Customers Who Have Never Placed an Order…
  2. Oct 13, 2026𝗧𝗢𝗣 𝗙𝗥𝗘𝗘 𝗖𝗢𝗨𝗥𝗦𝗘𝗦 𝗧𝗢 𝗕𝗢𝗢𝗦𝗧 𝗬𝗢𝗨𝗥 𝗦𝗞𝗜𝗟𝗟𝗦 𝗜𝗡 𝟮𝟬𝟮𝟲! Explor…
  3. Oct 12, 2026SQL Interview Series — Part 6 📌 Question 6: Find the Top 2 Highest-Paid Employees in Each…
  4. Oct 12, 2026🚀 𝗪𝗵𝗶𝗰𝗵 𝗧𝗲𝗰𝗵 𝗦𝗸𝗶𝗹𝗹 𝗪𝗶𝗹𝗹 𝗬𝗼𝘂 𝗠𝗮𝘀𝘁𝗲𝗿 𝗶𝗻 𝟮𝟬𝟮𝟲? Want to buil…
  5. Oct 9, 2026SQL Interview Series — Part 5 📌 Question 5: Find Employees Who Earn More Than Their Manag…
  6. Oct 9, 2026🇮🇳 𝗚𝗢𝗩𝗘𝗥𝗡𝗠𝗘𝗡𝗧 𝗢𝗙 𝗜𝗡𝗗𝗜𝗔 — 𝗔𝗜𝗖𝗧𝗘 𝗜𝗡𝗧𝗘𝗥𝗡𝗦𝗛𝗜𝗣𝗦 𝟮𝟬𝟮𝟲 🚀…
Threads Profile ViewerView any public Threads profile without an account.Open ThreadLook →Writing with AI? Make it sound human.Metric37 rewrites AI drafts so they read naturally. Free AI detector, 1,500 words free.Try Metric37 →