How to Execute a Basic WHERE Clause SQL Query In Microsoft Access

Kantus
In this tutorial, I will demonstrate the basics of the SQL WHERE clause as it is used in a Microsoft Access query. The WHERE clause is used to filter out the results that show up after you execute a query.

For example if you have a table with 1000 entries and you wanted to filter out the results based on a certain criteria, you would use the WHERE clause in your SQL query.

Imagine that you have a table with 1000 entries of people's first names and details about those persons. What if you wanted to view only those records in this table, for people that have the first name of Jack (multiple people have the first name of Jack)? You would use a WHERE clause to specify this filter.

If you remember from the previous readings where I showed how to use the basic SELECT statement on our table called "People", the query to display all the data in the table is typically queried as:

SELECT * FROM People;

But since we want to see only people that have a first name of "Jack", the query would require the filter I mentioned above, called a WHERE clause and it would be written like this:

SELECT * FROM People WHERE FirstName = 'Jack';

This query will give us the name and details of all the people that have a first name of Jack. Literally translated to English, the above SQL statement that demonstrates the WHERE clause would be interpreted like this: "Select all data from the table called People where the field FirstName is equal to 'Jack'". Remember that our People table has a field called FirstName. This is the simplest form of the WHERE clause.

Here we are checking the first name of the people in the database table and seeing if any of them are equal to "Jack", and if they are, only then display the results. This will filter out the people that are not named Jack which is a result that you're looking for.

Another example would be to do the check on the age of the people in the database table. Of the 1000 entries we have in our table, we want to see only result for people that are older than 50 years. The query would then be this:

SELECT * FROM People WHERE Age > 50

Remember in our People table there is a field called Age. This will give us the people and their details only if their age is greater than 50, we will have filtered out people whose age is 50 or less and this is the result that we are looking for.

As you can see, the only difference between this SQL SELECT statement and the ones in the previous reading where I show the most basic SQL SELECT statement is the addition of the WHERE at the end. After the WHERE, is an expression. In our examples, this expression was "FirstName = 'Jack'" and in the second query, "Age > 50". Only rows where this expression is true will get shown from the SELECT statement. That is all there is to writing the basic WHERE clause in your Microsoft Access SQL statements.

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.