Database: Adventureworks
Pro-Tip – pay extra attention to:
GROUP BYandHAVING(Question 1)INNER JOIN(Question 2)LEFT JOINwithIS NULL(Question 3)- Question 1: Finding High-Value Customers (The Aggregate Test)
- Healthcare Connection: In a hospital, managers often need to see which patient groups or insurance models cost the most money.
- The SQL Goal: Write a query using the
FactInternetSalestable. Find the total amount of money spent (SalesAmount) by each customer (CustomerKey). Only show customers who have spent more than $5,000 total, and sort them from highest to lowest. - The Key Trick: Remember that you cannot use
WHEREto filter on a sum. You must useGROUP BYandHAVING. [1, 2]
Click to see the correct SQL Code
sql
SELECT
CustomerKey,
ROUND(SUM(SalesAmount), 2) AS TotalSpent
FROM FactInternetSales
GROUP BY CustomerKey
HAVING SUM(SalesAmount) > 5000
ORDER BY TotalSpent DESC;
Use code with caution.
Question 2: Combining Data Tables (The Inner Join Test)
- Healthcare Connection: As an analyst, you will constantly combine a data table (like patient visits) with a lookup table (like doctor names or medical codes).
- The SQL Goal: Connect the
FactInternetSalestable with theDimCustomertable. Pull the customer’s first name, last name, and the total number of orders they placed. - The Key Trick: Use an
INNER JOINonCustomerKeyand useCOUNT(SalesOrderNumber)to get the order totals. [1]
Click to see the correct SQL Code
sql
SELECT
CONCAT(C.FirstName, ' ', ISNULL(C.MiddleName, ''), C.LastName) AS FullName,
COUNT(FIS.SalesOrderNumber) AS TotalOrders
FROM FactInternetSales AS FIS
INNER JOIN DimCustomer AS C
ON FIS.CustomerKey = C.CustomerKey
GROUP BY C.FirstName, C.MiddleName, C.LastName
ORDER BY TotalOrders DESC;
Use code with caution.
Question 3: Finding Unsold Products (The Left Join Test)
- Healthcare Connection: Hospitals often look for “gaps in care”—such as identifying patients who have an HCC diagnosis code but have not had a doctor visit this year.
- The SQL Goal: Write a query to find all products in
DimProductthat have never been sold inFactInternetSales. - The Key Trick: Use a
LEFT JOINfrom the product table to the sales table. Look for rows where the sales table’s keyIS NULL. [1]
Click to see the correct SQL Code
Why Healthcare Analysts Prefer NOT EXISTS
There are three major reasons why the second method wins in a hospital or insurance setting:
- It is Much Safer (Handles NULLs Better): Healthcare databases are full of missing data (
NULLvalues). If you try to useNOT INor certain complexJOINs,NULLvalues can break your query or give you the wrong answer.NOT EXISTShandles missing healthcare data perfectly every time. - It Runs Much Faster (Performance): A hospital database has millions of patient rows. A
LEFT JOINforces the computer to look at every single row in both tables and match them up before filtering.NOT EXISTSis smarter. It stops looking the exact second it finds a match, saving a lot of computer memory. - It Reads Like Natural English:
NOT EXISTSreads like a sentence: “Show me the patient info WHERE a doctor visit DOES NOT EXIST.” It matches how a medical researcher thinks.
SELECT P.ProductKey,
P.EnglishProductName
FROM DimProduct AS P
LEFT JOIN FactInternetSales AS FIS
ON P.ProductKey = FIS.ProductKey
WHERE FIS.ProductKey IS NULL
ORDER BY P.ProductKey ASC;
SELECT P.ProductKey,
P.EnglishProductName
FROM DimProduct AS P
WHERE NOT EXISTS (
SELECT *
FROM FactInternetSales AS FIS
WHERE FIS.ProductKey = P.ProductKey
)
ORDER BY P.ProductKey ASC;