l.bol
l.bol

Reputation: 7

SQL LIKE clause not returning proper calues

I'm new to SQL, so despite searching here and on the Web i haven't been able to figure out my issue. I need to select the products that are tables or desks and that cost more than $300, everything else is correct, but I'm returning all dollar amounts, not just those more than $300.

here is the statement

SELECT ProductDescription, ProductFinish, ProductStandardPrice 
FROM product_t WHERE ProductDescription LIKE '%table%' OR ProductDescription LIKE '%desk%' 
AND ProductStandardPrice > 300;

Upvotes: 0

Views: 45

Answers (1)

shmosel
shmosel

Reputation: 50716

Despite your formatting, operator precedence means your query is interpreted like this:

SELECT ProductDescription, ProductFinish, ProductStandardPrice 
FROM product_t WHERE ProductDescription LIKE '%table%'
OR (ProductDescription LIKE '%desk%' AND ProductStandardPrice > 300);

If a product description contains "table", it will be returned regardless of the price. To always check the price, just insert the parentheses appropriately:

SELECT ProductDescription, ProductFinish, ProductStandardPrice 
FROM product_t WHERE (ProductDescription LIKE '%table%' OR ProductDescription LIKE '%desk%')
AND ProductStandardPrice > 300;

Upvotes: 1

Related Questions