#39
Extract Product Category from SKU Code
EASYString ManipulationPOSTGRESQL
Problem Description
You are building a product analytics pipeline for a retail data warehouse. The catalog table stores every SKU code in the format CATEGORY-REGION-SEQUENCE, where the first three characters encode the product category (e.g., ELC for Electronics, CLO for Clothing). As part of the ingestion pipeline you need to parse each SKU, extract the category segment, and roll up a count of distinct products per category so that downstream dashboards can track category-level inventory and assortment depth. Write a query that returns each category code and the number of products that belong to it, ordered by product count descending and then by category code ascending to break ties.
**Output columns:** `category`, `product_count`
Topics
aggregationpattern matching
Asked at Companies
walmart
Solve This Problem
Sign up to access the interactive code editor, run test cases, view the editorial, and get AI-powered feedback on your solution.
Start Solving →