For example, the following SELECT statement uses the IN operator to retrieve products with a ProductID of 1, 2, 5, 15, 20, 45, or 50: SELECT ProductID, ProductName, QuantityPerUnit, Unit
Trang 1Figure 3.6: Products where ProductName is like 'Cha%'
The next SELECT statement uses the LIKE operator to retrieve products where the ProductName column is like '[ABC]%':
SELECT ProductID, ProductName
FROM Products
WHERE ProductName LIKE '[ABC]%';
Figure 3.7 shows the results of this SELECT statement LIKE '[ABC]%' matches products with a name that starts with any of the letters A, B, or C, and ends with any number of characters
Figure 3.7: Products where ProductName is like '[ABC]%'
The next SELECT statement uses the LIKE operator to retrieve products where the ProductName column is like '[^ABC]%':
Trang 2SELECT ProductID, ProductName
FROM Products
WHERE ProductName LIKE '[^ABC]%';
Figure 3.8 shows the results of this SELECT statement LIKE '[^ABC]%' matches
products with names that don't start with any of the letters A, B, or C, and end with any number of characters
Figure 3.8: Products where ProductName is like '[^ABC]%'
The next SELECT statement uses the LIKE operator to retrieve products where the ProductName column is like '[A-E]%':
SELECT ProductID, ProductName
FROM Products
WHERE ProductName LIKE '[A-E]%';
Figure 3.9 shows the results of this SELECT statement LIKE '[A-E]%' matches products with names that start with any of the letters A through E, and end with any number of characters
Trang 3Figure 3.9: Products where ProductName is like '[A-E]%'
Specifying a List of Values
You use the IN operator in a WHERE clause to retrieve rows with columns that contain values in a specified list For example, the following SELECT statement uses the IN operator to retrieve products with a ProductID of 1, 2, 5, 15, 20, 45, or 50:
SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice
FROM Products
WHERE ProductID IN (1, 2, 5, 15, 20, 45, 50);
Here's another example that displays the OrderID column from the Orders table for the
rows where the CustomerID column is in the list retrieved by a subquery; the subquery
retrieves the CustomerID column from the Customers table where the CompanyName is like 'Fu%':
SELECT OrderID
FROM Orders
WHERE CustomerID IN (
SELECT CustomerID
FROM Customers
WHERE CompanyName LIKE 'Fu%'
);
The results of the subquery are used in the outer query
Specifying a Range of Values
Trang 4You use the BETWEEN operator in a WHERE clause to retrieve rows with columns that contain values in a specified range For example, the following SELECT statement uses the BETWEEN operator to retrieve products with a ProductID between 1 and 12:
SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice
FROM Products
WHERE ProductID BETWEEN 1 AND 12;
Here's another example that displays the OrderID column for the rows from the Orders table where the OrderDate is between '1996-07-04' and '1996-07-08':
SELECT OrderID
FROM Orders
WHERE OrderDate BETWEEN '1996-07-04' AND '1996-07-08';
Reversing the Meaning of an Operator
You use the NOT keyword with an operator in a WHERE clause to reverse the meaning
of that operator For example, the following SELECT statement uses the NOT keyword
to reverse the meaning of the BETWEEN operator:
SELECT ProductID, ProductName, QuantityPerUnit, UnitPrice
FROM Products
WHERE ProductID NOT BETWEEN 1 AND 12;
Note You can use the NOT keyword to reverse other operators, for example, NOT LIKE, NOT IN
Retrieving Rows with Columns Set to Null
Earlier, I mentioned that columns can contain null values A null value is different from a blank string or zero: A null value represents a value that hasn't been set, or is unknown You can use the IS NULL operator in a WHERE clause to determine if a column contains
a null value For example, the following SELECT statement uses the IS NULL operator
to retrieve customers where the Fax column contains a null value:
SELECT CustomerID, CompanyName, Fax
FROM Customers
WHERE Fax IS NULL;
Figure 3.10 shows the results of this SELECT statement
Trang 5Figure 3.10: Using the IS NULL operator to retrieve customers where Fax contains a null value
As you can see, null values are displayed as NULL in the Query Analyzer
Specifying Multiple Conditions
You can use the logical operators shown in Table 3.3 to specify multiple conditions in a WHERE clause
Table 3.3: LOGICAL OPERATORS
OPERATOR DESCRIPTION
a AND b Evaluates to true when a and b are both true
a OR b Evaluates to true when either a or b are true
NOT a Evaluates to true if a is false, and false if a is true
For example, the following SELECT statement uses the AND operator to retrieve
products where the UnitsInStock column is less than 10 and the ReorderLevel column is less than or equal to 20:
SELECT ProductID, ProductName, UnitsInStock, ReorderLevel
FROM Products
WHERE UnitsInStock < 10
AND ReorderLevel <= 20;
Figure 3.11 shows the results of this SELECT statement
Trang 6Figure 3.11: Using the AND operator to retrieve products where UnitsInStock is less than
10 and ReorderLevel is less than or equal to 20
In the next example, the SELECT statement uses the OR operator to retrieve products where either the UnitsInStock column is less than 10 or the ReorderLevel column is less than or equal to 20:
SELECT ProductID, ProductName, UnitsInStock, ReorderLevel
FROM Products
WHERE UnitsInStock < 10
OR ReorderLevel <= 20;
Figure 3.12 shows the results of this SELECT statement
Figure 3.12: Using the OR operator to retrieve products where either UnitsInStock is less than 10 or ReorderLevel is less than or equal to 20
Trang 7The next SELECT statement uses the NOT operator to retrieve products where the
UnitsInStock column is not less than 10:
SELECT ProductID, ProductName, UnitsInStock, ReorderLevel
FROM Products
WHERE NOT (UnitsInStock < 10);
Sorting Rows
You can use the ORDER BY clause to sort rows retrieved from the database You specify the column (or columns) to sort in the ORDER BY clause By default, rows are sorted in ascending order For example, the following SELECT statement orders the rows using the ProductName column:
SELECT ProductID, ProductName, UnitsInStock, ReorderLevel
FROM Products
ORDER BY ProductName;
Figure 3.13 shows the results of this SELECT statement As you can see, the rows are ordered in ascending order using the ProductName column
Figure 3.13: Using the ORDER BY clause to order products by ascending ProductName
You can explicitly state the order for a column using the ASC or DESC keyword ASC orders the columns in ascending order (smallest item first), and DESC orders the columns
in descending order (largest item first) For example, the following SELECT statement orders the products in descending order using the ProductName column:
SELECT ProductID, ProductName, UnitsInStock, ReorderLevel
FROM Products
Trang 8ORDER BY ProductName DESC;
You can specify multiple columns in an ORDER BY clause For example, the following SELECT statement orders the rows using both the UnitsInStock and ReorderLevel columns:
SELECT ProductID, ProductName, UnitsInStock, ReorderLevel
FROM Products
ORDER BY UnitsInStock DESC, ReorderLevel ASC;
Figure 3.14 shows the results of this SELECT statement As you can see, the rows are ordered by the UnitsInStock column first (in descending order), and then by the
ReorderLevel column (in ascending order)
Figure 3.14: Using the DESC and ASC keywords to order products by descending
UnitsInStock and ascending ReorderLevel
Retrieving the Top N Rows
You use the TOP keyword to just retrieve the top N rows from a SELECT statement For
example, the following SELECT statement uses the TOP keyword to retrieve the top 10 rows from the Products table, ordered by the ProductID column:
SELECT TOP 10 ProductID, ProductName, UnitsInStock, ReorderLevel
FROM Products
ORDER BY ProductID;
Note I've also used the optional ORDER BY clause in this example SELECT statement to order the rows by the ProductID column
Figure 3.15 shows the results of this SELECT statement
Trang 9Figure 3.15: Using the TOP keyword to retrieve the top 10 products by ProductID
Eliminating Duplicate Rows
You use the DISTINCT keyword to eliminate duplicate rows retrieved by a SELECT statement For example, the following SELECT statement uses the DISTINCT keyword
to retrieve the distinct Country column values from the Customers table:
SELECT DISTINCT Country
FROM Customers;
Figure 3.16 shows the results of this SELECT statement