SQL SELECT


SQL SELECT is the first thing we are going to learn in our simple SQL tutorial. Before attempting to modify, insert or delete data from a database table, it’s a good idea to be able to read data first.

The SELECT clause is used to retrieve information from database table(s). Lets have a look at a very simple SQL SELECT statement selecting data from a database table called Cars, as this is the easiest way to understand how it works.

CarMakeModelYearColor
ToyotaCamry XLE2005Silver
HondaAccord EX2002Black
LexusES 3502008Silver

Above you see all entries in the table Cars and below is a simple statement that retrieves all these entries:

SELECT CarMake, Model, Year, Color
FROM Cars;

The SQL statement starts with the SELECT keyword, followed by a comma-separated list of columns. Then we have the FROM SQL keyword, which simply specifies which table the columns belong to. This instructs the SQL interpreter to select all values for the listed columns from the table Cars.

In the example above we requested to list all columns, and there is a short-cut statement that does exactly the same:

SELECT *
FROM Cars;

The * simply means that you require all columns in the table to be returned.

You can receive any number of columns from the table, in any order you want. For example the following SQL statement retrieves only the CarMake column:

SELECT CarMake
FROM Cars;

Here is the result:

CarMake
Toyota
Honda
Lexus

Here is another example retrieving Color and the CarMake (in this order) only.

SELECT Color, CarMake
FROM Cars;

And the result looks like this:

ColorCarMake
SilverToyota
BlackHonda
SilverLexus

The SELECT examples above are vary basic, and if you continue reading our SQL tutorial, you will learn how to use SQL SELECT clause along with WHERE clause, DISTINCT keyword, how to order the result of SELECT statement by using ORDER BY and more.



© SQLclauses.com 2010