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
Select *
From (Select p.category, p.product_name, Round(CAST(Sum(o.sales)as numeric),2)as product_total_sales, Round(Cast(Sum(o.profit)as numeric), 2)As product_total_profit,
		Rank() Over(Partition By p.category Order By Sum(o.sales) desc)As product_rank
	From public.orders as o
	Inner Join public.products as p On o.product_id=p.product_id
	Group By p.category, p.product_name)as tmp
Where product_rank<6
Spinner
DataFrameas
salesperson_market_sales_details
variable
-- salesperson_market_sales_details
Select p.person, o.market, 
		Case When o.sales>0 and o.sales<100 Then '0-100'
			 When o.sales>=100 and o.sales<500 Then '100-500'
			 When o.sales>=500 Then '500+' End As sales_bin,
	Count(DISTINCT o.order_id)As order_counts, Count(ro.returned)as orders_returned,
	Sum(o.sales)As total_sales, Sum(Case When ro.returned is null Then 0 Else o.sales End)As returned_sales
From public.orders as o
Inner Join public.people as p On o.region=p.region
Left Join public.returned_orders as ro On o.order_id=ro.order_id and o.market=ro.market
Group By sales_bin, p.person, o.market
Order By p.person, o.market, sales_bin
Spinner
DataFrameas
impute_missing_values
variable
-- impute_missing_values
With missing As (Select product_id, discount, market, region, sales, quantity From public.orders Where public.orders.quantity is null),
	 unit_prices As (Select o.product_id, Cast(o.sales/o.quantity as numeric)as unit_price 
					 From public.orders as o right join missing as m On m.product_id=o.product_id and o.discount=m.discount Where o.quantity is not null) 

Select distinct m.*, Round(Cast(m.sales as numeric)/up.unit_price)As calculated_quantity
From missing as m Inner join unit_prices as up On m.product_id=up.product_id