Databases and SQL - Retrieving Information From A Database
(Page 3 of 5 )
It wouldn't make sense to save information without the ability to retrieve it at some future date. The SELECT command is used to retrieve records from a table.
The SELECT statement is both powerful and flexible. But because this is an introductory tutorial, all of the nuances of the SELECT command won't be covered. I'll instead illustrate the basic usage of the command.
A simple form of the SELECT command is SELECT followed by the desired field to retrieve, FROM and then followed by the desired table. An asterisk can be used as shorthand to request all fields.
The results of such a query might resemble our initial table example.
Last Name First Name Address City State Zip Code --------------------------------------------------------------------- Adams Gwendolyn 205 W Third St. Brownville AL 35020 Johnson Diane 82 Richardson Ave. Fresno CA 93702 Smith Harold 321 Elm St. Portsmouth RI 02871 |
Of course returning every record might not be desired. The keyword WHERE may be used to set forth conditions to satisfy when selecting matching records.
SELECT * FROM addresses WHERE state = "CA"; |
Such a query would return only the records where the abbreviation for California appears in the state field.
Last Name First Name Address City State Zip Code ------------------------------------------------------------------ Johnson Diane 82 Richardson Ave. Fresno CA 93702 |
AND and OR may also be used to identify more than one field when constructing a WHERE condition.
SELECT * FROM employees WHERE first_name = "Diane" AND last_name = "Johnson"; |
Such a query would return only the records where the value of first_name is Diane and the value of last_name is Johnson.
ID First Name Last Name Date Hired Notes ----------------------------------------------------------------------- 189 Diane Johnson 2001-07-15 secretary of the year for 2003 |
As mentioned earlier, the asterisk is a shorthand request for all fields. Individual fields may be requested by listing them after SELECT. Commas separate multiple requested fields.
SELECT first_name, last_name FROM addresses; |
Such a query would return just the values of the first_name and last_name fields in the address table.
First Name Last Name --------------------- Gwendolyn Adams Diane Johnson Harold Smith |
Next: Changing Information In A Database >>
More Database Articles Articles
More By bluephoenix