AND operator in sql

AND operator in sql

Blog Bustle 15/03/2020

AND operator in sql is used with where clause to filter sql query result. We can use sql AND operator with select, update and delete sql query using where clause.

Note: Remember that when we combine two conditions by using AND operator in sql, both conditions should be true. If both conditions are not true, query will return no result.

Syntax

The basic syntax of AND operator in sql

1. Sql AND operator with select query

select column_1, column_2, column_3, ....
from table_name
where condition_1 AND condition_2 AND condition_3 .... ;

2. Sql AND operator with update query

update table_name
set column_1 = value_1,
column_2 = value_2,
column_3 = value_3,
....
where condition_1 AND condition_2 AND condition_3 .... ;

3. Sql AND operator with delete query

delete column_1, column_2, column_3, ....
from table_name
where condition_1 AND condition_2 AND condition_3 .... ;

Here are some examples which will help you to understand use of AND operator in sql.

I have student table.

id name city state
1 gaurav lucknow uttar pradesh
2 robin agra uttar pradesh
3 neha amritsar punjab
4 gurmeet patiala punjab

I want to find all students who belong to lucknow and their name is gaurav.

select * from student where city = 'lucknow' and name = 'gaurav';

id name city state
1 gaurav lucknow uttar pradesh

If i want to find all students id who belong to punjab state and patiala city.

select id from student where city = 'patiala' and state = 'punjab';

id
4

Remember that first select database then run any sql query related to that database otherwise you will get 'no database selected' error.

So we can easily understand use of AND operator in sql by above examples. Hope you like this tutorial.

Other important links

Sql LIKE Operator

Sql where clause

Sql distinct keyword

Sql rename table query

Insert into select query

AND operator in sql searches:

Tags: #mysql