|
|
|||
|
| |||
Part 2 Beginning SQL Programming Chapter 4: SQL Syntax and SELECT June 18, 2001
Suppose we have been asked to produce a report of the customer ID and name of all customers from the state of Washington. 1. We know that we need the Customers table as the basis for our query. We also know that we need the ID and name fields (CustomerID and CompanyName). With this information we can write the base query: SELECT CustomerID, CompanyName FROM CustomersThis query returns all of the customers in the table. 1. Now, we want to filter our data so that we only display the Washington customers. We will need to filter on the Region field in the database for all values of 'WA'. Since we are looking for an exact match, we should use the equals operator in our WHERE clause. With this information, we can now finish our query:
SELECT CustomerID, CompanyName FROM Customers WHERE Region = 'WA'When we execute this query we get our filtered result set:
How it Works When we use the WHERE clause in a query, we instruct SQL to filter the records based on the criteria in the WHERE clause. In our example, we are telling the query to filter for all of the records where the Region field contains only the string 'WA'. All other rows will be ignored. Each condition in a WHERE clause acts as a filter on the records from a field. WHERE clauses can be as complex as we need them to be, and we will be exploring their use throughout the book. Following are some errors and error messages you may come across when constructing some of these statements:
![]() A NULL value in SQL represents an unknown entity. This is significantly different from an empty value. An empty value tells us that we know that there is no data, but a NULL value means that we don't know what value should be in the field, thus NULL is considered to be an unknown value. NULL values are treated as special items in SQL. We can see this in the following simple comparison between NULL and empty in SQL Server. First, let's test to see if an empty value equals another empty value:
IF '' = '' PRINT 'TRUE' ELSE PRINT 'FALSE'The result in query analyzer is:
TRUENow, if we do the same test with NULL values:
IF NULL = NULL PRINT 'TRUE' ELSE PRINT 'FALSE'We get in the result window:
FALSESo while an empty value is the same as another empty value, a NULL is not the same as another NULL value, because NULL means 'unknown value'; two unknown values will not necessarily be the same! Keep this definition in mind during all of the SQL queries that we write. NULL values are special and are treated differently from other values. The WHERE clause has a special condition just for NULL values: IS NULL. Let's try using the IS NULL operator with the WHERE clause. Try it Out - Working with NULL Values We would like to find a list of all of our customers that are not located in the US and Canada. 2. Remember the Region field that holds state information for only US and Canada customers? Well in the Northwind database, all other international customers have a NULL value in this field. Using this as the basis of our query, we know that we want the company name from the Customers table filtered where the Region field is NULL as this would output the names of customers outside the US and Canada. 2. Enter the following SQL statement:
SELECT CompanyName FROM Customers WHERE Region IS NULLThis will return the following results:
![]()
![]() | |||
|
PAGE: 1 I 2 I 3 I 4 I NEXT PAGE |
|||
















