Jamaica trevoir.williams@gmail.com

SQL Operators in the WHERE Clause

Read Post
Share on:

Get familiar with commonly used to know SQL Operators and Keywords when filtering data with the WHERE Clause

SQL Operators include reserved keywords or characters used primarily in the SQL statement’s WHERE clause. These keywords allow you to be more specific with conditions.

SQL Comparison Operators

Comparison operators in SQL are used to compare the values of either a column and a static value, a column and a variable’s value or a column and another column.

Operator Usage Examples - SELECT * FROM tableName …
= Checks if a a column’s value is EQUAL to a particular value. WHERE COL1 = VALUE
!= or <> Checks if a column’s value is NOT EQUAL to a particular value. WHERE COL1 != VALUE
> Checks if a column’s value is GREATER THAN a particular value. WHERE COL1 > VALUE
< Checks if a column’s value is LESS THAN a particular value. WHERE COL1 < VALUE
>= Checks if a column’s value is GREATER THAN OR EQUAL TO a particular value. WHERE COL1 >= VALUE
<= Checks if a column’s value is LESS THAN OR EQUAL TO a particular value. WHERE COL1 <= VALUE

SQL Logical Operators

Logical operators in the SQL are used to enhance the WHERE clause’s ability to perform value comparisons between a column and a value.

Operator Usage Examples - SELECT * FROM tableName …
BETWEEN Checks if a column’s value is found in a range. WHERE COL1 BETWEEN VALUE1 AND VALUE2
IN Checks if a column’s value is IN a list of values. This list can be of words, numbers or a sub-query WHERE COL1 IN (val1, val2, …)
NOT Checks if a column’s value is NOT a particular value. WHERE COL1 > VALUE
AND Allows you to specify an additional condition in a WHERE clause. Both conditions must evaluate to true. If either side is false, then the whole WHERE condition will be false. WHERE COL1 < VALUE1 AND COL1 != VALUE2
OR Allows you to specify an additional condition in a WHERE clause. If either side is true, then the whole WHERE condition will be true. WHERE COL1 = VALUE1 OR COL1 = VALUE2
EXISTS Checks if a column’s value is present in a record set from a sub-query. WHERE EXISTS (SELECT col FROM tabeName)
LIKE Checks if a column’s value matches an expression. Otherwise known as the WILDCARD operator. This is usually used when you want to find a word based on a portion or pattern of the word. The modulus (‘%’) is used around the expression. WHERE COL1 LIKE ‘%expression%’
ANY Returns TRUE if ANY of the sub-query values meet the condition. WHERE COL1 ANY (SELECT col FROM tabeName)
ALL Returns TRUE if ALL of the sub-query values meet the condition. _WHERE COL1 ALL (SELECT col FROM tabeName)

Conclusion

These statements help us to enhance our WHERE clauses and allow us to be more specific in making reference to what values we are interested in retrieving from a table.

Enhance your Database Development and Design skills by enrolling in Microsoft SQL Server 2017 for Everyone! AND MySQL Database Development Mastery.

These courses will guide you through the intricate world of Database Design, Development and Querying; using the most popular, powerful and in demand Relational Database Management Systems in the industry.

Trevoir Williams

Jamaican Software Engineer and Lecturer.