SQL NULL Products

32 views Asked by At

I have to make it to show how many categories are available that are not currently any products in. Not sure what I am doing wrong, since I have moved stuff around and still get the result of 682 rows when there is supposed to be 0.

SELECT 
    Quantity, 
    ProductName, 
    CategoryID

FROM 
    Products,
    OrderItems 

WHERE NOT EXISTS (
    SELECT Quantity
    FROM OrderItems
    WHERE Quantity IS NULL
)

Was told that "NOT EXIST" needs to be used in it.

1

There are 1 answers

3
Gordon Linoff On

You need a join condition between the tables. First hint: Never use commas in the FROM clause. Always use proper, explicit JOIN syntax.

I assume you want something like this:

SELECT oi.Quantity, p.ProductName, p.CategoryID
FROM Products p LEFT JOIN
     OrderItems oi
     ON oi.ProductId = p.ProductId
WHERE oi.quantity IS NULL;

The exact syntax is a bit of a guess, because you don't provide sample data.