SQL SELECT Statement
The SQL SELECT Statement
The SELECT statement is used to select data from a table. The tabular result is stored in a result table (called the
result-set).
Syntax
SELECT column_name(s)
FROM table_name
|
Note: SQL statements are not case
sensitive. SELECT is the same as select.
SQL SELECT Example
To select the content of columns named "LastName" and "FirstName",
from the database table called "Persons", use
a SELECT
statement like this:
SELECT LastName,FirstName FROM Persons
|
The database table "Persons":
| LastName |
FirstName |
Address |
City |
| Hansen |
Ola |
Timoteivn 10 |
Sandnes |
| Svendson |
Tove |
Borgvn 23 |
Sandnes |
| Pettersen |
Kari |
Storgt 20 |
Stavanger |
The result
| LastName |
FirstName |
| Hansen |
Ola |
| Svendson |
Tove |
| Pettersen |
Kari |
Select All Columns
To select all columns from the "Persons" table, use a * symbol
instead of column names, like
this:
Result
| LastName |
FirstName |
Address |
City |
| Hansen |
Ola |
Timoteivn 10 |
Sandnes |
| Svendson |
Tove |
Borgvn 23 |
Sandnes |
| Pettersen |
Kari |
Storgt 20 |
Stavanger |
The Result Set
The result from a SQL query is stored in a result-set. Most database software systems allow navigation of the
result set with programming functions, like: Move-To-First-Record,
Get-Record-Content, Move-To-Next-Record, etc.
Programming functions like these are not a part of this tutorial. To learn about accessing data with function calls,
please visit
our ADO tutorial.
Semicolon after SQL Statements?
Semicolon is the standard way to separate each SQL statement in database systems that allow more than one
SQL statement to be executed in the same call to the server.
Some SQL tutorials end each SQL statement with a
semicolon. Is this necessary? We are using MS Access and SQL Server 2000 and we
do not have to put a semicolon after each SQL statement, but some database
programs force you to use it.
The SELECT DISTINCT Statement
The DISTINCT keyword is used to return only distinct (different) values.
The SELECT statement returns information from table columns. But what if we only want to select
distinct elements?
With SQL, all we need to do is to add a DISTINCT keyword to the SELECT
statement:
Syntax
SELECT DISTINCT column_name(s)
FROM table_name
|
Using the DISTINCT keyword
To select ALL values from the column named "Company" we use
a SELECT
statement like this:
SELECT Company FROM Orders
|
"Orders" table
| Company |
OrderNumber |
| Sega |
3412 |
| W3Schools |
2312 |
| Trio |
4678 |
| W3Schools |
6798 |
Result
| Company |
| Sega |
| W3Schools |
| Trio |
| W3Schools |
Note that "W3Schools" is listed twice in the result-set.
To select only DIFFERENT values from the column named "Company" we use
a SELECT
DISTINCT statement like this:
SELECT DISTINCT Company FROM Orders
|
Result:
| Company |
| Sega |
| W3Schools |
| Trio |
Now "W3Schools" is listed only once in the result-set.
The Ektron Intranet
lets you do everything you need to do on your corporate intranet and everything you want to do... all with just one application.
What can you do with the Ektron Intranet? |

|
Navigate through content, documents, assets, colleagues and workgroups quickly and intuitively with enterprise search |

|
Communicate with friends and colleagues with forums, message boards and corporate blogging using the new Social Networking Platform |

|
Promote collaboration among coworkers in your organization through project workspaces where others can efficiently find information and work together |

|
Personalize your company profile by bookmarking and organizing favorite content, uploading assets, posting photos, blogging, and more |

|
Interact with features like tagging, flagging, wikis and ratings found in the Web 2.0 Toolbox |
 |
Author/edit content, manage navigation, menus, audit trails, workflow and approvals with the best in breed Content Management |
|
|
|
|
See why there are 20,000+ Ektron integrations worldwide. Request an
INSTANT DEMO or download a
FREE TRIAL today. |
|