Read this chapter and take notes on the purpose and use of the clause SELECT and how the INSERT and DELETE statements are used to make changes to data. Also, note how functions are used in these statements.
SELECT Statement
The SELECT statement, or command, allows the user to extract data from tables, based on specific criteria. It is processed according to the following sequence:
SELECT DISTINCT item(s)
FROM table(s)
WHERE predicate
GROUP BY field(s)
ORDER BY fields
We can use the SELECT statement to generate an employee phone list from the Employees table as follows:
SELECT FirstName, LastName, phone
FROM Employees
ORDER BY LastName
This action will display employee's last name, first name, and phone number from the Employees table, seen in Table 16.1.
Last Name | First Name | Phone Number |
Hagans | Jim | 604-232-3232 |
Wong | Bruce | 604-244-2322 |
Table 16.1. Employees table.
In this next example, we will use a Publishers table (Table 16.2). (You will notice that Canada is mispelled in the Publisher Country field for Example Publishing and ABC Publishing. To correct mispelling, use the UPDATE statement to standardize the country field to Canada – see UPDATE statement later in this chapter.)
Publisher Name | Publisher City | Publisher Province | Publisher Country |
Acme Publishing | Vancouver | BC | Canada |
Example Publishing | Edmonton | AB | Canada |
ABC Publishing | Toronto | ON | Canda |
Table 16.2. Publishers table.
If you add the publisher's name and city, you would use the SELECT statement followed by the fields name separated by a comma:
SELECT PubName, city
FROM Publishers
This action will display the publisher's name and city from the Publishers table.
If you just want the publisher's name under the display name city, you would use the SELECT statement with no comma separating pub_name and city:
SELECT PubName city
FROM Publishers
Performing this action will display only the pub_name from the Publishers table with a "city" heading. If you do not include the comma, SQL Server assumes you want a new column name for pub_name.