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
United Kingdom | Advent Computer Training
Are you stuck in a dead end job? Do you want to take control of your salary, life and career? Advent IT and computer training offers advanced, professional training and helps you find the right ... more >
Hertfordshire, United Kingdom | Tesco.com
Solution Architect Lead Hertfordshire Who's behind the world's most successful online retailer?Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales at just under £1 billion. We ... more >
Berkshire, Reading, United Kingdom | Foster Wheeler
Sharepoint Administrator - Competitive Salary - Reading Foster Wheeler is a leading international project management, engineering and construction organisation with global construction capabilities working on major projects within upstream oil & gas, midstream & LNG, ... more >
Welwyn Garden City, Hertfordshire, United Kingdom | Tesco.com
Middle Tier solution Designer - Welwyn Garden CityWho's behind the world's most successful online retailer? Just over 10 years ago we started Tesco.com (aka Dotcom). Today, we've an incredible 750,000 active customers and sales at ... more >
More job opportunities