Magento 2 SQL queries › Orders and sales
Which countries did our orders come from last quarter?
Short answer: Countries live on the order addresses, not on the order. Join sales_order_address and use the shipping country, or the billing country for orders with nothing to ship.
In the Magento admin
- Go to Sales › Orders, add the Shipping Address column and filter by the quarter's dates.
- Export to CSV and count the countries in a spreadsheet.
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 COALESCE(sa.country_id, ba.country_id) AS country,
COUNT(*) AS orders,
ROUND(SUM(o.base_grand_total), 2) AS base_total
FROM sales_order o
LEFT JOIN sales_order_address sa ON sa.parent_id = o.entity_id AND sa.address_type = 'shipping'
LEFT JOIN sales_order_address ba ON ba.parent_id = o.entity_id AND ba.address_type = 'billing'
WHERE o.state <> 'canceled'
AND o.created_at >= MAKEDATE(YEAR(CURRENT_DATE - INTERVAL 1 QUARTER), 1)
+ INTERVAL QUARTER(CURRENT_DATE - INTERVAL 1 QUARTER) - 1 QUARTER
AND o.created_at < MAKEDATE(YEAR(CURRENT_DATE), 1) + INTERVAL QUARTER(CURRENT_DATE) - 1 QUARTER
GROUP BY country
ORDER BY orders DESC;
Good to know
country_idis the two-letter ISO code (GB, US, DE).- Each order has at most one shipping address; multi-address checkout creates one order per address, so nothing is counted twice.