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:
 SELECT  column_name(s)
   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:

 SELECT  LastName, FirstName  FROM  Persons;

 LASTNAME                         FIRSTNAME
 ---------------------------      ---------------------------
 Hansen                           Ola
 Svendson                         Tove
 Pettersen                        Kari

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

 SELECT  *  FROM  Persons;

 P_ID        LASTNAME         FIRSTNAME         ADDRESS          CITY
 --------    -------------    --------------    -------------    -----------
 1           Hansen           Ola               Timoteivn 10     Sandnes
 2           Svendson         Tove              Borgvn 23        Sandnes
 3           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