Skip to content

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:

ColumnDefinitionData typeComments
row_idUnique Record IDINTEGER
order_idIdentifier for each order in tableTEXTConnects to order_id in returned_orders table
order_dateDate when order was placedTEXT
marketMarket order_id belongs toTEXT
regionRegion Customer belongs toTEXTConnects to region in people table
product_idIdentifier of Product boughtTEXTConnects to product_id in products table
salesTotal Sales Amount for the Line ItemDOUBLE PRECISION
quantityTotal Quantity for the Line ItemDOUBLE PRECISION
discountDiscount applied for the Line ItemDOUBLE PRECISION
profitTotal Profit earned on the Line ItemDOUBLE PRECISION

returned_orders:

ColumnDefinitionData type
returnedYes values for Order / Line Item ReturnedTEXT
order_idIdentifier for each order in tableTEXT
marketMarket order_id belongs toTEXT

people:

ColumnDefinitionData type
personName of Salesperson credited with OrderTEXT
regionRegion Salesperson in operating inTEXT

products:

ColumnDefinitionData type
product_idUnique Identifier for the ProductTEXT
categoryCategory Product belongs toTEXT
sub_categorySub Category Product belongs toTEXT
product_nameDetailed Name of the ProductTEXT

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!

Spinner
DataFrameas
top_five_products_each_category
variable
-- top_five_products_each_category
with prod_ord as (
select p.category, p.product_name, sum(ROUND(o.sales::numeric, 2)) as product_total_sales, sum(ROUND(o.profit::numeric, 2)) as product_total_profit
from orders o LEFT JOIN products p using (product_id)
group by p.category, p.product_name
),

ranked_prod_ord as (
select *, rank() over (partition by category order by product_total_sales desc) as product_rank
from prod_ord
order by category asc
),

top_five_products_each_category as(
select * 
from ranked_prod_ord
where product_rank <= 5
order by category asc)

select *
from top_five_products_each_category;
Spinner
DataFrameas
impute_missing_values
variable
-- impute_missing_values
with emp as (
    select *
    from orders
    where quantity IS NULL
),

--region Africa
afr as (
    select * 
    from orders
    where region = 'Africa'
),

--region EMEA
emea as (
	select *
	from orders
	where region = 'EMEA'
),

--region West
west as (
	select *
	from orders
	where region = 'West'
),


-- afs_c as(
-- select *,case when product_id = 'TEC-STA-10003330' and quantity is null then COALESCE(quantity, sales/253.32)
-- when product_id = 'TEC-STA-10004542' and quantity is null then COALESCE(quantity, sales/40.08) end as calculated_quantity
-- from afr 
-- where afr.product_id IN (select emp.product_id from emp)),

-- emea_c as (
-- select *,case when product_id = 'FUR-ADV-10000571' and quantity is null then COALESCE(quantity, sales/109.74)
-- when product_id = 'FUR-ADV-10004395' and quantity is null then COALESCE(quantity, sales/42.06) end as calculated_quantity
-- from emea 
-- where emea.product_id IN (select emp.product_id from emp)),

-- west_c as (
-- select *,case when product_id = 'FUR-BO-10001337' and quantity is null then COALESCE(quantity, sales/102.833) end as calculated_quantity
-- from west 
-- where west.product_id IN (select emp.product_id from emp))
impute_missing_values as (
select product_id, discount, market, region, sales, quantity,
	case when product_id = 'TEC-STA-10003330' and quantity is null then COALESCE(quantity, sales/253.32)
when product_id = 'TEC-STA-10004542' and quantity is null then COALESCE(quantity, sales/40.08)
when product_id = 'FUR-ADV-10000571' and quantity is null then COALESCE(quantity, sales/109.74)
when product_id = 'FUR-ADV-10004395' and quantity is null then COALESCE(quantity, sales/42.06)
when product_id = 'FUR-BO-10001337' and quantity is null then COALESCE(quantity, sales/102.833)
end as calculated_quantity
from emp)

select *
from impute_missing_values;