Introduction to the Query: The SELECT Statement

To fully use the power of a relational database "Introduction to SQL," you need to communicate with it. The ultimate communication would be to turn to your computer and say, in a clear, distinct voice, "Show me all the left-handed, brown-eyed bean counters who have worked for this company for at least 10 years." A few of you may already be doing so (talking to your computer, not listing bean counters). Everyone else needs a more conventional way of retrieving information from the database. You can make this vital link through SQL's middle name, "Query."

The name Query is really a misnomer in this context. An SQL query is not necessarily a question to the database. It can be a command to do one of the following:

• Build or delete a table

• Insert, modify, or delete rows or fields

• Search several tables for information and return the results in a specific order

• Modify security information

A query can also be a simple question to the database. To use this powerful tool, you need to learn how to write an SQL query.

General Rules of Syntax

As you will find, syntax in SQL is quite flexible, although there are rules to follow as in any programming language. A simple query illustrates the basic syntax of an SQL select statement. Pay close attention to the case, spacing, and logical separation of the components of each query by SQL keywords.

SELECT NAME, STARTTERM, ENDTERM
FROM PRESIDENTS
WHERE NAME = 'LINCOLN';

I

In this example everything is capitalized, but it doesn't have to be. The preceding query would work just as well if it were written like this:

select name, startterm, endterm
from presidents
where name = 'LINCOLN';

Notice that LINCOLN appears in capital letters in both examples. Although actual SQL statements are not case sensitive, references to data in a database are. For instance, many companies store their data in uppercase. In the preceding example, assume that the column name stores its contents in uppercase. Therefore, a query searching for 'Lincoln' in the name column would not find any data to return. Check your implementation and/or company policies for any case requirements.

NOTE: Commands in SQL are not case sensitive.

Take another look at the sample query. Is there something magical in the spacing? Again the answer is no. The following code would work as well:

Select name, startterm, endterm from presidents where name = 'LINCOLN';

However, some regard for spacing and capitalization makes your statements much easier to read. It also makes your statements much easier to maintain when they become a part of your project.

Another important feature of; (semicolon) semicolon (;) the sample query is the semicolon at the end of the -expression. This punctuation mark tells the command-line SQL program that your query is complete.

If the magic isn't in the capitalization or the format, then just which elements are important? The answer is keywords, or the words in SQL that are reserved as a part of syntax. (Depending on the SQL statement, a keyword can be either a mandatory element of the statement or optional.) The keywords in the current example are

• SELECT

• FROM

• WHERE

The Building Blocks of Data Retrieval: SELECT and FROM

As your experience with SQL grows, you will notice that you are typing the words SELECT and FROM more than any other words in the SQL vocabulary. They aren't as glamorous as CREATE or as ruthless as DROP, but they are indispensable to any conversation you hope to have with the computer concerning data retrieval. And isn't data retrieval the reason that you entered mountains of information into your very expensive database in the first place?

This discussion starts with SELECT because most of your statements will also start with SELECT:

SYNTAX:

SELECT COLUMN NAMES

The commands see also statements basic SELECT statement couldn't be simpler. However, SELECT does not work alone. If you typed just SELECT into your system, you might get the following response:

FROM TABLE

Together, the statements SELECT and FROM begin to unlock the power behind your database.

NOTE: keywords clauses at this point you may be wondering what the difference is between a keyword, a statement, and a clause. SQL keywords refer to individual SQL elements, such as SELECT and FROM. A clause is a part of an SQL statement; for example, SELECT column1, column2, is a clause. SQL clauses combine to form a complete SQL statement. For example, you can combine a SELECT clause and a FROM clause to write an SQL statement.

NOTE: Each implementation of SQL has a unique way of indicating errors. Microsoft Query, for example, says it can't show the query, leaving you to find the problem. Borland's Interface pops up a dialog box with the error. Personal Oracle7, the engine used in the preceding example, gives you an error number (so you can look up the detailed explanation in your manuals) and a short explanation of the problem.

Examples

Before going any further, look at the sample database that is the basis for the following examples. This database illustrates the basic functions of SELECT and FROM. In the real world you would use the techniques, "Manipulating Data," to build this database, but for the purpose of describing how to use SELECT and FROM, assume it already exists. This example uses the CHECKS table to retrieve information about checks that an individual has written.

Your First Query

SYNTAX:

SQL> select * from checks;

ANALYSIS:

This output looks just like the code in the example. The columns in the output statement are right-justified and are left-justified. This format follows the alignment convention in which numeric data types are right-justified and character data types are left-justified. The asterisk (*) in select * tells the database to return all the columns associated with the given table described in the FROM clause. The database determines the order in which to return the columns.

Terminating an SQL Statement

In some implementations of SQL, the semicolon at the end of the statement tells the interpreter that you are finished writing the query. For example, Oracle's SQL*PLUS won't execute the query until it finds a semicolon (or a slash). On the other hand, some implementations of SQL do not use the semicolon as a terminator. For example, Microsoft Query and Borland's ISQL don't require a terminator, because your query is typed in an edit box and executed when you push a button.

Changing the Order of the Columns

The preceding example of an SQL statement used the * to select all columns from a table, the order of their appearance in the output being determined by the database. To specify the order of the columns, you could type something like:

INPUT:

SQL> SELECT payee, remarks, amount, check# from checks;

Notice that each column name is listed in the SELECT clause. The order in which the columns are listed is the order in which they will appear in the output. Notice both the commas that separate the column names and the space between the final column name and the subsequent clause (in this case FROM).

INPUT:

SELECT payee, remarks, amount, check#
FROM checks;

Notice that the FROM clause has been carried over to the second line. This convention is a matter of personal taste when writing SQL code. The output would look like this:

ANALYSIS:

The output is identical because only the format of the statement changed. Now that you have established control over the order of the columns, you will be able to specify which columns you want to see.

Selecting Individual Columns

Suppose you do not want to see every column in the database. You used SELECT * to find out what information was available, and now you want to concentrate on the check number and the amount. You type

INPUT:

SQL> SELECT CHECK#, amount from checks;

ANALYSIS:

Now you have the columns you want to see. Notice the use of upper- and lowercase in the query. It did not affect the result.

What if you need information from a different table?

Selecting Different Tables

Suppose you had a table called DEPOSITS : You would simply change the FROM clause to the desired table and type the following statement:

INPUT:

SQL> select * from deposits

ANALYSIS:

With a single change you have a new data source.

Queries with Distinction

If you look at the original table, CHECKS, you see that some of the data repeats. For example, if you looked at the AMOUNT column using

INPUT:

SQL> select amount from checks;

Notice that the amount 150 is repeated. What if you wanted to see how may different amounts were in this column? Try this:

INPUT:

SQL> select DISTINCT amount from checks;

ANALYSIS:

Notice that only six rows are selected. Because you specified DISTINCT, only one instance of the duplicated data is shown, this means that one less row is returned. ALL is a keyword that is implied in the basic SELECT statement. You almost never see ALL because SELECT Table and SELECT ALL Table have the same result.

Try this example--for the first (and only!) time in your SQL career:

INPUT:

SQL> SELECT ALL AMOUNT
2> FROM CHECKS;

It is the same as a SELECT Column. Who needs the extra keystrokes?

Summary

The keywords SELECT and FROM enable the query to retrieve data. You can make a broad statement and include all tables with a SELECT * statement or you can rearrange or retrieve specific tables. The keyword DISTINCT limits the output so that you do not see duplicate values in a column.