Screenshot from Access
This is the result from the sub-query when run on its own
R E L A T E D   C O N T E N T
ADVERTISEMENT

Hands on: Using SQL sub-queries

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

Mark Whitehorn, Personal Computer World 25 Jun 2008
ADVERTISEMENT

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)


All Software Applications
Tags: Hands On, Databases

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, Haringey, United Kingdom | Haringey Council
PMO Support Officer - Haringey, London - £32,289 - £37,542 pa   Experienced project support officer required by the internal IT services organisation of a London borough council to work within its Programme Management Office ... more >
Berkshire, Berkshire, United Kingdom | EDS
EDS are currently looking to recruit an experienced Core Infrastructure Project Manager to join our Project Management Defence team in one of the following locations: Reading or Bracknell (Berkshire) or Camberley (Surrey). Summary: Within DII ... more >
London, United Kingdom | MI5
Programme Managers - Project Managers -Project Support Staff - Competitive Salary + Excellent Benefits - London   Getting the best out of technology is critical to helping us protect the UK. Join MI5 and use ... more >
Leek Wootton, United Kingdom | Warwickshire Police
 IT Business Analyst - Leek Wootton, Warwickshire - £29,112 - £31,491 PA - 37 hrs per week   Everyone who works for Warwickshire Police helps to protect our communities from harm. Work with us and ... more >
More job opportunities