AI Reports

Magento 2 SQL queries › Orders and sales

How many orders used a coupon code last month, and which codes?

Short answer: The Coupons report does this after a statistics refresh. The query reads the code straight from each order, so it's always current.

In the Magento admin

  1. Refresh statistics in Reports › Statistics.
  2. Open Reports › Sales › Coupons and set the period to last month.

With one query

These queries only read data. Run them on a copy of your database if you can, or ask your developer to. If your tables have a prefix (set in app/etc/env.php), add it to each table name.

Works on Magento Open Source and Adobe Commerce:

SELECT o.coupon_code,
       COUNT(*) AS orders,
       ROUND(-SUM(o.base_discount_amount), 2) AS discount_given
FROM sales_order o
WHERE o.coupon_code IS NOT NULL
  AND o.state <> 'canceled'
  AND o.created_at >= DATE_FORMAT(CURRENT_DATE - INTERVAL 1 MONTH, '%Y-%m-01')
  AND o.created_at <  DATE_FORMAT(CURRENT_DATE, '%Y-%m-01')
GROUP BY o.coupon_code
ORDER BY orders DESC;

Good to know

Related

Gaurav Harsh

Gaurav Harsh is a Magento developer who works on large Adobe Commerce stores and builds AI Reports, a Magento extension for asking your store's data questions in plain English. LinkedIn