Combining conditions with AND #
Sometimes one condition isn't enough. The AND keyword lets you combine multiple conditions in a WHERE clause, keeping only the rows where all of them are true.
Syntax #
SELECT
column_name
FROM
table_name
WHERE
condition_1
AND condition_2;
A row is only included if condition_1 and condition_2 are both true.
Example #
SELECT
*
FROM
customers
WHERE
gender = 'F'
AND age > 65;
Result:
+----+------------+-----------+-----+--------+
| id | first_name | last_name | age | gender |
+----+------------+-----------+-----+--------+
| 2 | Jonathan | Dixon | 71 | F |
| 4 | Juan | Campos | 99 | F |
| 8 | Tammy | Woods | 86 | F |
+----+------------+-----------+-----+--------+
Kyle, who is 'F' but only 62 years old, is filtered out because she fails the second condition. You can chain more than two conditions by adding more AND keywords.
Exercise #
Write a query that retrieves all columns from the customers table, keeping only the rows where gender is 'F' and age is greater than 65.
- The result has an
agecolumn. - The result contains exactly the rows where
genderis'F'andageis greater than65, and no others.
Solution
SELECT
*
FROM
customers
WHERE
gender = 'F'
AND age > 65;
Basic Data Retrieval
Filtering Data
Aggregating Data
Joining Tables
Creating and Managing Tables
Transactions and Data Integrity