BBampaa

Lessons › SQL › Deep Dive: Data Cleaning

SQL lesson

Standardizing categories with CASE WHEN and LOWER()

Combining a case-insensitive conversion with conditional logic lets you collapse messy variations of the same value into one clean, consistent label.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

Standardizing Retail Categories with CASE WHEN and LOWER()

SELECT product_id, brand, price,
  CASE
    WHEN LOWER(category) = 'electronics' THEN 'Electronics'
    WHEN LOWER(category) = 'clothing' THEN 'Clothing'
    WHEN LOWER(category) = 'beauty' THEN 'Beauty'
    WHEN LOWER(category) = 'grocery' THEN 'Grocery'
    WHEN LOWER(category) = 'sports' THEN 'Fashion'
    ELSE 'Other'
  END AS clean_category
FROM products

Result:

product_idbrandpriceclean_category
P1001NovaTech299.99Electronics
P1002UrbanWear45.0Clothing
P1003GlowUp18.5Beauty
P1004FreshMart5.75Grocery
P1005ActiveGear89.99Fashion

Step by step

  1. One WHEN/THEN pair handles a single condition; rows that don't match get NULL (or the ELSE if present).
    SELECT
      CASE
        WHEN LOWER(category) = 'electronics' THEN 'Electronics'
        ELSE 'Other'
      END AS category
    FROM products;;
  2. Add a second WHEN - SQL checks the conditions top-down and uses the first match.
    SELECT
      CASE
        WHEN LOWER(category) = 'electronics' THEN 'Electronics'
        WHEN LOWER(category) = 'clothing' THEN 'Clothing'
        ELSE 'Other'
      END AS category
    FROM products;;
  3. Add a third branch the same way. Each WHEN is checked in order; first match wins.
    SELECT
      CASE
        WHEN LOWER(category) = 'electronics' THEN 'Electronics'
        WHEN LOWER(category) = 'clothing' THEN 'Clothing'
        WHEN LOWER(category) = 'beauty' THEN 'Beauty'
        ELSE 'Other'
      END AS category
    FROM products;;

Practice

Try each one in your head first, then open the answer.

1. Using customers, write a query that standardizes customer_id by converting it to lowercase and replacing 'n/a' with NULL.
SELECT CASE WHEN LOWER(customer_id) = 'n/a' THEN NULL ELSE LOWER(customer_id) END FROM customers
2. Using products, write a query that standardizes brand by converting it to lowercase and replacing 'n/a' with NULL.
SELECT CASE WHEN LOWER(brand) = 'n/a' THEN NULL ELSE LOWER(brand) END FROM products
3. Using suppliers, write a query that standardizes country by converting it to lowercase and replacing 'n/a' with NULL.
SELECT CASE WHEN LOWER(country) = 'n/a' THEN NULL ELSE LOWER(country) END FROM suppliers
4. Using employees, write a query that standardizes employee_id by converting it to lowercase and replacing 'n/a' with NULL.
SELECT CASE WHEN LOWER(employee_id) = 'n/a' THEN NULL ELSE LOWER(employee_id) END FROM employees
5. Using reviews, write a query that standardizes review_id by converting it to lowercase and replacing 'n/a' with NULL.
SELECT CASE WHEN LOWER(review_id) = 'n/a' THEN NULL ELSE LOWER(review_id) END FROM reviews

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed