The People table is used to demonstrate querying with conditions and operators
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: SQL conditions and operators

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

Mark Whitehorn, Personal Computer World 20 Mar 2008
ADVERTISEMENT

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.


All Software Applications
Tags: Databases, Sql, Software

Like this story? Spread the news by clicking below:

Post this to Delicious del.icio.us    Post this to Digg Digg this    Post this to reddit reddit!

Permalink for this story
R E A D E R   C O M M E N T S
M A R K E T P L A C E
Get your free demo of Numara Track-It! 8 - the leading help desk solution for IT related issues.
Make presentations, review documents & share your entire desktop. 30-day free trial! (cc required).
Discover how remote support can fuel your IT business in ways you've never thought of before.
Apply ITIL best practices at your service desk while eliminating integration cost. Learn more here.
WAN based, automated, daily vulnerability assessments. Click here to try and request our whitepapers.
Have your product or service listed here >   
Sponsored links
F E A T U R E D   J O B S
London, United Kingdom | InterSystems
ARCHITECT / DEVELOPER, London, Very Competitive £  OBJECT ORIENTED DEVELOPER / PROGRAMMER / ARCHITECT with strong OO (object oriented) development experience required by world leading global software provider to act as Senior Technical Consultants. InterSystems Corporation ... more >
Chichester, United Kingdom | West Sussex County Council
  Senior Application Specialist - Database Specialist, Chichester, £36,800 - £39,300 pa (includes a Market Rate Supplement) IT Services at WSCC supports and manages a variety of systems based on Oracle databases that include third party ... more >
United Kingdom | London Borough of Sutton
Business Relationship Manager (Finance), Based at Civic Offices, £ 41,790 - £ 44,373  (PO 7)   Fixed Term to 31st March 2009 The IT service has four Business Relationship Managers (BRM); each one responsible for delivering and developing ... more >
Birkenhead, United Kingdom | Crowder Consulting
Network Engineer / Data Analyst, Birkenhead, 18,000 - 23,000 Crowder Consulting are leading consultants in water and waste water network management.  We are specialists in the fields of leakage, hydraulic modelling, asset management, capital maintenance, technical ... more >
More job opportunities