Andrew Comeau ...
 Drewslair.com


 

 

 

Microsoft Access for Beginners - Additional Information

Understanding SQL - The Basics

In the chapter on queries, I briefly wrote about Structured Query Language (SQL) which Microsoft Access and other relational databases use to select and manipulate information.  SQL (often pronounced "sequel") is a scripting language resembling English that is used to relay instructions to the database and specify exactly what is to be done with the data.  Many Access users may think of a query as an object within the database window that can be run to retrieve data but the query is actually the SQL code within this object.  The Query Builder window that most Access users are familiar with is simply an interface that makes it easier to select tables and fields and specify what the query should do.  As you use this interface, Access writes the SQL code, referred to as a statement, in the background and saves it when you click the save button on the Query Builder interface.

In addition to these standalone queries, SQL statements can be directly used as the data source for forms, reports and controls within Access.  SQL statements can also be issued from macros and within VBA code in order to automate operations on data.  If you move beyond Access to databases like SQL Server, you will also see that SQL statements can be run from a command line to affect data being stored in a network database.  The following are examples of queries that you could actually run on the demonstration database and you might find them self-explanatory.

SELECT * FROM tblPersonnel WHERE LastName = "Ashford"

UPDATE tblDepartments SET DeptName = "Production" WHERE DeptNo = 014

DELETE * FROM tblAbsences WHERE EmpKey = 8

(The asterisk [*] is a wildcard symbol used to indicate all records matching the criteria in the statement.)

Many Access users will rely on the Query Builder interface to construct queries as needed.  While this may be all that you need when using Access, it's important to understand the basics of SQL if you actually want to understand how your database works.  Especially if you use other database systems, you will be much more efficient when it comes to retrieving and analyzing data if you can write at least basic queries rather than depending on whatever interface might be available at the time.  Despite it's appearance, SQL is a very simple language that you can learn the basics of in less than a day so there's really no reason to avoid it.

Behind the Scenes

By selecting SQL View instead of Datasheet view in the Query Builder, you can view and edit the SQL statement directly.  The following screenshots show a SELECT query on two of the tables in the demonstration database that displays records for employees on the first shift who have more than one absence recorded. 

Using the query window, you can easily build sophisticated queries while Access writes the SQL code in the background.

 SQL only looks complicated until you recognize the keywords and break it down accordingly.

At first, the SQL view looks like a confusing mess until you focus on the keywords that I've highlighted in red in the above picture.  Notice that all of these keywords are followed by table and field names.  Each of these keywords corresponds in some way to a part of the Query Builder window.

Access gets a little wordy when it writes the SQL so here's a simplified version of the query:

When processing the SQL statement, the program ignores extra whitespace and lines in the statement.  I formatted the below statement as I usually do for readability but I could also put everything in one continuous line or every word on a separate line and Access would read it just as easily.  I've also removed the table names from the code below to simplify things.  Specifying the table names before the field names often isn't necessary in queries unless you have the same field name in more than one table.

SELECT    Employee, LastName, FirstName, Count(AbsDate)
FROM      tblPersonnel
LEFT JOIN tblAbsences
ON        tblPersonnel.EmpKey = tblAbsences.EmpKey
WHERE     Shift = "1"
GROUP BY  Employee, LastName, FirstName
HAVING    Count(tblAbsences.AbsDate) > 1
ORDER BY  LastName;

Let's take a look at the individual parts:

The final results of the query.

Note that the semi-colon (;) at the end of the Order By clause is not part of that clause but actually indicates the end of the SQL statement.  This indicator is optional but if you include it and then continue the statement after it, you will not be able to run the query.

SQL contains many other statements, commands, operators and functions but the ones listed above will be enough to keep you from panicking when you view the SQL for a query. 

For more information:

SQL Pocket Guide (Pocket Reference (O'Reilly))
Jonathan Gennick

 

© 2010, Andrew Comeau, except where otherwise noted. Material contained on this website should not be republished without permission from the author. Questions?  Comments?  You can e-mail me at this address.

Microsoft is a registered trademark of Microsoft Corporation in the United States and other countries.