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:
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:
|
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:
|
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:
|
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:
|
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:
|
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:
|
Note: For setting a condition with an exclusive range, we must use the operators > and <.
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:
The above SQL statement is equivalent to the following:
|