How to use Logical Operators in MySQL
Notes:
How to use Logical Operators in MySQL:
- Logical operators are also known as logical connectives.
- They are used to connect one or more conditional expressions.
- They accept Boolean operands, on evaluation they yield the result either true or false.
- In MySQL; 1 indicates true and 0 indicates false vice versa.
&& / and : Logical And Operator:
If both LHS and RHS operands are true (1) then the result will be true (1), in all other cases the result will be false (0)
|| / or: Logical Or Operator:
If both LHS and RHS operands are false (0) then the result will be false (0), in all other cases the result will be true (1)
xor: Logical Exclusive-Or Operator:
If both LHS and RHS operands are same then the result will be false (0), in all other cases the result will be true (1)
! / not: Logical Not Operator:
If the given operand is false (0), then the result will be true (1). Vice versa
Example Code:
select true && true; // 1
select true && false; // 0
select false && true; // 0
select false && false; // 0
select 1 && 1; // 1
select 1 && 0; // 0
select 0 && 1; // 0
select 0 && 0; // 0
select true || true; // 1
select true || false; // 1
select false || true; // 1
select false || false; // 0
select 1 || 1; // 1
select 1 || 0; // 1
select 0 || 1; // 1
select 0 || 0; // 0
select true xor true; // 0
select true xor false; // 1
select false xor true; // 1
select false xor false; // 0
select 1 xor 1; // 0
select 1 xor 0; // 1
select 0 xor 1; // 1
select 0 xor 0; // 0
select !true; // 0
select !false; // 1
select !1; // 0
select !0; // 1
select * from tbl_student where (salary > 2000) && (salary < 10000);
Interview Questions: