W03 Paper CIT 225
W03 Paper CIT 225
23 January 2021
BTU-I CIT 225
SELECT Column_name
FROM table_name
WHERE condition2[AND [OR]] condtion2…
Equality comparisons
Equality comparisons looks if a value in SELECT clause is equal to another value. The following example
compares the “id” column to “5” showing only the rows with a value of 5 in the “id” column.
mysql> SELECT id, f_name, cell_number
-> FROM cell_phone_2
-> WHERE id = 5;
+ — — + — — — — + — — — — — — — +
| id | f_name | cell_number |
+ — — + — — — — + — — — — — — — +
| 5 | Barry | 444–111–5555 |
+ — — + — — — — + — — — — — — — +
1 row in set (0.00 sec)
These kind of conditions are known as equality conditions because it compares if one expressions equal to
another.
Inequality comparisons
Another kind of conditions compares if two expressions are not equal, and is known as inequality condition.
The following example shows an inequality condition using the <> operator:
mysql> SELECT pt.name product_type, p.name product
-> FROM product p INNER JOIN product_type pt
-> ON p.product_type_cd = pt.product_type_cd
-> WHERE pt.name <> 'Customer Accounts';
+-------------------------------+-------------------------+
| product_type | product |
+-------------------------------+-------------------------+
| Individual and Business Loans | auto loan |
| Individual and Business Loans | business line of credit |
| Individual and Business Loans | home mortgage |
| Individual and Business Loans | small business loan |
+-------------------------------+-------------------------+
4 rows in set (0.00 sec)
Range comparisons
Range is another kind of conditions. It allows to compare if an expression falls within a certain range. The
following examples show how it works:
SELECT *
FROM contacts
WHERE contact_id >= 100
AND contact_id <= 200;
SELECT *
FROM contacts
WHERE contact_id BETWEEN 100 AND 200;
This examples would return all rows from the contacts table where the “contact_id” is between 100 and 200
Lookup operators
There are many lookup operators. The following table shows some of these operators and a brief description:
Operator Description
= Checks if the values of the two operands are equal or not, if yes, then the condition
becomes true.
!= Checks if the values of the two operands are equal or not, if the values are not equal then
the condition becomes true.
> Checks if the value of the left operand is greater than the value of the right operand, if yes,
then the condition becomes true.
< Checks if the value of the left operand is less than the value of the right operand, if yes
then the condition becomes true.
>= Checks if the value of the left operand is greater than or equal to the value of the right
operand, if yes, then the condition becomes true.
<= Checks if the value of the left operand is less than or equal to the value of the right
operand, if yes, then the condition becomes true.