Upcoming Events

Executive conference

Cloud Connect March 16-18

Comprehensive thought leadership for executives, IT professionals and developers. Topics include: the ROI, cost and economics of on-demand computing; Migration strategies to move from on-premise to cloud-based IT; Vertical cloud specialization, tailoring features and architectures to specific applications, industries, and customer ecosystems

More Events »

Subscribe to Newsletter

  • Keep up with all of the latest news and analysis on the fast-moving IT industry with Network Computing newsletters.
Sign Up
Netdesign Manual

Part 2

Beginning SQL Programming

Chapter 4: SQL Syntax and SELECT


June 18, 2001

Brought to you by:





Check It Out!

Here is part 2 of a 3-part chapter excerpt from the book Beginning SQL Programming by John Kauffman, Brian Matsik, Kevin Spencer, and Tom Walsh; ISBN 1861001800; published March 2001, 723 pages.

Filtering Data

We now know how to query a table for records using the SELECT statement. You may have noticed that, to this point, whenever we have run a query, we have returned all of the records in the table. If the table is small, say thirty records, then this may not be a big problem. But what if the table contains a million records? Running a query against such a table will take time and monopolize resources. We will probably still need some way to filter for the specific information that we want.

SQL provides a way for us to filter our data to produce smaller result sets. The WHERE clause is used in a SQL statement to specify that we want all of the records that fit specific criteria.

Syntactically, the WHERE clause comes after the FROM clause:

SELECT
 Field1, Field2, Field3, Fieldn
FROM
 Table
WHERE
 Condition
The condition statement, known as the search criteria, in the WHERE clause can include any logical operator, string comparison, or other type of criteria that we will look at in later chapters.

A very simple example of using a WHERE clause here where we want a list of all the employees that live in Washington.

The Customer table has a field called Region. This field is used for state information that only US and Canada customers use.
SELECT
 EmployeeID,
 LastName,
 FirstName
FROM
 Employees
WHERE
 Region = 'WA'
This would produce the following result set of Washington employees:

EmployeeID LastName FirstName
1 Davolio Nancy
2 Fuller Andrew
3 Leverling Janet
4 Peacock Margaret
8 Callahan Laura

(5 row(s) affected)

There are several comparison operators that you can use in search criteria. Below is a list of valid operators that we can use in the WHERE clause of a SQL statement:



*NULL is used to describe a value which is unknown, as in the gender of an unborn baby. We'll discuss this operator further a little later in the chapter.
Let's look at a practical example of using the WHERE clause. Suppose we have been asked to find the first and last name of everyone in the Employee table who was hired after 1/1/1994.

We already know how to construct the SQL statement to get the names of everyone:

SELECT FirstName, LastName, HireDate
FROM Employees
Now, let's add the WHERE clause to meet our hire date criteria. In order for this query to work properly we need to be sure that we use the proper operator. Since we are looking for everyone hired after 1/1/1994 we will use the greater than operator (>):

SELECT FirstName, LastName, HireDate
FROM Employees
WHERE HireDate > '1/1/1994'
Note that we need to enclose the date in single quotes. If we were working in another database such as Microsoft Access we might need to use other delimiters. Access, for instance, uses the pound symbol (#) for enclosing dates. Check with your individual provider to determine which delimiters you should use. If you're working in Oracle, this date format will not work. The single quotes are fine, but you will have to write '1-JAN-1994' as in the following example, as this is how Oracle recognizes dates:

SELECT FirstName, LastName, HireDate
FROM Employees
WHERE HireDate > '1-JAN-1994';
When we run this query, we will have fewer records returned than are actually in the table:



Remember that we need to enclose dates in single quotation marks in SQL Server and in the pounds (#) in Microsoft Access.


PAGE: 1 I 2 I 3 I 4 I NEXT PAGE
 

Best of the Web

Data deduplication: Declawing the clones

Data deduplication is emerging as a critically important new arrow in the storage administrator's quiver to answer hard questions about the increasing problem in storage growth costs.

Quick Read

Compression, Encryption, Deduplication, and Replication: Strange Bedfellows

One of the great ironies of storage technology is the inverse relationship between efficiency and security: Adding performance or reducing storage requirements almost always results in reducing the confidentiality, integrity, or availability of a system.

Quick Read

WAN Optimization Whitelists and Blacklists

Optimization is a fantastic way of saving money and creating really happy customers at the same time, but it doesn't work flawlessly for all applications.

Quick Read

WAN Optimization as a Managed Service: It's Not About the Cost

This insight examines how organizations outsourcing their WAN optimization initiatives to a third-party go about achieving their goals for application performance, reducing operational costs, and streamlining enterprise infrastructure.

Quick Read

  Sponsored Links

Premium Content

Data Centers Gone Wild
February 22, 2010

NWC


Salary

Video