The SQL SELECT clause selects data from one or more database tables and/or views.

In its basic form the SQL SELECT syntax looks like this: SELECT ColumnName1, ColumnName2, … FROM Table1 Let's have a look at the SELECT SQL statement above. The first part starts with the SELECT clause followed by a list of columns separated by commas. This list of columns defines which columns we are selecting data from. The second part of our SQL SELECT starts with the FROM clause followed by name of table from where we are extracting data.
We will use a table called Weather with 3 columns – City, Average Temperature and Date, to give a real world SQL SELECT example:

City New York Seattle Washington

AverageTemperature 22 C 21 C 20 C

Date 10/10/2005 10/10/2005 10/10/2005

To select all cities from the above table, we will use this SQL SELECT statement: SELECT City FROM Weather The result will be:

City New York Seattle Washington
If we want to select all the data (all columns) from the Weather table we can do it with the following SQL SELECT: SELECT * FROM Weather The * replaced the column list following the SELECT SQL clause, thus instructing the SQL interpreter to return all columns.

The SQL INSERT INTO clause facilitates the process of inserting data into a SQL table. Here is how you can insert a new row into the Weather table, using SQL INSERT INTO:

INSERT INTO Weather (City, AverageTemperature, Date) VALUES ('Los Angeles', 20, '10/10/2005')
The result of the execution of the SQL INSERT INTO above will look like this:

City New York Seattle Washington Los Angeles

AverageTemperature 22 C 21 C 20 C 20 C

Date 10/10/2005 10/10/2005 10/10/2005 10/10/2005

You can produce the same result, with a slightly modified SQL INSERT INTO syntax:

INSERT INTO Weather VALUES ('Los Angeles', 20, '10/10/2005')
You are allowed to omit the list of column names in the SQL INSERT INTO clause, if you enter values for each of the table columns. When using SQL INSERT INTO you might not want to enter values for all columns and in this case you have to specify the list of columns you are entering values for. If you do not enter values for all columns, then the columns you have omitted must allow NULL values or at least have a default value defined. The following SQL INSERT example enters only 2 of the 3 columns in the Weather table:

INSERT INTO Weather (City, Date) VALUES ('Boston', '10/10/2005')
The result of this SQL INSERT will be as follows:

City New York Seattle Washington Los Angeles Boston

AverageTemperature 22 C 21 C 20 C 20 C NULL

Date 10/10/2005 10/10/2005 10/10/2005 10/10/2005 10/10/2005

We've inserted a new row for Boston, but we haven't received the temperature value for 10/10/2005 that's why we didn't enter it.

The SQL WHERE clause works in conjunction with other SQL clauses like SELECT, INSERT and UPDATE to specify a search condition for these statements. We are going to give an example of the SQL WHERE clause used along with the SQL SELECT clause:

SELECT AverageTemperature FROM Weather WHERE City = 'New York'
The first 2 lines of the SELECT SQL statement above are already familiar from the previous chapter, but the 3rd line is new and specifies the SQL WHERE search condition:

WHERE City = 'New York'
If we translate the SQL statement to plain English, it would sound like this "Select the average temperature for the city of New York" The result of the added SQL WHERE condition will be:

AverageTemperature 22 C
We can have more than one search condition after the SQL WHERE clause. To illustrate this we will put a couple of more entries in the weather table:

City New York Seattle Washington New York Seattle Washington

AverageTemperature 22 C 21 C 20 C 18 C 20 C 17 C

Date 10/10/2005 10/10/2005 10/10/2005 10/09/2005 10/09/2005 10/09/2005

SELECT * FROM Weather WHERE AverageTemperature > = 20 AND City <> 'New York'
The first 2 lines of the above SQL expression instruct the SQL engine to retrieve all the columns from the Weather table. The 3rd line is the line that defines what conditions needs to be met for a row to be returned in our result set. This time we have 2 search conditions after the SQL WHERE clause – the first condition limits the rows to those with AverageTemperature greater or equal to 20 C and the second condition limits the rows to only those where the City column is different than 'New York'. As you may have noticed the 2 SQL WHERE conditions are concatenated with the AND SQL keyword, meaning that both conditions have to be met for all rows returned in the result set. The result of this SQL WHERE clause condition is below:

City Seattle

AverageTemperature 21 C

Date 10/10/2005

Washington Seattle

20 C 20 C

10/10/2005 10/09/2005

What will happen if we concatenate the 2 SQL WHERE search condition with the OR SQL keyword instead of the AND SQL keyword?

SELECT * FROM Weather WHERE AverageTemperature > = 20 OR City <> 'New York'

City New York Seattle Washington Seattle Washington

AverageTemperature 22 C 21 C 20 C 20 C 17 C

Date 10/10/2005 10/10/2005 10/10/2005 10/09/2005 10/09/2005

As you can see this time we have 2 more rows returned (in green) compared with the previous SQL WHERE clause results. Why has this happened? This time we have used the OR SQL keyword, which means that every row which satisfies at least one of the 2 SQL WHERE conditions will be returned. The first row doesn’t satisfy the condition for the City, but satisfy the average temperature condition that’s why it shows up in the final result set. The last row satisfy the City condition, but not the average temperature condition, and because we need to meet at least one of the 2 SQL WHERE conditions this rows is presented in the result set too.

The SQL DISTINCT clause works in conjunction with the SQL SELECT clause and selects only distinct (unique) data from a database table(s). Here is an example of SQL DISTINCT clause:

SELECT DISTINCT Column1 FROM Table1
As you can see the DISTINCT keyword goes immediately after the SELECT clause and is then followed by a list of one or more column names. I'll give you an example why you might need to use the DISTINCT SQL clause. I'll use the Weather table from the SQL WHERE tutorial to demonstrate the SQL DISTINCT application:

City New York Seattle Washington New York Seattle Washington

AverageTemperature 22 C 21 C 20 C 18 C 20 C 17 C

Date 10/10/2005 10/10/2005 10/10/2005 10/09/2005 10/09/2005 10/09/2005

Consider the following SQL statement utilizing SQL DISTINCT:

SELECT DISTINCT City FROM Weather
This SQL DISTINCT expression will return a list with all cities found in the City column of the Weather table, but it will remove the duplicates and leave only a single entry for each city:

City New York Seattle Washington
You can use the SQL DISTINCT with any table column for example with the AverageTemperature:

SELECT DISTINCT AverageTemperature FROM Weather
The result of this SQL DISTINCT will be:

AverageTemperature 22 C 21 C 20 C

18 C 17 C
You can use the SQL DISTINCT with more than one column and if you do that, the result will have all distinct combinations of values for all columns. For example if our Weather table has the following entries:

City New York New York New York New York New York

AverageTemperature 22 C 22 C 20 C 20 C 18 C

Date 10/10/2005 10/09/2005 10/08/2005 10/07/2005 10/06/2005

And we run the following SQL DISTINCT statement:

SELECT DISTINCT City, AverageTemperature FROM Weather
The result will be:

City New York New York New York

AverageTemperature 22 C 20 C 18 C

SQL UPDATE
The SQL UPDATE clause serves to update data in database table. The SQL UPDATE clause basic syntax looks like this:

UPDATE Table1 SET Column1 = Value1, Column2 = Value2, …
The first line of the above SQL UPDATE statement defines which table we are updating. The second line starts with the SET SQL keyword followed by one or more Column = Value pairs separated by commas. The second line of the UPDATE statement defines which table columns to update and with what value. Please consider the following SQL UPDATE syntax:

UPDATE Weather SET AverageTemperature = 20
Before we run this UPDATE SQL expression, our Weather table looks like this:

City New York Seattle Washington New York Seattle Washington

AverageTemperature 22 C 21 C 20 C 18 C 20 C 17 C

Date 10/10/2005 10/10/2005 10/10/2005 10/09/2005 10/09/2005 10/09/2005

After the update it looks like this:

City New York Seattle Washington New York Seattle Washington

AverageTemperature 20 C 20 C 20 C 20 C 20 C 20 C

Date 10/10/2005 10/10/2005 10/10/2005 10/09/2005 10/09/2005 10/09/2005

As you can see all values in the AverageTemperature column were set to 20. But what if we want to update (change) only the AverageTemperature values for New York? We can do that by using the UPDATE and the WHERE SQL clauses together:

UPDATE Weather SET AverageTemperature = 20 WHERE City = 'New York'
The result will be:

City New York Seattle Washington New York Seattle Washington

AverageTemperature 20 C 21 C 20 C 20 C 20 C 17 C

Date 10/10/2005 10/10/2005 10/10/2005 10/09/2005 10/09/2005 10/09/2005

In some cases you might want to UPDATE a column in a table, and make the new value of the column dependable on the old one. For example you might want to increase the AverageTemperature column values with 5 C for all entries in the table. To do this kind of SQL UPDATE you can use the following UPDATE statement:

UPDATE Weather SET AverageTemperature = AverageTemperature + 5
Our SQL UPDATE statement above simply instructs says that the new value of AverageTemperature will be equal to the old one plus 5. The SQL UPDATE clause is very powerful and you can easily alter one or more table entries by mistake, thus losing their original values. To avoid that make sure you update only the rows that you want, by utilizing the SQL WHERE clause. It's a good idea to make a backup of your table before running UPDATE statements for tables with important data.

SQL DELETE
The SQL DELETE clause is used to delete data from a database table. The simplest SQL DELETE syntax looks like this:

DELETE FROM Table1
The SQL DELETE statement above will delete all data from the Table1 table. Most of the time we will want to delete only table rows satisfying certain search criteria defined in the SQL WHERE clause. We will use the Weather table again to illustrate how to use SQL DELETE to delete a limited number of rows from a table:

City New York Seattle Washington New York Seattle Washington

AverageTemperature 22 C 21 C 20 C 18 C 20 C 17 C

Date 10/10/2005 10/10/2005 10/10/2005 10/09/2005 10/09/2005 10/09/2005

If we wanted to delete all rows containing Weather data for New York, we would use the following SQL DELETE statement:

DELETE FROM Weather WHERE City = 'New York'
Be extremely careful when using SQL DELETE, as you cannot restore data once you delete it from the table. You might want to make a backup of important data before performing delete on it.

SQL TRUNCATE TABLE
The SQL TRUNCATE TABLE clause deletes all rows from a database table. Here is the SQL TRUNCATE TABKE syntax:

TRUNCATE TABLE Weather
The SQL TRUNCATE TABLE clause does the same as a SQL DELETE clause which doesn't have a SQL WHERE clause. The following two SQL statements are equivalent:

TRUNCATE TABLE Weather DELETE FROM Weather
Use SQL TRUNCATE TABLE only when you want to delete all rows in a table.

SQL TRUNCATE TABLE

The SQL TRUNCATE TABLE clause deletes all rows from a database table. Here is the SQL TRUNCATE TABKE syntax:

TRUNCATE TABLE Weather
The SQL TRUNCATE TABLE clause does the same as a SQL DELETE clause which doesn't have a SQL WHERE clause. The following two SQL statements are equivalent:

TRUNCATE TABLE Weather DELETE FROM Weather
Use SQL TRUNCATE TABLE only when you want to delete all rows in a table

SQL ORDER BY
The SQL ORDER BY clause defines in what order to return a data set retrieved with a SQL SELECT statement. Here is an example of using SQL ORDER BY to order the rows in our Weather table by city:

SELECT * FROM Weather ORDER BY City
The result of using this SQL ORDER BY clause will be the following:

City New York New York Seattle Seattle Washington Washington

AverageTemperature 18 C 22 C 20 C 21 C 17 C 20 C

Date 10/09/2005 10/10/2005 10/09/2005 10/10/2005 10/09/2005 10/10/2005

As you can see we just ordered the data by the city column. You can order the result of a SQL SELECT query by more than one column, for example:

SELECT * FROM Weather ORDER BY City, Date
SQL table columns may have different data types like varchar (a string of characters), datetime, int, etc. and for each of those types the SQL ORDER BY clause has somewhat different behavior. When you use SQL ORDER BY with varchar for example the result will be ordered alphabetically. With the int type the result will be ordered from the low to high number and with the date from dates in the past to later dates. Of course there are other SQL data types that can be used with ORDER BY, but this goes beyond the scope of this tutorial. There are two SQL keywords that augment the SQL ORDER BY clause and define the direction of the ordering. The two keywords are ASC and DESC. When none of these two SQL keywords is specified after the ORDER BY SQL clause then it’s assumed that we are using ASC (ordering alphabetically and from low to high). The following two SQL ORDER BY statements are equivalent:

SELECT * FROM Weather ORDER BY City

SELECT * FROM Weather ORDER BY City ASC
Here is how to use SQL ORDER BY along with DESC keyword:

SELECT * FROM Weather ORDER BY City DESC
The result will be:

City Washington Washington Seattle Seattle New York New York

AverageTemperature 20 C 17 C 21 C 20 C 22 C 18 C

Date 10/10/2005 10/09/2005 10/10/2005 10/09/2005 10/10/2005 10/09/2005

When ordering by more than one column, you can specify different ordering direction for each column, for example:

SELECT * FROM Weather ORDER BY City ASC, Date DESC