Logical and Comparison Operators

tags: #sql

Logical Operators

What is a logical operator?

SQL logical operators are used to combine and evaluate multiple conditions in a query's WHERE or HAVING clauses. The logical operators include:

  1. AND: returns true if all conditions are true
SELECT Column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND ...
  1. OR: returns true if any of the conditions are true
SELECT Column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR ...
  1. NOT: negates the truth value of a condition
SELECT Column1, column2, ...
FROM table_name
WHERE NOT condition1

These operators can be used to construct complex conditions in a query to filter data based on multiple criteria.


Comparison Operators

What are comparison operators?

Comparison operators are used to compare two values and return a boolean (TRUE or FALSE) result.

NULL Values

It is not possible to test for NULL values with comparison operators.

List of comparison operators

  1. Equality
VALUE = VALUE2
  1. Not Equal to
VALUE <> VALUE2

// alternative
VALUE != VALUE2
  1. Less or Greater than
VALUE > VALUE2 // greater than

VALUE < VALUE2 // less than

VALUE >= VALUE2 // greater than or equal to

VALUE <= VALUE2 // less than or equal to
  1. Between range of values
SELECT column1, column2, ...
FROM table_name
WHERE column_name BETWEEN (value1 AND value2);
  1. Like (pattern matching)
SELECT column1, column2, ...
FROM table_name
WHERE column_name LIKE pattern;
  1. To compare a value to some SET object (e.g., list) using IN
SELECT column1, column2, ...
FROM table_name
WHERE column_name IN (value1, value2, ...);
Powered by Forestry.md