Using AND, OR, <, > and = operators
It is possible to use the AND, OR <, > and = operators with a WHERE clause. This will refine a search further.
AND
Using AND allows the database developer to specify more than one condition that must be met. In this example, two conditions are specified.
SELECT PupilID, First Name, Surname, Class FROM PupilWHERE Class = ‘1T1’ AND Surname = ‘McNamee’;This would return:
| PupilID | First Name | Surname | Class |
| 0004 | James | McNamee | 1T1 |
| 0007 | Amy | McNamee | 1T1 |
| PupilID | 0004 |
|---|---|
| First Name | James |
| Surname | McNamee |
| Class | 1T1 |
| PupilID | 0007 |
|---|---|
| First Name | Amy |
| Surname | McNamee |
| Class | 1T1 |
OR
Using OR allows the database developer to specify a series of conditions, where only one of the conditions needs to be met.
SELECT PupilID, First Name, Merit PointsFROM PupilWHERE First Name = ‘Amy’ OR Merit Points > 60;This would return:
| PupilID | First Name | Merit Points |
| 0001 | Nisa | 74 |
| 0003 | Amy | 55 |
| 0004 | James | 88 |
| 0007 | Amy | 63 |
| 0009 | Beatrice | 77 |
| 0010 | Tom | 91 |
| PupilID | 0001 |
|---|---|
| First Name | Nisa |
| Merit Points | 74 |
| PupilID | 0003 |
|---|---|
| First Name | Amy |
| Merit Points | 55 |
| PupilID | 0004 |
|---|---|
| First Name | James |
| Merit Points | 88 |
| PupilID | 0007 |
|---|---|
| First Name | Amy |
| Merit Points | 63 |
| PupilID | 0009 |
|---|---|
| First Name | Beatrice |
| Merit Points | 77 |
| PupilID | 0010 |
|---|---|
| First Name | Tom |
| Merit Points | 91 |
Even though one of the pupils named Amy has less than 60 merit points, the record is still returned because of the OR operator. The first condition means that all records containing ‘Amy’ in the First Name column/field will be returned regardless of whether the second condition is met.
Operators
<, > and = are all operators
| < | Less than |
| > | Greater than |
| = | Equals |
| < |
| Less than |
| > |
| Greater than |
| = |
| Equals |
In the example above, > was used to return all results greater than a particular mark. < and = can be used in the same way
To show all records containing 'Amy' in the First Name column OR all records containing results less than 60 points in the Merit Points column, you would change the third line of code to:
WHERE First Name = ‘Amy’ OR Merit Points < 60;To show all records containing 'Amy' in the First Name column OR all records containing results equal to 60 points in the Merit Points column, you would change the third line of code to:
WHERE First Name = ‘Amy’ OR Merit Points = 60;