Cleaning a PostgreSQL Database
In this project, you will work with data from a hypothetical Super Store to challenge and enhance your SQL skills in data cleaning. This project will engage you in identifying top categories based on the highest profit margins and detecting missing values, utilizing your comprehensive knowledge of SQL concepts.
Data Dictionary:
orders:
orders:| Column | Definition | Data type | Comments |
|---|---|---|---|
row_id | Unique Record ID | INTEGER | |
order_id | Identifier for each order in table | TEXT | Connects to order_id in returned_orders table |
order_date | Date when order was placed | TEXT | |
market | Market order_id belongs to | TEXT | |
region | Region Customer belongs to | TEXT | Connects to region in people table |
product_id | Identifier of Product bought | TEXT | Connects to product_id in products table |
sales | Total Sales Amount for the Line Item | DOUBLE PRECISION | |
quantity | Total Quantity for the Line Item | DOUBLE PRECISION | |
discount | Discount applied for the Line Item | DOUBLE PRECISION | |
profit | Total Profit earned on the Line Item | DOUBLE PRECISION |
returned_orders:
returned_orders:| Column | Definition | Data type |
|---|---|---|
returned | Yes values for Order / Line Item Returned | TEXT |
order_id | Identifier for each order in table | TEXT |
market | Market order_id belongs to | TEXT |
people:
people:| Column | Definition | Data type |
|---|---|---|
person | Name of Salesperson credited with Order | TEXT |
region | Region Salesperson in operating in | TEXT |
products:
products:| Column | Definition | Data type |
|---|---|---|
product_id | Unique Identifier for the Product | TEXT |
category | Category Product belongs to | TEXT |
sub_category | Sub Category Product belongs to | TEXT |
product_name | Detailed Name of the Product | TEXT |
As you can see in the Data Dictionary above, date fields have been written to the orders table as TEXT and numeric fields like sales, profit, etc. have been written to the orders table as Double Precision. You will need to take care of these types in some of the queries. This project is an excellent opportunity to apply your SQL skills in a practical setting and gain valuable experience in data cleaning and analysis. Good luck, and happy querying!
-- top_five_products_each_category
WITH ranked_products AS (
SELECT p.category, p.product_name,
round(SUM(o.sales)::numeric, 2) AS product_total_sales,
round(SUM(o.profit)::numeric, 2) AS product_total_profit,
rank() over (partition by p.category order by sum(o.sales) desc) as product_rank
FROM orders o
JOIN products p ON o.product_id = p.product_id
GROUP BY p.category, p.product_name
)
SELECT category, product_name, product_total_sales, product_total_profit, product_rank
FROM ranked_products
WHERE product_rank <= 5
ORDER BY category, product_rank;-- impute_missing_values
WITH missing AS (
-- Step 1: Get all rows where quantity is missing
SELECT DISTINCT
product_id,
discount,
market,
region,
sales,
quantity,
NULL AS calculated_quantity -- Placeholder for the calculated quantity
FROM orders
WHERE quantity IS NULL
),
unit_prices AS (
-- Step 2: Calculate unit price (sales per quantity) for each product_id
SELECT
product_id,
CAST(AVG(sales / NULLIF(quantity, 0)) AS NUMERIC(10,2)) AS unit_price -- Prevent division by zero
FROM orders
WHERE quantity IS NOT NULL
GROUP BY product_id
)
-- Step 3: Select distinct missing rows and compute calculated_quantity
SELECT DISTINCT
m.product_id,
m.discount,
m.market,
m.region,
m.sales,
m.quantity, -- Will be NULL
ROUND(CAST(m.sales AS NUMERIC) / up.unit_price, 0) AS calculated_quantity -- Calculate missing quantity
FROM missing m
INNER JOIN unit_prices up
ON m.product_id = up.product_id;