The WHERE clause and searching conditions

Previous Index Next

Comparison operators Combinations of conditions (AND, OR) Range (BETWEEN ... AND) List (IN)

Sometimes we want to retrieve those records which satisfy a certain condition. This can be achieved by using the WHERE clause in the SELECT statement.

The syntax for a SELECT statement using the WHERE clause is as follows:

SELECT <list_of_fields> FROM <table_name> WHERE <search_condition>

Only those records that satisfy the <search_condition> will be retrieved.

SQL provides a variety operators and keywords for expressing the <search_condition>. Here we will discuss the following:

Comparison operators Top

The comparison operators provided in SQL are the same as those in Pascal:

Operator = > < >= <= <>
Meaning equal to greater than less than greater than or equal to less than or equal to not equal to

With the comparison operators, the WHERE clause can be used in the following way:

WHERE <expression> <comparison_operator> <expression>

An <expression> can be a constant, a field name or any combination of them connected by arithmetic operators (e.g. +, -, *, /).

For example:

List the class numbers, names and the marks of Chinese Language and Culture of those students with passes (>= 50) in Chinese Language and Culture.
SELECT class_num, name, clc FROM s6a WHERE clc >= 50
Output:
class_num name clc
1 Chan Wai Man, Raymond 64
2 Chow Chi Ling 70
3 Fung Ching Man, Mandy 72
4 Hung Wai Ming 52
6 Poon Kwok Fai 59
7 Sung Hing Wah, Patrick 70
8 Tang Wing Chi 80
10 Yeung Chun 69
11 Lai Fung Chun 78
13 Man Fook Wing 55
14 Chung Kwok Fai, Fred 72
15 Lee Lai May 67
 
List the class numbers and the names of those students with failures (< 50) in the average marks.
SELECT class_num, name FROM s6a WHERE (clc + ue + pm + phy + cs) / 5 < 50
Output:
class_num name
5 Leung King
9 Wong Ka Tak, Kent

Like Pascal, comparison operators can be used with character expressions. The characters are ordered alphabetically. However, whether the comparison is case sensitive or not depends on the system used. For Microsoft Visual FoxPro and Microsoft Access, the comparsion is case insensitive.

For example:

List all the fields of those records with student IDs "93" or over:
SELECT * FROM s6a WHERE stud_id >= '93'
Output:
class_num stud_id name email clc ue phy pm cs
3 94302 Fung Ching Man, Mandy mandyfung@example.com 72 50 42 59 60
8 97602 Tang Wing Chi s97602@sample.edu.hk 80 79 70 72 69
10 93211 Yeung Chun cyeung@testing.com.hk 69 80 77 60 52
11 96374 Lai Fung Chun s96374@sample.edu.hk 78 75 69 65 61
12 94412 Chan Lai Yin cly@testing.com.hk 43 59 53 61 60
13 98832 Man Fook Wing fwman@testing.com.hk 55 77 34 40 51
14 95343 Chung Kwok Fai, Fred fredchung@example.com 72 62 53 47 50
15 97233 Lee Lai May maylee@testing.com.hk 67 71 56 60 55

Combinations of conditions (AND, OR) Top

The use of the operators AND and OR are similar to that in other programming languages:

List the class numbers, the names and the marks of CLC and UE of those students who get a pass (>= 50) in both CLC and UE:
SELECT class_num, name, clc, ue FROM s6a WHERE clc >= 50 AND ue >= 50
Output:
class_num name clc ue
1 Chan Wai Man, Raymond 64 55
2

Chow Chi Ling

70 62
3 Fung Ching Man, Mandy 72 50
6 Poon Kwok Fai 59 60
7 Sung Hing Wah, Patrick 70 72
8 Tang Wing Chi 80 79
10 Yeung Chun 69 80
11 Lai Fung Chun 78 75
13 Man Fook Wing 55 77
14 Chung Kwok Fai, Fred 72 62
15 Lee Lai May 67 71
 
List the class numbers, the names and the marks of CLC and UE of those students who fail (mark < 50) in either CLC or UE or both:
SELECT class_num, name, clc, ue FROM s6a WHERE clc < 50 OR ue < 50
Output:
class_num name clc ue
4 Hung Wai Ming 52 48
5 Leung King 40 50
9 Wong Ka Tak, Kent 35 40
12 Chan Lai Yin 43 59

Range (BETWEEN ... AND) Top

With BETWEEN ... AND, the WHERE clause can be used in the following way:

WHERE <expression> BETWEEN <value_1> AND <value_2>

This WHERE clause indicates that the searching condition is <expression> lies between <value_1> and <value_2> inclusively. Therefore, this WHERE clause is equivalent to the following:

WHERE <expression> >= <value_1> AND <expression> <= <value_2>

For example:

List the class numbers, names and the marks of Computer Studies of those students who obtain 50-59 inclusive in Computer Studies.
SELECT class_num, name, cs FROM s6a WHERE cs BETWEEN 50 AND 59
Output:
class_num name cs
1 Chan Wai Man, Raymond 59
4 Hung Wai Ming 59
10 Yeung Chun 52
13 Man Fook Wing 51
14 Chung Kwok Fai, Fred 50
15 Lee Lai May 55

Note: For setting a condition with an exclusive range, we must use the operators > and <.

List (IN) Top

The IN keyword allows the user to select values that match anyone of a list of values. For example:

List the class numbers, names and the marks of Computer Studies of those students who obtain 50, 60, 70, 80 or 90 marks in Computer Studies.
SELECT class_num, name, cs FROM s6a WHERE cs IN (50, 60, 70, 80, 90)
Output:
class_num name cs
2

Chow Chi Ling

70
3 Fung Ching Man, Mandy 60
5 Leung King 60
7 Sung Hing Wah, Patrick 70
12 Chan Lai Yin 60
14 Chung Kwok Fai, Fred 50

The above SQL statement is equivalent to the following:

SELECT class_num, name, cs FROM s6a
  WHERE cs = 50 OR cs = 60 OR cs = 70 OR cs = 80 OR cs = 90)

Previous Index Next