SQLite - WHERE Clause
SQLite - The WHERE Clause
Section titled “SQLite - The WHERE Clause”The SQLite WHERE clause is the primary tool for filtering records. It is used to extract only those records that fulfill a specified condition, allowing you to selectively retrieve, update, or delete data.
If the condition specified in the WHERE clause evaluates to true for a given row, that row is included in the operation. This clause is not exclusive to SELECT statements; it is a crucial part of UPDATE and DELETE statements as well, ensuring that modifications are precise and intentional.
Syntax
Section titled “Syntax”The basic syntax of a SELECT statement with a WHERE clause is as follows:
SELECT column_listFROM table_nameWHERE [condition];The [condition] is an expression that typically involves comparison operators (=, >, <, >=, <=, <>), logical operators (AND, OR, NOT), and other specialized operators like LIKE, IN, BETWEEN, and IS NULL.
Setup: Creating Sample Data
Section titled “Setup: Creating Sample Data”To demonstrate the WHERE clause, let’s create and populate an EMPLOYEES table. This table will serve as the basis for our examples.
CREATE TABLE EMPLOYEES( ID INT PRIMARY KEY NOT NULL, NAME TEXT NOT NULL, AGE INT NOT NULL, DEPARTMENT CHAR(50), SALARY REAL);
INSERT INTO EMPLOYEES (ID,NAME,AGE,DEPARTMENT,SALARY) VALUES (1, 'David', 27, 'IT', 85000.00 );INSERT INTO EMPLOYEES (ID,NAME,AGE,DEPARTMENT,SALARY) VALUES (2, 'Allen', 25, 'HR', 15000.00 );INSERT INTO EMPLOYEES (ID,NAME,AGE,DEPARTMENT,SALARY) VALUES (3, 'Martin', 23, 'Finance', 20000.00 );INSERT INTO EMPLOYEES (ID,NAME,AGE,DEPARTMENT,SALARY) VALUES (4, 'Mark', 25, 'IT', 65000.00 );INSERT INTO EMPLOYEES (ID,NAME,AGE,DEPARTMENT,SALARY) VALUES (5, 'Paul', 32, 'Finance', 21000.00 );INSERT INTO EMPLOYEES (ID,NAME,AGE,DEPARTMENT,SALARY) VALUES (6, 'Kim', 22, 'HR', 45000.00 );INSERT INTO EMPLOYEES (ID,NAME,AGE,DEPARTMENT,SALARY) VALUES (7, 'James', 24, 'Marketing', 10000.00 );INSERT INTO EMPLOYEES (ID,NAME,AGE,DEPARTMENT,SALARY) VALUES (8, 'Chris', 45, 'IT', NULL);Examples
Section titled “Examples”Let’s explore various filtering conditions using the WHERE clause.
- Find all employees in the ‘IT’ department:
sqlite> SELECT * FROM EMPLOYEES WHERE DEPARTMENT = 'IT';
ID NAME AGE DEPARTMENT SALARY-- ----- --- ---------- -------1 David 27 IT 85000.04 Mark 25 IT 65000.08 Chris 45 IT- Find all employees older than 30 OR who earn more than 50,000:
sqlite> SELECT * FROM EMPLOYEES WHERE AGE > 30 OR SALARY > 50000;
ID NAME AGE DEPARTMENT SALARY-- ----- --- ---------- -------1 David 27 IT 85000.04 Mark 25 IT 65000.05 Paul 32 Finance 21000.08 Chris 45 IT- Find all employees whose name starts with ‘M’: The
LIKEoperator is used for pattern matching in strings. The percent sign (%) is a wildcard that matches any sequence of characters.
sqlite> SELECT * FROM EMPLOYEES WHERE NAME LIKE 'M%';
ID NAME AGE DEPARTMENT SALARY-- ------ --- ---------- -------3 Martin 23 Finance 20000.04 Mark 25 IT 65000.0- Find all employees who work in the ‘HR’ or ‘Finance’ departments: The
INoperator provides a concise way to check if a value matches any value in a list.
sqlite> SELECT * FROM EMPLOYEES WHERE DEPARTMENT IN ('HR', 'Finance');
ID NAME AGE DEPARTMENT SALARY-- ------ --- ---------- -------2 Allen 25 HR 15000.03 Martin 23 Finance 20000.05 Paul 32 Finance 21000.06 Kim 22 HR 45000.0- Find all employees whose salary is between 20,000 and 50,000: The
BETWEENoperator is inclusive of the start and end values.
sqlite> SELECT * FROM EMPLOYEES WHERE SALARY BETWEEN 20000 AND 50000;
ID NAME AGE DEPARTMENT SALARY-- ------ --- ---------- -------3 Martin 23 Finance 20000.05 Paul 32 Finance 21000.06 Kim 22 HR 45000.0- Find all employees whose salary has not been recorded: The
IS NULLoperator is used to check forNULLvalues. You cannot use= NULL.
sqlite> SELECT * FROM EMPLOYEES WHERE SALARY IS NULL;
ID NAME AGE DEPARTMENT SALARY-- ----- --- ---------- -------8 Chris 45 ITCommon Pitfalls and Best Practices
Section titled “Common Pitfalls and Best Practices”-
NULLHandling: A common mistake is usingcolumn = NULLorcolumn <> NULL.NULLrepresents an unknown value, so it cannot be compared with standard operators. Always useIS NULLorIS NOT NULL. -
Operator Precedence: When combining
ANDandOR, be aware thatANDhas higher precedence. To ensure your logic is correct, use parentheses to group conditions explicitly. For example:WHERE (DEPARTMENT = 'IT' OR DEPARTMENT = 'HR') AND SALARY > 50000. -
Performance: For large tables, queries can be slow. Columns that are frequently used in
WHEREclauses are excellent candidates for database indexes, which can dramatically speed up data retrieval. We will cover indexes in a later chapter. -
Data Types: Ensure that the data you compare matches the column’s data type. Comparing a string to a numeric column (e.g.,
WHERE AGE = '25') might work in SQLite due to its flexible typing, but it’s bad practice and can lead to unexpected results or poor performance.