Learn About Subqueries in Access Courses

An understanding of queries is critical to database management. Without queries, you can’t get at the information and the data is useless.

Once you master queries, a subquery is a powerful tool that greatly expands your abilities to get exactly the data you need. To unleash the real power of a subquery, you need to become familiar with SQL (pronounced “sequel”), the language of database queries. This type of knowledge, usually taught in advanced level Access courses and can make your database exponentially more powerful.Subquery Basics

As you would learn in Access courses, a query in SQL is a SELECT statement. A subquery is simply a SELECT statement within a SELECT statement.

For example a simple query from our Access courses might read like this:

SELECT Employees.Name, Employees.Salary
FROM Employees;

This retrieves a list of employee names and their salaries from the Employees table.

A subquery from advanced Access courses could appear as such:

SELECT Employees.Name, Employees.Salary
FROM Employees
WHERE Employees.Salary IN
( SELECT TOP 5 Salary
FROM Employees AS Dupe
WHERE Dupe.Salary = Employees.Salary);

The main query pulls the same list of employees and their salaries. The subquery then takes from that list the five highest paid employees and puts them in a separate table aliased as Dupe.

Subqueries make it easy to perform such tasks as:

- Expressing values such as sales made in a given month as a percentage for all sales in the year

- List customers who haven’t ordered in 60 days or employees who haven’t made a recent sale

- Extract demographic data on customers from a specific state or city

- Calculate year to date totals

Problems With Subqueries

After learning how to create subqueries you will also learn some of the pitfalls to avoid.

Complex subqueries can perform quite slowly. If you find that performance is poor, try a stacked query instead. Make your first query then use the output table as the input table into the second query. Avoid nested subqueries (a query within a query within a query) as they can really drag down performance.

Subqueries that use the same table as the main query will require aliases, as demonstrated in the example above. The subquery uses the alias Dupe to make it clear it is referring to fields in the output of the main query and not in the main Employees table.

Sometimes Access just chokes on what seems to be a straightforward subquery. Verify the statement, confirming things like matching data types and that none of your names are on the reserved words list. Rewrite the query or, if necessary, break it into stacked queries.

Subqueries are incredibly powerful and flexible tools for data access but can also be a source of frustration and error if used incorrectly. Though they can be complicated, you may be amazed at how much more control you have over your data once you master this knowledge.

Author is a freelance copywriter. For more information on Access Courses, visit http://www.MicrosoftTraining.net/.

Article Source: http://EzineArticles.com/?expert=Christine_Harrell

A useful introductory article for SQL developers requiring the extra control and power to your Access database queries and ultimate reports. Nesting queries can slow the system down – that’s very true but to help mitigate and increase performance, consider adding indexes to selective fields too which speeds the query up.

Indexing is widely overlooked when designing an Access database and you need to set additional fields along side the primary key to help handle optimisation of your reports (generally known as secondary keys).

For a quick reference guide of the basic SQL language, take a look at http://www.about-access-databases.com/sql-query-syntax.html.

This entry was posted in Database Theory, MS Access, Queries, Reports and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>