Boolean Expressions in SQL
Boolean expressions are a core concept in SQL, helping to filter and manipulate data based on conditions. These expressions evaluate to one of three Boolean values: TRUE
, FALSE
, or UNKNOWN
. They are extensively used in WHERE
clauses, HAVING
clauses, and conditional statements to query and retrieve specific data from a database.
In this article, we will explore examples of Boolean expressions in SQL to understand how they work and their practical applications.
Boolean Expressions in SQL
Unlike other data types, SQL does not allow defining Boolean data types directly in table creation. Instead, Boolean logic is implemented using comparison operators (=
, >
, <
, etc.) and logical operators (AND
, OR
, NOT
). These expressions form the foundation of SQL's powerful querying capabilities.
To make this explanation more practical, let’s use a sample table named demo_table, which contains employee details like NAME
, AGE
, and CITY
.
Example 1: Boolean Expression Using the Equal To (=
) Operator
This expression checks for equality. For instance, if we want to retrieve all employees whose age is exactly 22, we use the Boolean condition AGE = 22
.
Query:
SELECT * FROM demo_table
WHERE AGE = 22;
{Boolean expression - > (AGE =22)}
Output
Explanation
The query filters the rows where the value in the AGE
column equals 22. In this case, it identifies employees whose age is exactly 22 and returns their details. This would include entries like ROMY and AKANKSHA from the demo_table
.
Example 2: Boolean Expression Using the Greater Than (>
) Operator
This expression retrieves rows where the AGE
column is greater than 22. This operator is commonly used to filter data for higher thresholds.
Query :
SELECT * FROM demo_table
WHERE AGE > 22;
{Boolean expression - > (AGE > 22)}
Output
Explanation:
The query checks each row and selects employees older than 22. It excludes ROMY and AKANKSHA while including others like PUSHKAR and SAMITA.
Example 3: Boolean Expression Using the OR
Operator
The OR
operator is used to check multiple conditions. For example, selecting employees aged 22 or 23.
Query:
SELECT * FROM demo_table
WHERE AGE = 22 OR AGE = 23;
{Boolean expression - > (AGE = 22 OR AGE = 23)}
Output
Explanation:
This expression evaluates to TRUE
if either condition is satisfied (AGE = 22
or AGE = 23
). The query first checks each row for employees whose age equals 22 and includes them in the result. Employees like ROMY
, AKANKSHA
, and PUSHKAR
meet at least one of these criteria.
Conclusion
Boolean expressions are essential tools for SQL queries, enabling users to filter and manipulate data based on logical conditions. By using operators like =
, >
, and OR
, we can create dynamic queries to extract meaningful insights. Understanding and applying Boolean expressions effectively is a key skill for working with databases, making data retrieval more precise and efficient. Mastering these expressions will significantly improve our SQL querying skills and help us handle complex data scenarios effortlessly.