Microsoft Access Tutorial: Executing a SELECT Statement

Kantus
In the previous tutorial, you saw how to execute a query in Microsoft Access using the wizard, and how you can see the SQL behind that query that was executed to show you the resulting data.

In that tutorial, the final SQL statement was "SELECT People.FirstName, People.LastName, People.Age, People.State FROM People;"

In this tutorial I will go into further detail about what that means and how you can write your own SQL Select statements like that one to show exactly the data you want. The advantage of using a SELECT in Access as opposed to the wizard from the previous tutorial is the SQL provides more flexibility and you are able to show exactly the data you want and are not limited to the options presented to you in the GUI wizard. This is just a basic overview of the simple SELECT Access query you can make.

What if you want to show all the fields and all the rows that exist in the table? This is the first query a beginner will learn, and should learn.

In the above query, there are four fields written between the word SELECT and FROM. Those four fields are the only ones going to be shown. However, what if we want to see all the fields in the table and don't know how many fields the table has? Then you can use the wildcard asterisk "*" to specify to Access that the SELECT Access query wants to show all the fields. An example using the above table would be:

SELECT * FROM People;

That will simply show all the fields and all the rows in the table called People.

Notice that the only difference between our new SELECT Access query and the previous one is that the text "People.FirstName, People.LastName, People.Age, People.State" has been replaced by the text "*". Both SELECT queries show you the same data.

You might be wondering why the Access wizard generated the much longer query and not the SELECT Access query that uses the asterisk. The reason is when you went through the wizard, you got to select which fields you wanted to show using the SELECT Access query.

Microsoft Access doesn't know how many fields you will select. If you had selected just one field for example ("People.FirstName"), then the query would be "SELECT People.FirstName FROM People;" This is the SELECT statement that Access would generate, and it would be correct. We cannot use the asterisk if we want to show just one field because the asterisk shows all of them.

Because of this, a SELECT Access query created by the Microsoft Access Query Wizard will put in the names of the individual fields even if you select all the available fields in the Access table.

Published by Kantus

I love writing short stories and humor articles, but tend to stick with topics that are discoverable by search engines and capable of spreading virally.  View profile

To comment, please sign in to your Yahoo! account, or sign up for a new account.