00:00

MySQL AND Condition

Last updated: 4 min read

The AND operator combines conditions so that a row must satisfy all of them. It is how you narrow a search: hotels in Goa and rated 4+ and under ₹7,000.

1. Syntax and Meaning

syntax.sql
WHERE condition1 AND condition2 [AND condition3 ...]
AND keeps rows that satisfy every conditionTwo overlapping circles: hotels in Goa and hotels rated 4 or more. AND returns only the overlap: hotels that are in Goa and rated 4 or more.city = 'Goa'rating >= 4AND = the overlap: both conditions TRUE
Figure 1: AND narrows the result. Every extra AND condition can only remove rows, never add them.

Table: hotels (7 rows)

hotel_idnamecityratingpricepoolopen_since
1Sea BreezeGoa4.5620012012
2Palm CourtGoa3.8390012018
3Hill ViewShimla4.2540002009
4Lagoon BayGoa4.1880012021
5City InnJaipur3.5250002015
6Fort PalaceJaipur4.71200012005
7Snow PeakShimlaNULL470002023

2. Two Conditions

query.sql
SELECT name, city, rating FROM hotels WHERE city = 'Goa' AND rating >= 4;

Result: 2 rows

namecityrating
Sea BreezeGoa4.5
Lagoon BayGoa4.1

3. Three or More Conditions

query.sql
SELECT name, city, rating, price
FROM hotels
WHERE city = 'Goa'
  AND rating >= 4
  AND price < 7000;

Result: 1 row

namecityratingprice
Sea BreezeGoa4.56200
query.sql
SELECT name FROM hotels WHERE pool = TRUE AND open_since >= 2010 AND rating IS NOT NULL;

Result: 3 rows

name
Sea Breeze
Palm Court
Lagoon Bay

4. AND Truth Table (with NULL)

ABA AND B
TRUETRUETRUE
TRUEFALSEFALSE
FALSENULLFALSE
TRUENULLNULL
NULLNULLNULL
query.sql
SELECT (1=1) AND (2=2) AS t_t, (1=1) AND (1=2) AS t_f, (1=2) AND NULL AS f_null, (1=1) AND NULL AS t_null;

Result: 1 row

t_tt_ff_nullt_null
100NULL

Snow Peak's rating is NULL, so city = 'Shimla' AND rating > 4 is NULL for it, and it isn't returned:

query.sql
SELECT name, rating FROM hotels WHERE city = 'Shimla' AND rating > 4;

Result: 1 row

namerating
Hill View4.2

5. AND for Ranges

Two conditions on the same column make a range. BETWEEN is a shortcut for the inclusive form:

query.sql
SELECT name, price FROM hotels WHERE price >= 4000 AND price <= 6500;

Result: 3 rows

nameprice
Sea Breeze6200
Hill View5400
Snow Peak4700
query.sql
SELECT name, price FROM hotels WHERE price BETWEEN 4000 AND 6500;

Result: 3 rows

nameprice
Sea Breeze6200
Hill View5400
Snow Peak4700

6. AND in UPDATE and DELETE

update-and.sql
UPDATE hotels SET price = price - 500
WHERE city = 'Jaipur' AND pool = FALSE;
SELECT name, price FROM hotels WHERE city = 'Jaipur';

Result: 2 rows

nameprice
City Inn2000
Fort Palace12000

7. && as a Synonym

MySQL accepts && for AND, but it is deprecated and non-standard. Write AND.

query.sql
SELECT name FROM hotels WHERE city = 'Goa' && pool = TRUE;

Result: 3 rows

name
Sea Breeze
Palm Court
Lagoon Bay

8. AND and Indexes

For several equality/range conditions joined by AND, a composite index covering them works best. Put equality columns first, then the range column:

explain.sql
CREATE INDEX idx_city_rating ON hotels (city, rating);
EXPLAIN SELECT name FROM hotels WHERE city = 'Goa' AND rating >= 4;

Result: 1 row

typekeykey_lenrowsExtra
rangeidx_city_rating662Using index condition
The order of conditions in the WHERE clause doesn't matter; the optimizer decides the evaluation order. The order of columns in the index does matter.

9. Practice Exercises

Exercise 1: Hotels with a pool, rated above 4, opened after 2010.
solution.sql
SELECT name FROM hotels WHERE pool = TRUE AND rating > 4 AND open_since > 2010;

Result: 2 rows

name
Sea Breeze
Lagoon Bay
Exercise 2: Jaipur hotels between ₹2,000 and ₹5,000.
solution.sql
SELECT name, price FROM hotels WHERE city = 'Jaipur' AND price BETWEEN 2000 AND 5000;

Result: 1 row

nameprice
City Inn2000

10. Summary

  • AND returns rows where every condition is TRUE.
  • FALSE AND anything is FALSE; TRUE AND NULL is NULL (row excluded).
  • Use AND for multi-filter searches and ranges (or BETWEEN).
  • A composite index on the AND-ed columns speeds queries up; write AND rather than &&.