SQL

This work sample demonstrates the ability to query a dataset using SQL. 

Dataset and environments:

Demonstrated skills:

  • SQL Clause and Sub Queries (SQL order or operation)
  • Joining Tables (Inner Join, Left Join, Right Join)
  • Operators (Logical operators, Comparison operators)
  • Functions (Aggregate, Numeric, Data and Time) 

Question No.1:

AdventureWorks is a large multinational fictious company focusing on bicycles. What are the sales by product category in United Kingdom between 2012 and 2013? 

Answer:

				
					Select

c.EnglishProductCategoryName as ProductCategory,
subc.EnglishProductSubcategoryName as ProdcutSubCategory,
Sum(fa.Salesamount) as Amount,
ROUND(Sum(fa.Salesamount)/(Select Sum(Salesamount) from FactInternetSales where SalesTerritoryKey = 10 AND Year(Orderdate) IN (2012,2013) ),2)as PercentofTotal

From FactInternetSales as fa
	LEFT JOIN DimProduct as prod
	On fa.ProductKey = prod.ProductKey
	LEFT JOIN DimProductSubcategory as subc
    On prod.ProductSubcategoryKey = subc.ProductSubcategoryKey
    LEFT JOIN DimProductCategory as c
    On subc.ProductCategoryKey = c.ProductCategoryKey

WHERE fa.SalesTerritoryKey = 10
AND Year(Orderdate) IN (2012,2013)

Group BY c.EnglishProductCategoryName, subc.EnglishProductSubcategoryName

ORDER BY Amount DESC
				
			

Note:

Line 3-4: Use original column name not an alias in Select statement due to the order of SQL operation

Line 6: Use SubQuery to get total sales amount

Line 6 & Line 10: SalesTerritoryKey for UK = 10 was queried beforehand by seperately querying DimmSalesTerritory Table

Line 8-14: Left Join from fact sales table to retrieve product category corresponding to each sales record.

Line 17: Year Function to filter for the selected year

Line 17: IN operator to simplify multiple OR operators

Line 17: AND Operator for multiple conditions

Line 19: Group BY statement to group by product category 

Query Result:

ProductCategoryProductSubCategoryAmountPercentofTotal
BikesRoad Bikes1165576.81750.41
BikesMountain Bikes1044330.63660.37
BikesTouring Bikes521644.890.18
AccessoriesHelmets27957.010.01
AccessoriesTires and Tubes24906.320.01
ClothingJerseys18718.370.01
AccessoriesBottles and Cages6522.840.00
AccessoriesBike Stands4293.000.00
AccessoriesHydration Packs3629.340.00
AccessoriesBike Racks3360.000.00
ClothingShorts3289.530.00
ClothingGloves3036.760.00
AccessoriesFenders2989.280.00
ClothingCaps2903.770.00
ClothingVests2413.000.00
AccessoriesCleaners588.300.00
ClothingSocks548.390.00

Question No.2:

Download full product catalog including  Model name and color with corresponding sales. 

Answer:

				
					Select 
prod.ProductKey,
prod.Englishproductname,
ISNULL(prod.ModelName,N'not_assigned') as ModelName,
prod.Color,
ISNULL(sum(fa.Salesamount),0) as Amount

From FactInternetSales as fa
	Right join DimProduct as prod
	On fa.ProductKey = prod.ProductKey
	
GROUP BY prod.ProductKey,prod.Englishproductname,prod.ModelName,prod.Color

ORDER BY Amount DESC
				
			

Note:

Line 4: ISNULL function to replace null with “not assigned” for Model name.

Line6: ISNULL function to replace null with “0” for Sales amount for products with no sales record. 

Line 8-10: Right join from fact sales table to retrieve full list of products including product with no sales record. 

Query Result: