English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
SQLite WHEREThe clause is used to specify conditions when data is retrieved from one or more tables.
If the given condition is met, it is true, then it will return the specific value from the table. You will have to use the WHERE clause to filter records and extract only the necessary records.
WHERE clause is used not only in SELECT statements, but also in UPDATE, DELETE statements, etc., which will be introduced in subsequent chapters.
The following is the basic syntax of SQLite SELECT statements with WHERE clause.
SELECT column1, column2, columnN FROM table_name WHERE [condition]
You can useComparison operators or logical operators (For example >, <, =, LIKE, NOT, etc.) specify conditions. Please see the COMPANY table with the following records-
ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Mark 25 000.0 15The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 3 Teddy 23 Norway 20000.0 4 Rich 25 Mond-David 65The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 5 Texas 27 000.0 85The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 6 Kim 22 South-Hall 45The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 7 James 24 Houston 10The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than
The following is a simple example of the usage of SQLite logical operators. The following SELECT statement lists AGE (age) greater than or equal to25 and SALARY (salary) greater than or equal to65000.00 all records.
sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 AND SALARY >= 65000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 4 Rich 25 Mond-David 65The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 5 Texas 27 000.0 85The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than
The following SELECT statement lists AGE (age) greater than or equal to25 or SALARY (salary) is greater than or equal to65000.00 all records.
sqlite> SELECT * FROM COMPANY WHERE AGE >= 25 OR SALARY >= 65000; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Mark 25 000.0 15The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 4 Rich 25 Mond-David 65The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 5 Texas 27 000.0 85The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than
Immediately following the SELECT statement, lists all records where AGE (age) is not NULL, which means that all records will be displayed because there are no records where the AGE value is equal to NULL.
sqlite> SELECT * FROM COMPANY WHERE AGE IS NOT NULL; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 1 Paul 32 California 20000.0 2 Mark 25 000.0 15The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 3 Teddy 23 Norway 20000.0 4 Rich 25 Mond-David 65The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 5 Texas 27 000.0 85The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 6 Kim 22 South-Hall 45The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 7 James 24 Houston 10The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than
The following SELECT statement lists all records whose NAME starts with 'Ki' and has any records following 'Ki'.
sqlite> SELECT * FROM COMPANY WHERE NAME LIKE 'Ki%'; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 6 Kim 22 South-Hall 45The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than
The following SELECT statement lists all records whose NAME starts with 'Ki', regardless of the records following 'Ki'.
sqlite> SELECT * FROM COMPANY WHERE NAME GLOB 'Ki*'; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 6 Kim 22 South-Hall 45The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than
After the SELECT statement, lists the AGE (age) values that are25or27All records.
sqlite> SELECT * FROM COMPANY WHERE AGE IN ( 25, 27 ); ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 2 Mark 25 000.0 15The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 4 Rich 25 Mond-David 65The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 5 Texas 27 000.0 85The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than
The following SELECT statement lists the AGE (age) values that are neither25Nor27All records.
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 45The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 7 James 24 Houston 10The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than
After the SELECT statement, lists the AGE (age) values in25and27All records between
sqlite> SELECT * FROM COMPANY WHERE AGE BETWEEN 25 AND 27; ID NAME AGE ADDRESS SALARY ---------- ---------- ---------- ---------- ---------- 2 Mark 25 000.0 15The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 4 Rich 25 Mond-David 65The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than 5 Texas 27 000.0 85The following SELECT statement uses SQL subquery, where the subquery finds the SALARY of the AGE (age) field greater than
All records of 000, followed by the WHERE clause together with the EXISTS operator, to list all records of the external query that have AGE existing in the results returned by the subquery 65000.0-
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 SQL subquery, where the subquery finds the SALARY of the AGE field greater than 65All records of 000, as well as those with the > operator used in the WHERE clause, list all records of the external query whose AGE is greater than the AGE 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