Page 265 - PYTHON-12
P. 265
For example, mysql> SELECT * FROM student
WHERE Stream <> ‘Commerce’;
The above command shall display the records of all the students who are not from
Commerce stream.
Resultant table: student
Rollno Name Gender Marks DOB Mobile_no Stream
1. Raj Kumar M 93 17-Nov-2000 9586774748 Science
3. Ankit Sharma M 76 02-Feb-2000 8567490078 Science
4. Radhika Gupta F 78 03-Dec-1999 9818675444 Humanities
5. Payal Goel F 82 21-April-1998 9845639990 Vocational
6. Diksha Sharma F 80 17-Dec-1999 9897666650 Humanities
7. Gurpreet Kaur F 65 04-Jan-2000 7560567890 Science
9. Shreya Anand F 70 08-Oct-1999 8876543988 Vocational
10. Prateek Mittal M 75 25-Dec-2000 9999967543 Science
8 rows in a set (0.02 sec)
Thus, while using relational operators in a WHERE clause with a SELECT statement, the
database program goes through the entire table checking each record one by one and
compares with the condition specified. If it is true, the corresponding row is selected for
display, otherwise ignored.
CTM: While comparing character, date and time data using relational operators, it should be enclosed in
single quotation marks.
(c) Logical Operators
The SQL logical operators are the operators used to combine multiple conditions to narrow
the data selected and displayed on the basis of the condition specified in an SQL statement.
Logical operators are also known as Boolean operators. The three logical operators in
SQL are—AND, OR and NOT operator. Out of these, AND and OR operators are termed as
Conjunctive operators since these two operators combine two or more conditions. The
AND and OR operators are used to filter records based on more than one condition.
These operators provide a means to make multiple comparisons with different operators
in the same SQL statement.
CTM: The order of precedence for logical operators (AND, OR, NOT operator) is NOT(!), AND(&&) and OR(||).
1. AND operator
The AND operator displays a record and returns a true value if all the conditions
(usually two conditions) specified in the WHERE clause are true.
Condition 1 Condition 2 Result (AND operation)
True True True
True False False
False True False
False False False Relational Database and SQL
As shown in the table, when both condition 1 and condition 2 are true, then only is
the result true. If either of them is false, the result becomes false.
12.35