Profit

Analyzing Business Data in SQL

Michel Semaan

Data Scientist

Recap

  • Revenue: The money a company makes
    • Multiply each meal's price times its ordered quantity, then sum the results
  • Cost: The money a company spends
    • Multiply each meal's cost times its stocked quantity, then sum the results
  • Profit = Revenue - Cost
Analyzing Business Data in SQL

Why is profit important?

  • Key Performance Indicator (KPI): A metric with some value that a company use to measure its performance

  • Profit per user: Identify the "best" users

  • Profit per meal: Identify the most profitable meals
  • Profit per month: Tracks profit over time
Analyzing Business Data in SQL

Revenue vs profit

meal_id  meal_price  order_quantity  revenue  cost  profit
-------  ----------  --------------  -------  ----  ------
21       8           100             800      500   300
22       5           80              400      80    320
  • Meal ID 21 has a higher price (8), ordered quantity (100), and revenue (800)
  • However, meal ID 22 brings in more profit (320) for Delivr
Analyzing Business Data in SQL

Bringing revenue and cost together

Query

WITH revenue AS (
  SELECT
    meals.meal_id,
    SUM(meal_price * meal_quantity) AS revenue
  FROM meals
  JOIN orders ON meals.meal_id = orders.meal_id
  GROUP BY meals.meal_id),

cost AS ( SELECT meals.meal_id, SUM(meal_cost * stocked_quantity) AS cost FROM meals JOIN stock ON meals.meal_id = stock.meal_id GROUP BY meals.meal_id)
Analyzing Business Data in SQL

Calculating profit

Query

WITH revenue AS (...),
  cost AS (...)

SELECT
  revenue.meal_id,
  revenue,
  cost,
  revenue - cost AS profit
FROM revenue
JOIN cost ON revenue.meal_id = cost.meal_id
ORDER BY profit DESC
LIMIT 3;

Results

meal_id  revenue   cost    profit    
-------  --------  ------  -------
11       17664.0   3072    14592.0   
10       16769.5   4573.5  12196.0   
8        13995.0   2332.5  11662.5   
Analyzing Business Data in SQL

Profit

Analyzing Business Data in SQL

Preparing Video For Download...