Table of Contents

References

This tutorial is based on the book "SQL Practice Problems"

Imports

Load the database

Get pandas dataframes

PySpark dataframes

Intermediate SQL Questions

20. Categories, and the total products in each category

For this problem, we’d like to see the total number of products in each category. Sort the results by the total number of products, in descending order.

21. Total customers per country/city

In the Customers table, show the total number of customers per Country and City.

22. Products that need reordering

What products do we have in our inventory that should be reordered? For now, just use the fields UnitsInStock and ReorderLevel, where UnitsInStock is less than the ReorderLevel, ignoring the fields UnitsOnOrder and Discontinued. Order the results by ProductID.

23. Products that need reordering, continued

Now we need to incorporate these fields— UnitsInStock, UnitsOnOrder, ReorderLevel, Discontinued—into our calculation. We’ll define “products that need reordering” with the following:

24. Customer list by region

A salesperson for Northwind is going on a business trip to visit customers, and would like to see a list of all customers, sorted by region, alphabetically. However, he wants the customers with no region (null in the Region field) to be at the end, instead of at the top, where you’d normally find the null values. Within the same region, companies should be sorted by CustomerID.

25. High freight charges

Some of the countries we ship to have very high freight charges. We'd like to investigate some more shipping options for our customers, to be able to offer them lower freight charges. Return the three ship countries with the highest average freight overall, in descending order by average freight.

26. High freight charges - 1997

We're continuing on the question above on high freight charges. Now, instead of using all the orders we have, we only want to see orders from the year 1997.

27. High freight charges with between

28. High freight charges - last year

We're continuing to work on high freight charges. We now want to get the three ship countries with the highest average freight charges. But instead of filtering for a particular year, we want to use the last 12 months of order data, using as the end date the last OrderDate in Orders.

29. Inventory list

We're doing inventory, and need to show information like the below, for all orders. Sort by OrderID and Product ID.

Expected Results:

EmployeeID LastName OrderID ProductName Quantity

Look at the entity relation:

30. Customers with no orders

There are some customers who have never actually placed an order. Show these customers.

31. Customers with no orders for EmployeeID 4

One employee (Margaret Peacock, EmployeeID 4) has placed the most orders. However, there are some customers who've never placed an order with her. Show only those customers who have never placed an order with her.

Time Taken