1. Computing

Discuss in my forum

Retreiving Data with the SELECT Statement

By

The SELECT command is the most commonly used command in SQL. It allows database users to retrieve the specific information they desire from an operational database. In a moment, we’ll take a look at a few examples. First, we need to take a brief look at an example database table.

Imagine that we’re building a database of employees for our company. We might want this database to contain contact information, payroll details, job history, supervisor relationships, performance reviews and tons of other information. For this example, we’ll focus on a single table, called personal_info, which contains contact information for all of our employees.

The table might be set up with the following attributes:
  • employee_id
  • first_name
  • last_name
  • street_address
  • city
  • state
  • zip
  • phone
One of the first tasks we might face as the administrator of this database would be to simply retrieve all of the information it contains. This can be done with the command shown below:

SELECT *
FROM personal_info;

Note that the asterisk is used as a wildcard in SQL. This literally means "Select everything from the personal_info table."

Alternatively, users may want to limit the attributes that are retrieved from the database. For example, the Human Resources department may require a list of the last names of all employees in the company. The following SQL command would retrieve only that information:

SELECT last_name
FROM personal_info;

Finally, the WHERE clause can be used to limit the records that are retrieved to those that meet specified criteria. Suppose the CEO is planning to visit California and wants to make sure he is familiar with all of the employees living there. We might use the following command to get the information he needs:

SELECT *
FROM personal_info
WHERE state = ‘CA’;

We can even get a little more complicated by combining several of these concepts. Let’s imagine that the CEO is only visiting the Los Angeles office and isn’t interested in a great deal of detail. He just wants to know the names of all employees in the Los Angeles, CA office. We could use this SQL:

SELECT first_name, last_name
FROM personal_info
WHERE state=’CA’ and city=’Los Angeles’;

Those are the basics of the SELECT statement! You should now be able to retrieve information from a database with simple queries!

Learning More

If you would like to learn more about the Structured Query Language, read Introduction to SQL or sign up for our free Learning SQL e-mail course.
Related Video
Using Meta Data for Search
Export Data From an Excel Sheet to a Word Document
  1. About.com
  2. Computing
  3. Databases
  4. Learning SQL
  5. Basic SQL Tutorials
  6. Retrieving Data with the SELECT Statement

©2014 About.com. All rights reserved.