Slide 7.7: SQL insert into statement (cont.)
Slide 7.9: SQL select distinct statement
Home

SQL Select Statement


The SELECT statement is used to select data from a database.

The result is stored in a result table (called the result-set). The syntax on the right is for the select statement:
1SELECT  column_name(s)
2  FROM  table_name

An SQL Select Example
Assume the table Persons is given as follows:

P_Id LastName FirstName Address City
1 Hansen Ola Timoteivn 10 Sandnes
2 Svendson Tove Borgvn 23 Sandnes
3 Pettersen Kari Storgt 20 Stavanger

To select the columns named LastName and FirstName, use a select statement like this:

1SELECT  LastName, FirstName  FROM  Persons;
2 
3LASTNAME                         FIRSTNAME
4---------------------------      ---------------------------
5Hansen                           Ola
6Svendson                         Tove
7Pettersen                        Kari

Select * Example
Now we want to select all the columns from the Persons table. We use the following select statement:

1SELECT  FROM  Persons;
2 
3P_ID        LASTNAME         FIRSTNAME         ADDRESS          CITY
4--------    -------------    --------------    -------------    -----------
51           Hansen           Ola               Timoteivn 10     Sandnes
62           Svendson         Tove              Borgvn 23        Sandnes
73           Pettersen        Kari              Storgt 20        Stavanger


Demonstration
The following is an SQL test area from W3Schools, which uses the well-known Northwind sample database and the tables are for read only.
For security reasons, the following demonstration may only work in Chrome.

SQL Statement:

Edit the SQL statement and click     to see the result, or    

Result:

Click “Run SQL” to execute the SQL statement above.
W3Schools has created an SQL database in your browser.
The menu to the right displays the database, and will reflect any changes.
Feel free to experiment with any SQL statement.
You can restore the database at any time.
The Database includes:
The Database includes:

TablenamesRecords
Customers91
Categories8
Employees10
OrderDetails518
Orders196
Products77
Shippers3
Suppliers29