The People table is used to demonstrate querying with conditions and operators

Hands on: SQL conditions and operators

We continue a tour of SQL querying and look at a solution to the HMRC’s woes

Written by Mark Whitehorn

As I said in a previous column, knowing some SQL gives a great boost to your querying skills, and so the theme continues here.

We’ve already met the WHERE clause, as in:
SELECT LastName, FirstName
FROM People
WHERE FirstName=”Bill”;
which returns anyone in the People table with the first name of Bill. The WHERE clause is called a ‘condition’: it defines the conditions under which we want rows returned. The equals sign that follows WHERE is an ‘operator’ and is one of many that can be used.

A condition is evaluated and the rows for which it evaluates as true are included in the answer table. Apart from using the = operator, I slipped one other into the previous column mentioned above:
SELECT FirstName, LastName
FROM People
WHERE (3<4);

In this query the condition is ‘where three is less than four’ and will always evaluate to true and therefore return all rows.

Given the table shown in the accompanying picture, we can find all people with more than three children:
SELECT FirstName, ChildNo
FROM People
WHERE ChildNo >3;
(Q01 – see below for an explanation of the numbers in brackets.)

This returns four rows. (The file DBCMAR08.MDB on the cover disc for the March 2008 issue of PCW contains all the queries here, each has a number (ie (Q01) shown after the query). To exclude people with six or more children, you’d alter the condition to:
WHERE ChildNo>3 AND ChildNo<6;
(Q02)
and two rows are returned. Alternatively you could use IN to identify the acceptable values, which returns the same two rows:
WHERE ChildNo IN (4,5);
(Q03)

IN also works with text:
WHERE HomeTown IN (‘London’,’Wigan’,’Bedford’);
(Q04)
finds people from these towns. For numbers of children of between two and four, it’ll be no surprise that you’d use the BETWEEN… AND operator:
WHERE ChildNo BETWEEN 2 AND 4;
(Q05)

Numbers of two, three and four are returned. This operator can also be used with text strings, for instance:
WHERE HomeTown BETWEEN 4
‘Edinburgh’ AND ‘London’;
(Q06)
returns all home towns that are alphabetically between, and including, Edinburgh and London. The six comparison operators work with numbers, text and dates, so to find all home towns after (alphabetically speaking) but not including Hereford you can use:
WHERE HomeTown>’Hereford’;
(Q07)
and this:
WHERE HomeTown>=’Hereford’;
(Q08)
includes Hereford, while this:
WHERE People.DateJoined>4
=#1/3/2004#;
(Q09)
will return all rows with join dates on or later than 1 March 2004.

reader comments

related articles

 

related whitepapers

today's top stories

Body Shop rolls out PCI system

Retailer hopes to benefit from improved customer data analysis 07 Oct 2008

Where to offshore (and why not here?)

Tholons, the research firm founded by well-known offshoring guru Avinash Vashistha , has just published some new research in Global Services magazine... 07 Oct 2008

The future of Ethernet

Where is Ethernet going? We look at the future of the widely-used networking technology. 07 Oct 2008

The pIT stop Q&A: How can I measure the business success of IT applications?

Ou expert panel answers readers' real-life IT questions 07 Oct 2008

National Identity Fraud Prevention Week

Every Monday seems to mark the beginning of a new awareness drive and this week’s theme has particular importance to small businesses... 06 Oct 2008

Advertisement

Newsletter signup

Sign up for our range of FREE newsletters:

Existing User

Newsletter user login:

Jobs

Related jobs

Job of the week

Job alerts

Sign up here

Find your next job

Advertisement

White papers

Search white papers

Top categories

VPN, Extranet and Intranet Solutions

WAN/ LAN Solutions

Network Security

Interoperability-Connectivity

Grid/ Utility Computing

Latest poll

Would you apply for a job that was advertised on Facebook or a similar social networking site?

Would you apply for a job that was advertised on Facebook or a similar social networking site?

The government is using Facebook to recruit IT staff - would you apply to such an ad?

Previous poll results

Latest audio and video articles

Ethernet cableVideo

The future of Ethernet

Where is Ethernet going? We look at the future of the widely-used networking technology. 07 Oct 2008

Podcast imageAudio

Computing podcast - Next-generation broadband Britain; and we report from Gartner's IT security summit

In our latest podcast, we discuss the hurdles that a national fibre-optic network must overcome, and look at the issues discussed at the recent IT security conference 02 Oct 2008

Latest in-depth articles

Features

How to ensure progress in programming

Best practice advice from Forrester Research 02 Oct 2008

BT workersAnalysis

Wanted: a viable model for fibre

While other European countries are pressing ahead with fibre rollouts, progress in the UK is being held back as the debate over who will foot the bill drags on, writes Dave Bailey 02 Oct 2008

Advertisement

Primary Navigation