DISTINCT — Used to display unique values in a column.
DISTINCTROW — Used to exclude records based on the entire duplicate records, not just duplicate fields.
ORDER BY — Used to sort retrieved records in descending or ascending order.
Query — A question or command posed to the database.
Query Design view — Enables you to create queries by select- ing table and column names and specifying conditions on the data you want to retrieve.
Result set — Records retrieved from the database.
SELECT statement — Used to retrieve records from the database.
TOP — Used to display records that fall at the top or bottom of a range that is specified by an ORDER BY clause.
TOP PERCENT — Used to display a percentage of records that fall at the top or bottom of a range that is specified by an ORDER BY clause.
In the grid below, select the Field1 field from the drop-down menu in the Table category. Refer to Figure 4-1.
Now, switch to SQL view, as shown in Figure 4-2.
To switch to SQL view, use the View button and select SQL View. (Click the down arrow located on the View button to find the SQL View option.)
Figure 4-1. Query Design view
Figure 4-2. SQL view
Note that the derived SQL statement consists of two parts.
First there is the SELECT part followed by the field we selected, then the FROM part showing which table was used for the select.
Look at the following syntax for the SELECT statement:
SELECT Columnname(s) FROM TableName(s);
The SELECT keyword is used by SQL to specify what data is desired from a table. The FROM keyword tells SQL what table the columns come from.
Ü
Note: When you view SQL script generated by Microsoft Access, the table name is shown with the column name. For example, in Figure 4-2 you see the following:SELECT TableOne.Field1
This technique is commonly referred to as qualification. To qualify a column, you must specify the table name (TableOne) and type a period, followed by the name of the column (Field1).
Ü
Note: TableOne was created in Example 1 of Chapter 3.Qualification is normally used when you are querying more than one table; therefore we will not use this technique unless we are querying multiple tables.
In this chapter, we will keep it simple by having every SELECT statement include one table after the FROM key- word. Later, in Chapter 8, we will expand on this concept and show how to query multiple tables.
A few syntax rules are important to remember. When you create a SELECT statement, every column name specified after the SELECT keyword must be separated by a comma.
Additionally, when you specify more than one table after the FROM keyword, all table names must also be separated by a comma.
Take a look at Example 1.
Example 1
Say you want to display the values stored in the ToyName and Price columns from the original Toys table in Figure 4-3. Type the following script:
SELECT ToyName, Price FROM Toys;
This script uses the SELECT keyword to specify the ToyName and Price columns from the Toys table. The FROM keyword is used to specify the name of the table (Toys) from which to retrieve records. The closing semicolon tells the DBMS where the query ends. Take a look at Figure 4-4, which shows the results from the query.
Example 1 illustrates how to display two columns from a table.
You can display single, multiple, or all columns from a table.
The order in which you place the column names after the SELECT keyword determines the order in which they will be
Figure 4-3. Toys table
Figure 4-4. Results (output)
displayed in the output or result set. Take a look at Example 2, which shows how to display every column from a table.
Example 2
Say you want to display every column from the Toys table in Figure 4-3. In Access Query Design view, you open up a query and move the first field line, the “*” (or Toys.* line), down to the query grid. Refer to Figure 4-5.
Shifting over to SQL view produces a corresponding SQL state- ment, as shown in Figure 4-6.
Figure 4-5. Query Design view
Look at the following script:
SELECT * FROM Toys;
This script combines an asterisk (*) with the SELECT key- word, which tells the DBMS to select every column from a table. The FROM keyword specifies the name of the table from which to retrieve records. Look at the results in Figure 4-7.
Ü
Note: Make sure you specify column names as they appear in the table. The space character is a delimiter character to SQL. Quite often an SQL statement will return an error because you have included a space in a column name. You think the two words are a single name, but SQL thinks other- wise. You can get around this ambiguity by surrounding the column name in brackets ([ ]).Ü
Note: Access does not perform any simplifying when it builds its SQL statements. Note in the example above that Access defines the field as table.field even though there is only one table and the table name is superfluous. Access SQL does a lot of this. For most of our examples we will be abbreviating the SQL statements. Both versions are processed the same way. (To try this, take out the table name from the SQL view, then con- vert it back to Design view. The same query will be presented.Unfortunately, if you now convert it back to SQL view you get the table name back.)
Another interesting point is that if you have the same field name in two tables that are used in a query (we will be getting to this soon) youmustspecify the table name. Otherwise Access gives you an error message.
Figure 4-7. Results (output)