Simple clear advice in plain English

Hands on: Using SQL sub-queries

Fine-tune your database queries, choose your Access version, and code maps

This part in our occasional SQL series introduces sub-queries.

Understanding sub-queries gives you a useful and adaptable technique for writing queries.

The WHERE clause, covered here, lets you select rows that meet certain conditions.

Sub-queries let you add further refinement to the selection. A sub-query is just an SQL statement that would run on its own but is nested within another SQL statement. Here’s an example:

SELECT FirstName, LastName, Dept
FROM SalesStaff
WHERE SPID IN
(SELECT SPID
FROM SalesStaff
WHERE Dept = ‘Horticulture’) ;

This is in the sample file DBCJuly08.MDB, which you can find here.

The file contains all the queries described in this column and each has a number for easy identification, for example Q02, shown here in brackets after the relevant query.

An SQL statement can have many sub-queries within it, which brings us into the realm of complex nested SQL. A great deal of complexity can be introduced, and long, complicated code can be (and has been) written.

In my opinion, very complex SQL is often undesirable for two reasons. First, it makes the code unreadable for anyone who has to understand, maintain or edit it later. Second, the query may run slowly. This is because the query optimiser can find it difficult to identify an optimised pathway through the operations contained in convoluted code. The result is that the query runs sluggishly.

Both of these issues are addressed by writing a series of simpler SQL statements with one calling another. Not only is it easier for human beings to see what the code is doing, debug it and edit it if necessary, but these simple statements won’t give the optimiser indigestion and the queries will fly.

This is a complex area and some people write very clever, complex SQL statements that are faster than a series of smaller ones. But such people are relatively rare.

People learn SQL more easily when it’s split into smaller chunks, making me more of a fan of this approach. Let’s write a sub-query: DBCJuly08.MDB is available in two versions ­ the Access 2007 version is called DBCJul08-07.MDB.

The first thing to note is that the sub-query is wrapped in brackets (the number refers to the query name in the sample database):

(SELECT SPID
FROM SalesStaff
WHERE Dept = ‘Horticulture’) ;
(Q02)

Article tags

Reader Comments

   

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

Hands on: Blasts from the past

Time doesn’t necessarily lessen the relevance of database solutions

Hands on: SQL conditions and operators

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

Hands on: Taking a structured approach

Learn how a little SQL can go a very long way

Question & Answer

Q.How do I store musician and other information about...

> Read the answer

Q.Why can't my browser find the website address I typed...

> Read the answer

Q.All updates have been downloaded, so why won't Windows...

> Read the answer

Best deals on the web

img

Apple MacBook Pro (MC724LL/A)

£999.99- Buy it now

img

Sony Vaio VPCF23P1E/B

£679.98- Buy it now

img

Samsung 300E5A-A01DX

£449.99- Buy it now

Great benefits for subscribers!

Most popular articles

Poll

Which is your preferred web browser

Jargon Buster

Computing terms explained in plain English

VoIP

Voice over IP. The routing of voice conversations over the internet, which is cheaper than the telephone...

Great shopping deals from Computeractive