AI Reports

Magento 2 SQL queries › Stock

Which products have fewer than 5 units in stock?

Short answer: The Low Stock report uses each product's “notify for quantity below” setting. To use one threshold for everything, query the stock table.

In the Magento admin

  1. Go to Reports › Products › Low Stock. It lists products under their own Notify for Quantity Below value, not a number you choose.

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, si.is_in_stock
FROM cataloginventory_stock_item si
JOIN catalog_product_entity e ON e.entity_id = si.product_id
WHERE e.type_id IN ('simple', 'virtual')
  AND si.qty < 5
  AND (si.manage_stock = 1 OR si.use_config_manage_stock = 1)
ORDER BY si.qty, e.sku;

Adobe Commerce

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

SELECT e.sku, si.qty, si.is_in_stock
FROM cataloginventory_stock_item si
JOIN catalog_product_entity e ON e.entity_id = si.product_id
WHERE e.type_id IN ('simple', 'virtual')
  AND si.qty < 5
  AND (si.manage_stock = 1 OR si.use_config_manage_stock = 1)
  AND e.created_in <= UNIX_TIMESTAMP() AND e.updated_in > UNIX_TIMESTAMP()
ORDER BY si.qty, e.sku;

With multi-source inventory, the quantity per warehouse is in inventory_source_item:

SELECT sku, source_code, quantity, status
FROM inventory_source_item
WHERE quantity < 5
ORDER BY quantity, sku;

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