Tt
Click this widget to change the font size.
CC
Click this widget to change contrast.

Home Page 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | Links | Search | Bio |

Guide to HTML, CSS, and JavaScript (Buffalo State College Version)


Adding Cascading Style Sheets to Your Web Page

In Chapter 2 I introduced the SELECT statement and provided examples for retreiving all the fields or just certain fields in a record. SQL also allows you to restrict your query to return on certail records in a table using condtitionals.

Using a WHERE Conditional on a SELECT Statement

The WHERE keyword can ne used to provide a condition (restriction) so the records retreived (result set) contains only the records you are interested in. The WHERE can use operators and wildcards.

Conparison
Operator
Description
=Equal To
>Greater Than
<Less Than
!>Not Greater Than
!<Not Less Than
>=Greater Than or Equal To
<=Less Than or Equal To
<>Not Equal To
!=Not Equal To
Arithmatic
Operator
Description
+Addition
-Subtraction
*Multiplication
/Division
%Modulus
Logical
Operator
Description
ALLCompares all the values in one set to another set
ANDReturns values if all the contitinals are TRUE
ANYCompares one value tomore than one value
BETWEENReturns values within a range
EXISTSDetermines if a record is found matching the criteria
INDetermines if the value is in a list of values
LIKECompare values using wildcards
NOTUsed to negate any conditional operator
ORReturns values if at least one criteria is TRUE
IS NULLReturns a value if the field contains a NULL value
SOMEReturns values from a subquery

Figure 3-1: SQL Operators

Using Wildcards in WHERE Conditional

WildcardDescription
%Will match any sequence of charaters and numbers
_Will match any single character in a certain position

Figure 3-2: SQL Wildcards
SELECT *
FROM jimTable
WHERE jimID = 1;

     JIMID FIRSTNAME                 LASTNAME
---------- ------------------------- -------------------------
         1 Jim                       Gerland
Figure 3-3: Using the WHERE Conditional With a equal to (=) Operator

Note, when using the LIKE operator the string needs to be surroiunded with single quotes (''). The LIKE operator is case-sensitive.

SELECT *
FROM jimTable
WHERE firstName LIKE 'J%';

     JIMID FIRSTNAME                 LASTNAME
---------- ------------------------- -------------------------
         1 Jim                       Gerland
Figure 3-4: Using the WHERE Conditional the LIKE and the % Wildcard
SELECT *
FROM jimTable
WHERE firstName LIKE '_ally';

     JIMID FIRSTNAME                 LASTNAME
---------- ------------------------- -------------------------
         1 Sally                     Smart
Figure 3-5: Using the WHERE Conditional the LIKE and the _ Wildcard

Sorting Your Result Set

You can use the ORDER BY clause to sort the result set from your SELECT statement. By default, the result set is sorted ascending based on the field (or fields) you supply on the ORDER BY clause. You can add the DESC to order your result set in descending order.

SELECT *
FROM jimTable
ORDER BY lastName DESC;


     JIMID FIRSTNAME                 LASTNAME
---------- ------------------------- -------------------------
         3 Sally                     Smart
         2 Phil                      Phailure
         1 Jim                       Gerland
Figure 3-5: Using the ORDER BY DESC Clause