Publishers of technology books, eBooks, and videos for creative people

Home > Blogs > Confusing AND and OR, Five SQL Tips in Five Days, Part 2

Confusing AND and OR, Five SQL Tips in Five Days, Part 2

By  Jul 22, 2008

Topics: Productivity

It's easy to translate a correctly phrased spoken-language statement into an incorrect SQL statement.

If you say, "List the books priced less than $10 and more than $30," the and suggests the use of the AND operator:

SELECT title_name, price
  FROM titles
  WHERE price < 10 AND price > 30; --Wrong

This query returns no rows, however, because it's impossible for a book to be priced less than $10 and more than $30 simultaneously, as AND logic commands. The logical meaning of OR finds books that meet any of the criteria, not all the criteria at the same time:


Check back here each day this week for more SQL tips.

And for more on SQL, be sure to check out my book SQL: Visual QuickStart Guide, 3rd Edition.