# SQL Business Queries

> 30+ SQL queries on a sample company database (sales, overtime, leave, profit). Found that the reference answer for one exam question was missing a JOIN, inflating sales 18–27×, and proved it with a Python script.

- Result: Caught a missing JOIN in the reference answer — sales inflated 18–27×
- Stack: SQL, Python

Practice and analysis for the Database course: the database has 17 tables — employees, departments, customers, products, orders, purchasing, overtime, leave and more — and the exercises answer business questions with SQL, such as which sales reps beat the threshold two years running, overtime pay by department, and profit from Taipei customers.

## Problem

Answer business questions about sales, staff and costs with SQL on a sample company database with 17 tables (employees, departments, customers, products, orders, purchasing, overtime, leave and more).

## Key decisions

1. **Break complex queries into CTEs (WITH)**: "Compute yearly sales first, then pick the people above the threshold in both years" becomes several steps, each of which can be checked on its own.
2. **When an answer didn't match, verify it with code instead of trusting the reference answer**: Parsing the database export and recalculating proves with numbers where the mistake is.

## Results

- Wrote 30+ analysis queries: yearly and quarterly sales, growth rates, overtime pay, perfect-attendance lists, product margins, the cost of slow-moving stock
- Found that a reference answer was missing a JOIN condition (every order line was cross-joined with all 20 products), inflating sales 18–27×; recalculating in Python, the version without the JOIN matched the reference answer exactly
- For another question, "salary bands", a script also showed that the reference answer's sort order differed from what the question asked

## Queries

- Sales: yearly, quarterly and monthly sales, growth rates, the top 15 customers, the best sales rep in each city
- Staff: overtime hours and pay by department, leave statistics, perfect-attendance lists, salary bands
- Costs: product margins, the cost of slow-moving stock, supplier purchasing rankings
- Syntax: multi-table JOINs, subqueries, CTEs (WITH), conditional sums with CASE WHEN, HAVING filters

## Finding

- The question: find the sales reps whose sales exceeded the threshold in both 2017 and 2018. The reference answer's threshold was 4 billion, but calculated correctly, even the top rep made only a little over 400 million a year
- The cause: the FROM clause listed four tables, but the WHERE clause was missing orderdetail.ProdId = product.ProdID, so every order line was cross-joined with all 20 products
- A Python script that parses the database export and recalculates showed the version without the JOIN matching the reference numbers exactly (0.00% difference), proving that was the cause
- Another question, "salary bands": it asked for sorting by department and name, but the reference answer was actually sorted by employee ID — also verified with a script

> Database course. The data is a sample company database provided by the course; the names in the screenshot are sample data, not real people.

---

Author: PINGWEI LI (李秉威, Li Ping-Wei)
Original page: https://kbs0830.com/en/projects/sql-analysis
