BBampaa

Lessons › SQL › Chapter 9: Data Cleaning

SQL lesson

Standardizing categories with CASE WHEN and LOWER()

The same category can be typed many ways: 'Beauty', 'BEAUTY', 'beauty'. LOWER makes them all match. CASE WHEN then turns each one into a single tidy name. ELSE catches anything you didn't list.

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

Example

One spelling for each value

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 'Sports'
         ELSE 'Other'
       END AS clean_category
FROM products

Result:

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

Step by step

  1. 'BEAUTY' and 'beauty' both become 'beauty'.
    SELECT product_id, CASE WHEN LOWER(category) = 'clothing' THEN 'Clothing' WHEN LOWER(category) = 'beauty' THEN 'Beauty' ELSE 'Other' END AS clean_category FROM products
  2. CASE turns each match into one clean label.
    SELECT product_id, CASE WHEN LOWER(category) = 'clothing' THEN 'Clothing' WHEN LOWER(category) = 'beauty' THEN 'Beauty' ELSE 'Other' END AS clean_category FROM products
  3. ELSE handles anything you didn't list.
    SELECT product_id, CASE WHEN LOWER(category) = 'clothing' THEN 'Clothing' WHEN LOWER(category) = 'beauty' THEN 'Beauty' ELSE 'Other' END AS clean_category FROM products

Practice

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

1. Using customers, show customer_id and city in small letters. Name it clean_city.
SELECT customer_id, LOWER(city) AS clean_city FROM customers
2. Using customers, show customer_id and city in small letters, with n/a turned into NULL however it's typed. Name it clean_city.
SELECT customer_id, CASE WHEN LOWER(city) = 'n/a' THEN NULL ELSE LOWER(city) END AS clean_city FROM customers
3. Using suppliers, show supplier_id and country in small letters, with n/a turned into NULL however it's typed. Name it clean_country.
SELECT supplier_id, CASE WHEN LOWER(country) = 'n/a' THEN NULL ELSE LOWER(country) END AS clean_country FROM suppliers
4. Using employees, show employee_id and department, with 'Mktg', 'MKTG' and 'Marketing' all written as 'marketing', and every other department in small letters. Name it clean_department.
SELECT employee_id, CASE WHEN LOWER(department) IN ('mktg', 'marketing') THEN 'marketing' ELSE LOWER(department) END AS clean_department FROM employees
5. Using shipments, count how many shipments have each status, with the spelling tidied to small letters. Show clean_status and shipments.
SELECT LOWER(status) AS clean_status, COUNT(*) AS shipments FROM shipments GROUP BY LOWER(status)

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