Wednesday, June 20, 2007

WHERE Clause

Next, we might want to conditionally select the data from a table. For example, we may want to only retrieve stores with sales above $1,000. To do this, we use the WHERE keyword. The syntax is as follows:

SELECT "column_name"
FROM "table_name"
WHERE "condition"

For example, to select all stores with sales above $1,000 in Table Store_Information,
Table Store_Information


























store_name Sales Date




Los Angeles





$1500





Jan-05-1999





San Diego





$250





Jan-07-1999





Los Angeles





$300





Jan-08-1999

Boston



$700





Jan-08-1999




we key in,

SELECT store_name
FROM Store_Information
WHERE Sales > 1000

Result:





















Store





Total Sales





Los Angeles





$1800





San Diego





$250





Boston





$700

DISTINCT KEYWORD

The SELECT keyword allows us to grab all information from a column (or columns) on a table. This, of course, necessarily mean that there will be redundancies. What if we only want to select each DISTINCT element? This is easy to accomplish in SQL. All we need to do is to add DISTINCT after SELECT. The syntax is as follows:

SELECT DISTINCT "column_name"
FROM "table_name"
For example, to select all distinct stores in Table Store_Information,
Table Store_Information

store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999

we key in,

SELECT DISTINCT store_name FROM Store_Information
Result:

store_name
Los Angeles
San Diego
Boston

SELECT STATEMENT

SELECT "column_name" FROM "table_name"
To illustrate the above example, assume that we have the following table:
Table Store_Information

store_name Sales Date
Los Angeles $1500 Jan-05-1999
San Diego $250 Jan-07-1999
Los Angeles $300 Jan-08-1999
Boston $700 Jan-08-1999

We shall use this table as an example throughout the tutorial (this table will appear in all sections). To select all the stores in this table, we key in,
SELECT store_name FROM Store_Information

Result:

store_name
Los Angeles
San Diego
Los Angeles
Boston
Multiple column names can be selected, as well as multiple table names.

SQL Introduction

SQL (Structured Query Language) is a computer language aimed to store, manipulate, and retrieve data stored in relational databases. The first incarnation of SQL appeared in 1974, when a group in IBM developed the first prototype of a relational database. The first commercial relational database was released by Relational Software (later becoming Oracle).

Standards for SQL exist. However, the SQL that can be used on each one of the major RDBMS today is in different flavors. This is due to two reasons: 1) the SQL standard is fairly complex, and it is not practical to implement the entire standard, and 2) each database vendor needs a way to differentiate its product from others. In this tutorial, such differences are noted where appropriate.

This SQL programming help site lists commonly-used SQL statements, and is divided into the following sections:

>>SQL Commands: Basic SQL statements for storing, retrieving, and manipulating data in a relational database.
>>Table Manipulation: How SQL statements are used to manage tables inside the database.
>>Advanced SQL: Advanced SQL commands.
>>SQL Syntax: A single page that lists the syntax for all the SQL commands in this tutorial.

For each command, the SQL syntax will first be presented and explained, followed by an example. By the end of this tutorial, you should have a good general understanding of the SQL syntax, and be able to write SQL queries using the correct syntax. My experience is that understanding the basics of SQL is much easier than mastering all the intricacies of this database language, and I hope you will reach the same conclusion as well.

If you are interested in how to retrieve data using SQL, we recommend that you start with the SQL Commands section. If you are interested in understanding how SQL can be used to manipulate database tables, we recommend that you start with the Table Manipulation section.