Simple clear advice in plain English

Hands on: Taking a structured approach

Learn how a little SQL can go a very long way

OR is more
What if you wanted to see the rows for more than one person? It’s an oddity of the human language that we can express this requirement in two ways.

Show me all the rows for Bill AND Ted.
Show me all the rows for either Bill OR Ted.

In SQL we have only one option: we always use the word OR in this case. You’ll fnd the dbcxmas07.mbd database here.

We use AND when we want multiple conditions to be true for the same row.

As a general rule, AND queries reduce the number of rows returned (we no longer want all ‘Bill’s, we want only the ‘Bill Preston’s), while OR queries increase the number (all the ‘Bill’ plus all the ‘Ted’). ‘OR is more’ makes a handy mnemonic.

How a query works
It’s convenient to talk about queries somewhat anthropomorphically. Take our ‘find the Bill rows’ query above.

We’d say the query looks at each row, decides whether it contains Bill or not, and adds all the Bill rows to the answer table. Because most SELECT queries examine the contents of fields, this description is a good fit for the behaviour we see.

This way of thinking about the query is fine for most cases, but there are occasions when a more technical understanding can be useful.

WHERE is a reserved word (it has a specific meaning in SQL) and is always followed by an expression that can be evaluated to one of two states, true or false. When a query is run, the expression is evaluated for the truth or falsehood, for each row in the table.

The answer table contains only the rows where it evaluates to true. So the WHERE clause:
WHERE FirstName=”Bill”;
evaluates to true wherever the string “Bill” is found in the FirstName column, and these rows are included in the answer table. The remaining rows are not. By way of proof of this concept, take a look at the WHERE clause below:

SELECT FirstName, LastName
FROM People
WHERE (3<4);

The expression (3<4) always evaluates to true because three is always smaller than four and always will be. When the query is run, this expression is evaluated for every row and always, inevitably, returns true.

The answer table therefore will contain every row. The two following examples demonstrate how an understanding of query evaluation for truth can be turned to our advantage.

Article tags

Reader Comments

Parameters with dates

It is possible make the same with dates. (Access 2003) For example if the parameter is a date and is null what use a date like #01/01/2008# I tried but don't work Thanks and sorry for my english because i'm Spanish

Posted by Juan Antonio Gordo, 20 Oct 2008

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

r368-novatech-nfinity-1

Novatech Nfinity 2367 Plus

An ultrabook laptop at an incredibly low price

PC Pitstop PC Matic

PC Pitstop PC Matic utility software

Computer cleaning and anti-virus combined

Braebo Athena

Braebo Athena desktop computer

A small and cheap back-to-school desktop PC

Question & Answer

Q.Why are some of the keys on my keyboard doing strange...

> Read the answer

Q.Is my phone’s Bluetooth any use?

> Read the answer

Q.Can I switch boot drives so that I can work on older...

> Read the answer

Best deals on the web

img

Samsung RV520-A07

£359.98- Buy it now

img

Acer Aspire 5750G (LX.RXP02.019)

£399.99- Buy it now

img

Apple MacBook Pro (MD313B/A)

£904.37- Buy it now

Latest issue & subscription deals

Poll

Are you concerned about viruses that target mobile phones?

Jargon Buster

Computing terms explained in plain English

Virtual drive

A set of files seen by Windows as a separate hard disk.

Great shopping deals from Computeractive