WITH user_revenues AS (
  SELECT
    -- Select the user ID and revenue
    user_id,
    SUM (m.meal_price * o.order_quantity) AS revenue
  FROM meals AS m
  JOIN orders AS o
  ON m.meal_id = o.meal_id
  GROUP BY user_id)

SELECT
  -- Return the frequency table of revenues by user
  ROUND (revenue :: NUMERIC, -2) AS revenue_100,
  COUNT (DISTINCT user_id) AS users
FROM user_revenues
GROUP BY revenue_100
ORDER BY revenue_100 ASC;


SELECT
  -- Select the user ID and the count of orders
  user_id,
  COUNT (DISTINCT order_id) AS orders
FROM orders
GROUP BY user_id
ORDER BY user_id ASC
LIMIT 5;


WITH user_orders AS (
  SELECT
    user_id,
    COUNT(DISTINCT order_id) AS orders
  FROM orders
  GROUP BY user_id)

SELECT
  -- Return the frequency table of orders by user
  orders,
  COUNT (DISTINCT user_id) AS users
FROM user_orders
GROUP BY orders
ORDER BY orders ASC;
