SQL WHERE Boolean Example | SQL Boolean Data Type

Summary: In this tutorial, we’re going to cover SQL Boolean Data Type and SQL WHERE Boolean practical examples. one such part is the SQL Boolean.

Boolean Operators AND, OR & NOT – WHERE Clause

The SQL WHERE Boolean- The WHERE clause conditions contain multiple conditions or either be simple. Here, multiple conditions can be built using Boolean operators such as AND, OR, and NOT.

  • Therefore, if two conditions are connected by the SQL AND operator, then the rows are retrieved for which both conditions are true.
  • If two conditions are connected by the OR operator, then all rows of a table are retrieved in which either the 1st or the 2nd condition (or both) is true.

SQL WHERE Boolean

Syntax:

Boolean Expressions

The Boolean expressions are allowed in SQL WHERE clauses and in check constraints. Boolean expressions in a SQL Server WHERE clause has a highly liberal syntax. Let’s see SQL WHERE Boolean Example.

A boolean expression in SQL Server can include a boolean operator or operators. These operators are listed in the given below the following table.

Boolean operators Table

Operator Syntax Explanation and Example
AND, OR, NOT Here, you can evaluate any operand(s) that are boolean expressions
(orgin_Railwaystation= ‘DEL’) OR
(Destination_Railwaystation= ‘LKO’)
— returns true
Comparisons <, =, >, <=, >=, <> thats are applicable to all of the built-in types.
DATE(‘2017-02-21’) <
DATE(‘2017-03-11’)
— returns true

Note: Here, the derby also accepts the != operator, Therefore, which is not included in the SQL standard.
BETWEEN This operator tests whether the 1st operand is between the 2nd and 3rd operands. Therefore, the 2nd operand must be less than the 3rd operand. Thus its will be applicable only to types to which <= and >= can be applied.
WHERE Reservation_Booking_Date BETWEEN
DATE(‘2017-02-21’) AND
DATE(‘2017-03-11’)
IN The operates on table subquery or a list of values. Therefore, it returns TRUE if the left expression’s value is in the result of the table subquery or in the list of values. Furthermore, The table subquery can return multiple rows but it must return a single column.
WHERE Reservation_Booking_Date NOT IN
(SELECT Reservation_Booking_Date FROM
RailwayBookings WHERE seats_available = 0)
EXISTS Operates on a table subquery. Therefore, they return TRUE if the table subquery returns any rows, & FALSE if it returns no rows. Table subquery can return multiple columns (Note: only if you use * to denote multiple columns) & rows.
WHERE EXISTS
(SELECT *
FROM Trains
WHERE Destination_Railwaystation= ‘LKO’
AND orgin_Railwaystation= ‘DEL’)
IS NULL, IS NOT NULL This operator will test whether the result of an expression is null or not.
WHERE MiddleName IS NULL
LIKE Attempts to match a character expression to a character pattern, which is a character string that has one or additional wildcards.
% matches any number of characters in the corresponding position within the initial character expression.
_ matches one character within the corresponding position within the character expression.
Any other character matches only that character in the corresponding position within the character expression.
SELECT a FROM tabA WHERE a
LIKE ‘%=_’ ESCAPE ‘=’
Quantified comparison