Home » Computers » Software

Learn about Subqueries in Access Courses

Nov 12, 2007
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.
About the Author
Author is a freelance copywriter. For more information on Access courses, please visit http://www.microsofttraining.net
Rating:
Please Rate:
(Average: Not rated)
Views: 145
Print Email Report Share
Article Categories
    • Artists
    • Gambling
    • Humanities
    • Humor
    • Movies
    • Music
    • Photography
    • Tattoos
    • Television
    • Classic Cars
    • Motorcycles
    • Recreational Vehicles
    • SUVs
    • Trucks
    • Vans
    • Branding
    • Business Opportunities
    • Careers and Jobs
    • Corporate
    • Customer Service
    • Direct Mail
    • Entrepreneurship
    • Ethics
    • Financing
    • Franchising
    • Home-Based Business
    • Human Resources
    • Import and Export
    • Leadership
    • Management
    • Market Research
    • Marketing and Advertising
    • Negotiation
    • Network Marketing
    • Networking
    • Organizational
    • Presentation
    • Project Management
    • Public Relations
    • Small Business
    • Strategic Planning
    • Team Building
    • Telemarketing
    • Training
    • Ask an Expert
    • College and University
    • Home Schooling
    • K-12
    • Languages
    • Online Education
    • Psychology
    • Accounting
    • Credit
    • Currency Trading
    • Debt Consolidation
    • Insurance
    • Investing
    • Leasing
    • Loans
    • Mortgage
    • Mutual Funds
    • Personal Finance
    • Stock Market
    • Structured Settlements
    • Taxes
    • Wealth Building
    • Coffee
    • Cooking
    • Gourmet
    • Recipes
    • Wine and Spirits
    • Acne
    • Aerobics
    • Alternative Medicine
    • Beauty
    • Cancer
    • Cosmetics
    • Depression
    • Diabetes
    • Diseases and Conditions
    • Fitness Equipment
    • Fitness
    • Hair Loss
    • Heart Disease
    • Medicine
    • Men's Health
    • Muscle Building
    • Nutrition
    • Skin Care
    • Supplements and Vitamins
    • Weight Loss
    • Women's Health
    • Yoga
    • Arts and Crafts
    • Babies
    • Collecting
    • Elderly Care
    • Genealogy
    • Hobbies
    • Parenting
    • Pets
    • Pregnancy
    • Woodworking
    • Feng Shui
    • Gardening
    • Home Appliances
    • Home Security
    • Interior Design
    • Landscaping
    • Affiliate Programs
    • Article Marketing
    • Auctions
    • Audio
    • Banner Advertising
    • Blogging
    • Broadband
    • Domain Names
    • E-Books
    • E-Commerce
    • Email Marketing
    • Ezines and Newsletters
    • Forums
    • Internet Marketing
    • Link Popularity
    • Pay-Per-Click
    • Podcasting
    • RSS
    • Search Engine Marketing
    • Search Engine Optimization
    • Security
    • Social Media
    • Spam
    • Video
    • Viral Marketing
    • Web Design
    • Web Development
    • Web Hosting
    • Copyright
    • Cyber Law
    • Intellectual Property
    • National, State, Local
    • Patents
    • Regulatory Compliance
    • Trademarks
    • Buying
    • Selling
    • Baseball
    • Basketball
    • Boating
    • Cycling
    • Extreme Sports
    • Fishing
    • Football
    • Golf
    • Hockey
    • Hunting
    • Martial Arts
    • Running
    • Scuba Diving
    • Soccer
    • Swimming
    • Tennis
    • Dating
    • Divorce
    • Marriage
    • Weddings
    • Astrology
    • Buddhism
    • Christianity
    • Faith
    • Hinduism
    • Islam
    • Judaism
    • Meditation
    • Metaphysical
    • New Age
    • Cable and Satellite TV
    • Cell Phones
    • Communication
    • Gadgets and Gizmos
    • GPS
    • Satellite Radio
    • Video Conferencing
    • VoIP
    • Addictions
    • Coaching
    • Goal Setting
    • Motivational
    • Stress Management
    • Time Management
    • Clothing
    • Electronics
    • Fashion
    • Gifts
    • Jewelry
    • Causes and Organizations
    • Environment
    • History
    • Holidays
    • Men's Issues
    • Nature
    • Philosophy
    • Politics
    • Women's Issues
    • World Affairs
    • Air Travel
    • Camping
    • Cruises
    • Destinations
    • Outdoors
    • Article Writing
    • Book Reviews
    • Copywriting
    • Fiction
    • Non-Fiction
    • Poetry
    • Quotes
    • Screenplay
    • Tools and Resources