AI Reports

Magento 2 SQL queries › Stock

Which in-stock products haven't sold in 90 days?

Short answer: Join stock with recent order lines and keep the products that have stock but no sales.

In the Magento admin

  1. Export stock and the Products Ordered report for 90 days, and compare them 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.

Magento Open Source

SELECT e.sku, si.qty
FROM cataloginventory_stock_item si
JOIN catalog_product_entity e ON e.entity_id = si.product_id
WHERE si.is_in_stock = 1
  AND si.qty > 0
  AND e.type_id IN ('simple', 'virtual')
  AND e.created_at < NOW() - INTERVAL 90 DAY
  AND NOT EXISTS (
    SELECT 1 FROM sales_order_item i
    JOIN sales_order o ON o.entity_id = i.order_id
    WHERE i.product_id = e.entity_id
      AND o.state <> 'canceled'
      AND o.created_at >= NOW() - INTERVAL 90 DAY
  )
ORDER BY si.qty DESC;

Adobe Commerce

Product, category, rule and CMS values are stored per version and joined on row_id (why):

SELECT e.sku, si.qty
FROM cataloginventory_stock_item si
JOIN catalog_product_entity e ON e.entity_id = si.product_id
WHERE si.is_in_stock = 1
  AND si.qty > 0
  AND e.type_id IN ('simple', 'virtual')
  AND e.created_at < NOW() - INTERVAL 90 DAY
  AND NOT EXISTS (
    SELECT 1 FROM sales_order_item i
    JOIN sales_order o ON o.entity_id = i.order_id
    WHERE i.product_id = e.entity_id
      AND o.state <> 'canceled'
      AND o.created_at >= NOW() - INTERVAL 90 DAY
  )
  AND e.created_in <= UNIX_TIMESTAMP() AND e.updated_in > UNIX_TIMESTAMP()
ORDER BY si.qty 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