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 sales AS (
SELECT product_id, ROUND(SUM(sales::NUMERIC), 2) as product_total_sales
FROM orders
INNER JOIN products
USING (product_id)
GROUP BY product_id
),
profit AS (
SELECT product_id, ROUND(SUM(profit::NUMERIC), 2) as product_total_profit
FROM products
LEFT JOIN orders
USING (product_id)
GROUP BY product_id
),
ranked AS (
SELECT p.category, p.product_name, s.product_total_sales, r.product_total_profit, RANK() OVER(
PARTITION BY p.category
ORDER BY s.product_total_sales DESC
) AS product_rank
FROM products AS p
LEFT JOIN sales AS s
ON p.product_id=s.product_id
LEFT JOIN profit as r
ON p.product_id=r.product_id
)
SELECT *
FROM ranked
WHERE product_rank <6
ORDER BY category;-- impute_missing_values
WITH unitary AS (
SELECT product_id, discount, market, region, SUM(sales::NUMERIC) / SUM(quantity::NUMERIC) AS unit_price
FROM orders
WHERE quantity IS NOT NULL
GROUP BY product_id, discount, market, region
),
missing AS (
SELECT o.product_id, o.discount, o.market, o.region, o.quantity, ROUND(o.sales::NUMERIC/u.unit_price, 0) AS calculated_quantity
FROM orders AS o
LEFT JOIN unitary AS u
ON o.product_id = u.product_id
WHERE quantity IS NULL
)
SELECT o.product_id, o.discount, o.market, o.region, o.sales, o.quantity, m.calculated_quantity
FROM orders as o
FULL JOIN missing as m
ON o.product_id = m.product_id;WITH unitary AS (
SELECT product_id, discount, market, region, SUM(sales::NUMERIC) / SUM(quantity::NUMERIC) AS unit_price
FROM orders
WHERE quantity IS NOT NULL
GROUP BY product_id, discount, market, region
),
missing AS (
SELECT o.product_id, o.discount, o.market, o.region, o.quantity, ROUND(o.sales::NUMERIC/u.unit_price, 0) AS calculated_quantity
FROM orders AS o
LEFT JOIN unitary AS u
ON o.product_id = u.product_id
WHERE quantity IS NULL
)
SELECT calculated_quantity
FROM missing;