We use the AND
keyword with WHERE
in SQL when we want to filter based on many conditions.
The WHERE AND
keyword selects data from the table based on multiple conditions.
Note: We can have any number of
AND
conditions, but we must specify the column name separately for everyAND
condition.
SELECT column_name
FROM table_name
WHERE condition
AND condition;
Let’s assume we have a table called Person
with the columns ID
, name
, age
, state
, and gender
.
Now, we want to get the name and ages of all female persons in the state of Lagos.
How do we get this information from our table?
The following code shows how to do this using the WHERE AND
keyword in SQL.
CREATE TABLE Person ( ID int, name varchar(100), age int, gender varchar(10), state varchar(15) ); -- Insert data INSERT INTO Person VALUES (1,'Sharon Peller','16','Female','Kogi'); INSERT INTO Person VALUES (2,'Paul Dons','20','Male','Lagos'); INSERT INTO Person VALUES (3,'Ameera Abedayo','28','Female','Ibadan'); INSERT INTO Person VALUES (4,'Maria Elijah','25','Female','Lagos'); INSERT INTO Person VALUES (5,'David Hassan','30','Male','Abuja'); INSERT INTO Person VALUES (6,'Niniola Disu','28','Female','Lagos'); INSERT INTO Person VALUES (7,'Praise Dominion','16','Female','Ibadan'); INSERT INTO Person VALUES (8,'Joe Smith','16','Male','Lagos'); -- Query SELECT name, age FROM Person WHERE gender = 'Female' AND state = 'Lagos';
Person
with the columns ID
, name
, age
, gender
, and state
.Person
table.WHERE AND
keyword.Note: For every
AND
condition, we must specify the column name.
RELATED TAGS
CONTRIBUTOR
View all Courses