3. Limiting Rows Using Restriction
LastName GPA CourseID
Bartell 3.21 DCS
Kebel 2.71 DIC
Lee 3.82 DIT
Lewis 2.51 DICT
Law 3.05 DIT
Mikulski 1.89 DCS
Tham 3.89 DCS
Faga 2.22 DIC
Nicosia 3.11 DICT
Owen 3.34 DIC
……..
LastName GPA CourseID
Bartell 3.21 DCS
Mikulski 1.89 DCS
Tham 3.89 DCS
Williams 2.74 DCS
Retrieve only those
enrolled in DCS
course
4. The WHERE clause
SELECT [DISTINCT] {*, column AS [alias],...}
FROM table
[WHERE condition(s)]
• WHERE clause to restrict the rows returned from the
query.
• A WHERE clause contains a condition that must be met
• Write the WHERE clause follows the FROM clause
• The WHERE clause consists of three elements:
Column name
Comparison operator
Column name, constant, or list of values
5. Using the WHERE clause
SELECT LastName, GPA, CourseID
FROM student
WHERE CourseID = 'DCS';
Output
LastName GPA CourseID
Bartell 3.21 DCS
Mikulski 1.89 DCS
Tham 3.89 DCS
Williams 2.74 DCS
6. Restricting data that are Character
String and Date
Character strings in the WHERE clause
must be enclosed in single quotation
marks (' ') or double quotation marks (“ ”).
Dates must enclosed in pound signs (# #)
Number constants, however, must not.
All character searches are non-case
sensitive.
7. Restricting data that are Character
String and Date
SELECT LastName, FirstName,
Gender
FROM student
WHERE Gender = ‘F’;
SELECT LastName, FirstName,
Gender
FROM student
WHERE Gender = “F”;
or
LastName GPA CourseID
Bartell 3.21 DCS
Mikulski 1.89 DCS
Tham 3.89 DCS
Williams 2.74 DCS
Note : using ‘f’ or ‘F’ will also return the same result
Single-quotation
double-quotation
11. BETWEEN..AND.. operator
SELECT LastName, FirstName, DateEnrolled
FROM student
WHERE DateEnrolled Between #01-Jan-2001# And #31-Dec-2001# ;
Output :
LastName FirstName DateEnrolled
Kebel Laura N. 23-Jun-01
Law Arthur 01-Apr-01
Faga Mark J. 25-Jun-01
Owen John M. 17-Sep-01
Ng Jian Xian 01-Apr-01
Jann How 01-Apr-01
Lower limit Upper limit
Note:
Dates must enclosed
in pound signs (# #)
12. IN operator
The IN operator is used when there is a
list of discrete values that satisfy the
condition.
The set of all these valid values is placed
in parenthesis as a comma-delimited list.
All values must have the same data type
13. IN operator (example)
SELECT LastName, FirstName, CourseID
FROM student
WHERE CourseID IN (‘DCS’, ‘DIC’);
Output :
LastName FirstName CourseID
Bartell Joseph P. DCS
Kebel Laura N. DIC
Mikulski Kathleen DCS
Tham Tian En DCS
Faga Mark J. DIC
Owen John M. DIC
Williams Jason R. DCS
Chan Xi Xi DIC
14. LIKE operator
The LIKE operator is used for finding patterns in the
data.
Patterns are specified using wildcard characters
Wildcard characters Meaning
* (asterisk) A string of characters of any length
? (question mark) One character
# (pound sign) One digit (numeric character)
[b-f] (square brackets with a
dash)
Range of characters
[!b-f] Outside a range of characters
[*] , [?], [#] Putting a character in square brackets means to
take it literally, rather than using as a wildcard
character.
15. LIKE operator
Examples of wildcard patterns in Access
Pattern Wildcard Pattern Examples
Text string beginning with a ‘s’ ‘s*’ ‘start’
‘stop’
‘Smith’
Four characters ending with an
‘e’
‘???e’ ‘none’
‘123e’
Starting with a letter between
‘a’ and ‘c’ followed by two
digits
‘[a-c]##’ ‘a12’
‘c33’
16. LIKE operator (example)
SELECT LastName, FirstName, CourseID
FROM student
WHERE LastName Like ‘Le*’;
LastName FirstName CourseID
Lee Choy Yan DIT
Lewis Derrick H. DICT
Output :
17. IS NULL operator
IS NULL operator only can be used to retrieve
NULL values in a table.
SELECT LastName, FirstName
FROM student
WHERE GroupLeader IS NULL;
Output :
LastName FirstName
Law Arthur
Tham Tian En
Owen John M.
Jann How
19. Using AND Operator
AND requires both conditions to be TRUE.
SELECT FirstName, Gender, GPA
FROM student
WHERE Gender = ‘F’AND GPA > 3;
Output :
FirstName Gender GPA
Choy Yan F 3.82
Tian En F 3.89
Xi Xi F 3.12
How F 3.76
20. Using OR operator
OR requires either condition to be TRUE
SELECT FirstName, DateEnrolled,CourseID
FROM student
WHERE DateEnrolled > #01-MAY-2003# OR CourseID = 'DICT';
Output :
FirstName DateEnrolled CourseID
Derrick H. 03-Mar-00 DICT
Kathleen 12-Sep-03 DCS
Tian En 19-Sep-03 DCS
Anthony L. 01-Feb-02 DICT
Jennifer L. 25-Sep-03 DICT
21. Using NOT operator
NOT is used to reverse the meaning of the
original conditions.
NOT usually use together with IN, BETWEEN,
LIKE and IS NULL.
Example:
…. WHERE CourseID NOT IN (‘DCS’, ‘DICT’);
…. WHERE GPA NOT BETWEEN 2.0 AND 3.0;
…. WHERE MentorName NOT LIKE ‘C*’;
…. WHERE GroupLeader IS NOT NULL
22. Rules of Precedence
Use parentheses to override rules of
precedence.
Order Evaluated Operator
1 All comparison
operators
2 NOT
3 AND
4 OR
23. Rules of Precedence
SELECT FirstName, GroupLeader, GPA
FROM student
WHERE GroupLeader = 'S007' OR GroupLeader ='S016‘
AND GPA > 3;
FirstName GroupLeader GPA
Laura N. S007 2.71
Choy Yan S016 3.82
Derrick H. S007 2.51
Jian Xian S007 1.88
Xi Xi S016 3.12
Output :
24. Rules of Precedence
Using Parenthesis to change the precedence
FirstName GroupLeader GPA
Choy Yan S016 3.82
Xi Xi S016 3.12
SELECT FirstName, GroupLeader, GPA
FROM student
WHERE (GroupLeader = 'S007' OR GroupLeader ='S016‘)
AND GPA > 3;
Output :
25. ORDER BY clause
The ORDER BY clause determines how
the rows of the result table are sorted
ORDER BY clause must be placed at the
last clause in the SELECT statement.
The default sorting order is ascending.
Sort order options for each column
asc - means ascending order
desc - means descending order
26. Sorting Data
Sorting the rows by single column
SELECT LastName, FirstName
FROM student
ORDER By LastName;
Output :
Query1
LastName FirstName
Bartell Joseph P.
Chan Xi Xi
Faga Mark J.
Jann How
Kebel Laura N.
Law Arthur
Lee Choy Yan
Lewis Derrick H.
Maser Jennifer L.
Mikulski Kathleen
……
Ascending
order
27. Sorting Data (several columns)
SELECT GroupLeader, LastName,
FirstName
FROM student
ORDER By GroupLeader, LastName;
1 2
Output :
Query1
GroupLeader LastName FirstName
Jann How
Law Arthur
Owen John M.
Tham Tian En
S005 Bartell Joseph P.
S005 Nicosia Anthony L.
S005 Roche Stephanie N.
S007 Kebel Laura N.
S007 Lewis Derrick H.
S007 Ng Jian Xian
S010 Faga Mark J.
S010 Mikulski Kathleen
S010 Williams Jason R.
…….
1
2
2
2
2
28. Sorting Data (in descending)
Query1
MentorID MentorName
1004 Schubert
1002 Rimes
1005 Norman
1001 Goile
1003 Christopher
1006 Carroll
SELECT MentorID, MentorName
FROM Mentor
Order By MentorName Desc;
Output :
Descending
order
29. Sorting Data (various orders)
Query1
CourseID LastName GPA
DCS Tham 3.89
DCS Bartell 3.21
DCS Williams 2.74
DCS Mikulski 1.89
DIC Owen 3.34
DIC Chan 3.12
DIC Kebel 2.71
DIC Faga 2.22
DICT Nicosia 3.11
DICT Maser 3
DICT Lewis 2.51
DIT Lee 3.82
DIT Law 3.05
DIT Ng 1.88
DIT Roche 1.88
DNC Jann 3.76
SELECT CourseID, LastName,
GPA
FROM Student
Order By CourseID Asc,
GPA Desc ;
30. Summary
At the end of this chapter, you should
know how to use WHERE clause to
restrict data and also ORDER BY
clause to arrange data.
The different types of operator used in
SQL.