Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

SQLite Where Clause

SQLite Where Clause

SQLite's WHERE The clause is used to specify the conditions for fetching data from one or more tables.

If the given condition is satisfied, i.e., it is true, the specific values are returned from the table. You can use the WHERE clause to filter records and fetch only the records you need.

The WHERE clause can be used not only in SELECT statements, but also in UPDATE, DELETE statements, and so on, which we will learn about in subsequent chapters.

Syntax

The basic syntax of a SQLite SELECT statement with a WHERE clause is as follows:



SELECT column1, column2, columnN 

FROM table_name

WHERE [condition]

Example

You can also usecomparison or logical operatorsto specify conditions, such as >,<, =, LIKE, NOT, and so on. Assume the COMPANY table has the following records:



ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

1           Paul        32          California  20000.0

2           Allen       25          Texas       15000.0

3           Teddy       23          Norway      20000.0

4           Mark        25          Rich-Mond   65000.0

5           David       27          Texas       85000.0

6           Kim         22          South-Hall  45000.0

7           James       24          Houston     10000.0

The following example demonstrates the use of SQLite logical operators. The following SELECT statement lists those with AGE greater than or equal to 25 andand salary greater than or equal to 65000.00:



sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000;

ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

4           Mark        25          Rich-Mond   65000.0

5           David       27          Texas       85000.0

The following SELECT statement lists all records with AGE greater than or equal to 25 orand salary greater than or equal to 65000.00:



sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000;

ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

1           Paul        32          California  20000.0

2           Allen       25          Texas       15000.0

4           Mark        25          Rich-Mond   65000.0

5           David       27          Texas       85000.0

The following SELECT statement lists all records where AGE is not NULL; the result shows all records, meaning no record has AGE equal to NULL:



sqlite>  SELECT * FROM COMPANY WHERE AGE IS NOT NULL;

ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

1           Paul        32          California  20000.0

2           Allen       25          Texas       15000.0

3           Teddy       23          Norway      20000.0

4           Mark        25          Rich-Mond   65000.0

5           David       27          Texas       85000.0

6           Kim         22          South-Hall  45000.0

7           James       24          Houston     10000.0

The following SELECT statement lists all records where NAME starts with 'Ki'; characters after 'Ki' are not restricted:



sqlite> SELECT * FROM COMPANY WHERE NAME LIKE 'Ki%';

ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

6           Kim         22          South-Hall  45000.0

The following SELECT statement lists all records where NAME starts with 'Ki'; characters after 'Ki' are not restricted:



sqlite> SELECT * FROM COMPANY WHERE NAME GLOB 'Ki*';

ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

6           Kim         22          South-Hall  45000.0

The following SELECT statement lists all records where AGE is 25 or 27:



sqlite> SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 );

ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

2           Allen       25          Texas       15000.0

4           Mark        25          Rich-Mond   65000.0

5           David       27          Texas       85000.0

The following SELECT statement lists all records where AGE is neither 25 nor 27:



sqlite> SELECT * FROM COMPANY WHERE AGE NOT IN ( 25, 27 );

ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

1           Paul        32          California  20000.0

3           Teddy       23          Norway      20000.0

6           Kim         22          South-Hall  45000.0

7           James       24          Houston     10000.0

The following SELECT statement lists all records where AGE is between 25 and 27:



sqlite> SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27;

ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

2           Allen       25          Texas       15000.0

4           Mark        25          Rich-Mond   65000.0

5           David       27          Texas       85000.0

The following SELECT statement uses an SQL subquery that finds all records with an AGE field where SALARY > 65000; the following WHERE clause is used with the EXISTS operator to list all records in the outer query whose AGE exists in the results returned by the subquery:



sqlite> SELECT AGE FROM COMPANY 

        WHERE EXISTS (SELECT AGE FROM COMPANY WHERE SALARY > 65000);

AGE

----------

32

25

23

25

27

22

24

The following SELECT statement uses an SQL subquery that finds all records with an AGE field where SALARY > 65000; the following WHERE clause is used with the > operator to list all records in the outer query whose AGE is greater than the ages in the results returned by the subquery:



sqlite> SELECT * FROM COMPANY 

        WHERE AGE > (SELECT AGE FROM COMPANY WHERE SALARY > 65000);

ID          NAME        AGE         ADDRESS     SALARY

----------  ----------  ----------  ----------  ----------

1           Paul        32          California  20000.0