AND,OR,NOT and WHERE Conditions





In this post we will be studying about AND, OR and NOT conditions The AND, OR and NOT condition are combined with where clause to retrieve records based on more conditions.

Using And,OR,Not condition, we can retrieve get into the deep and retrieve specific records from table. You will see the examples below how we can do it.




  • AND Condition

Syntax

SELECT column_name_1, column_name_2 from table_name
WHERE condition_1 AND condition_2
ID Name City

State
1 Raj Mumbai

Maharashtra
2 Rahul Nashik

Maharashtra
3SaurabhNagar

Maharashtra
4Ryanpanaji

Goa


Example

SELECT  * from Detail where state='Maharashtra' AND city='Mumbai'




Output

ID Name City

State
1 Raj Mumbai

Maharashtra




  • OR Condtion


Syntax

SELECT column_name_1, column_name_2 from table_name
WHERE condition_1 OR condition_2
ID Name City

State
1 Raj Mumbai

Maharashtra
2 Rahul Nashik

Maharashtra
3SaurabhNagar

Maharashtra
4Ryanpanaji

Goa


Example


SELECT  * from Detail where state='Maharashtra' OR city='Nashik'



Output

ID        Name        City       State
2         Rahul         Mumbai     Maharashtra




  • NOT condition


Syntax

WHERE column_name_1, column_name_2 from table_name
WHERE NOT condition
ID Name City

State
1 Raj Mumbai

Maharashtra
2 Rahul Nashik

Maharashtra
3SaurabhNagar

Maharashtra
4Ryanpanaji

Goa


Example

SELECT  * from Detail where not State='Maharashtra'



Output
ID Name City

State
4Ryanpanaji

Goa




In this post we will be studying about WHERE condition. The WHERE  clause acts like a filter. using WHERE we can extract only those records which matches the given condition.

Syntax

SELECT column_name_1,column_name_2 from table_name
WHERE condition;


With SELECT  statement WHERE clause can be used with INSERT and UPDATE statement as well.

Consider following table for example.
ID Name

City
1 Raj

Mumbai
2 Rahul

Nashik
3Saurabh

Nagar


Example

Suppose we want the details of first record in the table.


select * from Details where Id=1.


Output

ID Name

City
1 Raj

Mumbai


Example_2

Suppose we want the details of second record in the table.

We can also use string to filter records.

select * from Details where city='Nashik'.


Output

ID Name

City
2 Rahul

Nashik
AND,OR,NOT and WHERE Conditions AND,OR,NOT and WHERE Conditions Reviewed by LanguageExpert on August 06, 2017 Rating: 5

2 comments