Skip to main content

Retrieve the out-of-stock items in Magento 2, you can use the following SQL query

To retrieve the out-of-stock items in Magento 2, you can use the following SQL query. This query joins the cataloginventory_stock_status table with the catalog_product_entity table to get products that are out of stock:

 

SELECT 
    cpe.entity_id AS product_id,
    cpe.sku,
    cpei.value AS product_name,
    css.stock_status
FROM 
    cataloginventory_stock_status AS css
JOIN 
    catalog_product_entity AS cpe ON cpe.entity_id = css.product_id
JOIN 
    catalog_product_entity_varchar AS cpei ON cpei.entity_id = cpe.entity_id
    AND cpei.attribute_id = (
        SELECT attribute_id 
        FROM eav_attribute 
        WHERE attribute_code = 'name' AND entity_type_id = 4
    )
WHERE 
    css.stock_status = 0; -- 0 indicates 'out of stock'