| Lesson 8 || Use the SELECT statement |
| Objective || Create a SELECT statement to return the city for each of the rows in the customer table. |
Create SELECT Statement
The SELECT statement has the following overall basic syntax:
SELECT what FROM where
You can indicate that individual columns or all the columns should be returned.
To indicate all the columns, use the keyword ALL, or the asterisk, as we've done before.
The following is an example of a query that will return only the name fields from Customer Table:
SELECT lastname, firstname from Customer
This gives you just these two columns from the table, as shown below.
There is still more to learn about the SELECT statement, and we will examine advanced uses in the next module.
Then we will investigate how to narrow the results returned by the SELECT statement, and how you can sort the results.
The FROM clause lists the tables from which the database should pull data. Normally if the query pulls data from more than one table, the query either uses a JOIN or a WHERE clause to indicate how the records in the tables are related.
For example, the following statement selects information from the Orders and OrderItems tables.
It matches records from the two using a WHERE clause. That clause tells the database to associate Orders records with OrderItems records that have the same OrderId value.
SELECT * FROM Orders, OrderItems
WHERE Orders.OrderId = OrderItems.OrderId
Several different kinds of JOIN clauses perform roughly the same function as the previous WHERE clause.
They differ in how the database handles records in one table that have no corresponding records in the second table.
Select Statement - Exercise