WHERE Clause

Introduction

The WHERE clause is used to filter the data records on the basis of a specified condition or a set of conditions in the search queries. When the WHERE condition is used in a query, the data records are verified based on the specified conditions, and the commands are executed only on the records that satisfy the conditions.

For example, the syntax for using the WHERE condition in a SELECT statement is:

    
copy
SELECT column_name(s) FROM base_table_name WHERE condition

The WHERE condition can be used with the UPDATE and DELETE statements as well.


Operators Supported by the WHERE Clause

You can use the following operators in the WHERE conditions in ZCQL queries:

Operators Description
= Equal to
IS TRUE if the operand is the same as the value
IS NULL TRUE if the operand is a null value
IS NOT NULL TRUE if the operand is not a null value
=! Not equal to
LIKE TRUE if the operand matches a pattern
NOT LIKE TRUE if the operand does not match a pattern
BETWEEN TRUE if the operand value is between the start and end values
IN TRUE if the operand is equal to a list of expressions
NOT IN TRUE if the operand is not equal to a list of expressions
> Greater than
>= Greater than or equal to
< Lesser than
<= Lesser than or equal to

Note: The LIKE, NOT LIKE, and BETWEEN operations are not supported for the SELECT statement currently. You can only execute them for the UPDATE and DELETE statements.

Example:

To view a list of the movies that are screened on days other than July 13, 2018 from the Movies table, you can specify the date condition in the query using the WHERE clause in the following way:

    
copy
SELECT MovieName, ShowDate FROM Movies WHERE ShowDate IS '2018-07-14'

This will generate the following output:

MovieName ShowDate
Hotel Transylvania 3: Summer Vacation 2018-07-14
Skyscraper 2018-07-14

Multiple WHERE Conditions

You can specify a maximum of five WHERE conditions in a single query. When multiple WHERE conditions are specified, you can use either an AND or an OR operator to link the conditions together. The functionalities of the AND and OR operators are:

  • AND: Produces only the data records that satisfy both of the conditions that are associated with the AND operator.

  • OR: Produces the data records that satisfy either of the conditions that are associated with the OR operator.

For example, the syntax for using multiple WHERE conditions in a SELECT statement is:

    
copy
SELECT column_name(s) FROM base_table_name WHERE condition_1 AND|OR condition_2.. AND|OR condition_5

Example:

To view a list of the show dates and show times from the Movies table for either ‘The First Purge’ or ‘Skyscraper’ movies that are screened on July 14, execute the following query:

    
copy
SELECT MovieName, ShowDate, ShowTime FROM Movies WHERE MovieName='The First Purge' OR MovieName='Skyscraper' AND ShowDate='2018-07-14'

There is only one record that matches the above conditions. The query will therefore generate the following result:

MovieName ShowDate ShowTime
Skyscraper 2018-07-14 21:30:00

LIKE Statement

The LIKE condition enables you to indicate records which contain a specific criteria and select all records that match that condition. For example, you can indicate a specific alphabet that the records you require to be fetched must begin with, or a particular value that they must contain.

Note: The LIKE condition cannot be used with the SELECT statement currently. You can use it with the UPDATE and DELETE statements.

For example, the syntax of the LIKE condition in an UPDATE statement is as follows:

    
copy
UPDATE base_table_name SET column_name(s)=value(s) WHERE condition LIKE *value*

The ‘*’ can be used either before or after, or both before and after, the value that you specify. The ‘*’ is essentially a placeholder that indicates that any value can be replaced in its stead.

For example, to indicate that all records in a table that begin with the alphabet ‘A’ must be fetched, you can enter the LIKE value as ‘A*’. This specifies that after the letter A, any values can follow.

Example:

To update the records where the movie names begin with the letter ‘S’ in the Movies table, execute the following query:

    
copy
UPDATE Movies SET ShowDate='2018-07-17' WHERE MovieName like 'S*'

This will update the following record and set the value:

MovieID MovieName ShowDate ShowTime TheaterID
2059 Skyscraper 2018-07-17 21:30:00 053

BETWEEN Statement

The BETWEEN condition enables you to filter records by indicating the starting and ending values in a particular column containing numerical values. For example, you can select all the records where the percentage values are between 70 and 80.

Note: The BETWEEN condition cannot be used with the SELECT statement currently. You can use it with the UPDATE and DELETE statements.

For example, the syntax of a BETWEEN condition in a DELETE statement is as follows:

    
copy
DELETE FROM base_table_name WHERE condition BETWEEN Value1 AND Value2

Note: You can use the BETWEEN statement only for selecting 'Int' or 'Double' values in the Catalyst Data Store table. You will not be able to use it for any other data types.

Example:

To delete the records where the value of the MovieID is between 2056 and 2059 in the Movies table, execute the following query:

    
copy
DELETE FROM Movies WHERE MovieID BETWEEN 2056 AND 2059

The following records from the table will be deleted:

MovieID MovieName ShowDate ShowTime TheaterID
2057 Ant-Man and the Wasp 2018-07-13 14:20:00 052
2058 Hotel Transylvania 3: Summer Vacation 2018-07-14 17:00:00 052