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:
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:
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:
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
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!