SQL AND Operator
The AND
operator in SQL is used to combine multiple conditions in a WHERE
clause. It ensures that only rows satisfying *all* the specified conditions are included in the result set.
AND: Definition and Usage
AND
acts as a logical conjunction. It's like saying "this condition *and* that condition must be true". If any of the conditions connected by AND
are false, the entire expression becomes false, and the row is excluded from the results.
Syntax
Syntax
SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
Example Database: Customers Table
The following examples use this sample 'Customers' table:
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country |
---|---|---|---|---|---|---|
1 | Alfreds Futterkiste | Maria Anders | Obere Str. 57 | Berlin | 12209 | Germany |
2 | Ana Trujillo Emparedados y helados | Ana Trujillo | Avda. de la Constitución 2222 | México D.F. | 05021 | Mexico |
3 | Antonio Moreno Taquería | Antonio Moreno | Mataderos 2312 | México D.F. | 05023 | Mexico |
4 | Around the Horn | Thomas Hardy | 120 Hanover Sq. | London | WA1 1DP | UK |
5 | Berglunds snabbköp | Christina Berglund | Berguvsvägen 8 | Luleå | S-958 22 | Sweden |
6 | Gusto Grezzo | Giovanni Rovelli | Via Degli Ortolani, 10 | Reggio Emilia | 42100 | Italy |
7 | Gourmet Lanchonetes | André Fonseca | Av. Brasil 2300 | Campinas | 01000 | Brazil |
8 | Grandma Kelly's Homestead | Regina Murphy | 183 Main St. | Springfield | 98100 | USA |
9 | Great Lakes Food Market | Howard Snyder | 2732 Baker Blvd. | Eugene | 97403 | USA |
10 | Grupo Grabados | Françisco Chang | C/ Araquil, 67 | Madrid | 28023 | Spain |
Examples
Multiple Conditions
This query retrieves customers from Spain whose names start with 'G'.
Syntax
SELECT *
FROM Customers
WHERE Country = 'Spain' AND CustomerName LIKE 'G%';
Output
CustomerID | CustomerName | ContactName | Address | City | PostalCode | Country
--------------------------------------------------------------------------------------------------
10 | Grupo Grabados | Françisco Chang | C/ Araquil, 67 | Madrid | 28023 | Spain
AND with Multiple Conditions
This query demonstrates the use of multiple AND
conditions.
Syntax
SELECT * FROM Customers
WHERE Country = 'Germany'
AND City = 'Berlin'
AND PostalCode > 12000;
Output
(Only customers from Germany, living in Berlin, with PostalCode > 12000 will be returned)
Combining AND and OR (with parentheses)
Correctly using parentheses with AND
and OR
is crucial for correct logic.
Syntax
SELECT * FROM Customers
WHERE Country = 'Spain'
AND (CustomerName LIKE 'G%' OR CustomerName LIKE 'R%');
Output
(Only Spanish customers whose names start with 'G' or 'R' will be returned)
Combining AND and OR (without parentheses - incorrect)
Illustrating the importance of parentheses. Without them, the logic is different.
Syntax
SELECT * FROM Customers
WHERE Country = 'Spain'
AND CustomerName LIKE 'G%' OR CustomerName LIKE 'R%';
Output
(This will return all customers whose names start with 'R', plus any customers from Spain whose names start with 'G'. The result is different from the previous example because of the missing parentheses.)