Loading

SQL AND / OR Operator

Submitted by: 
Language: 
Visitors have accessed this post 4768 times.


The AND/OR can be used to make a complex or multiple condition for the WHERE clause while selecting, updating or deleting some data.

The AND works as follows

Condition 1 AND condition 2 AND……… condition N

We can use as many conditions as we want with the AND. This condition’s result will only come out to be true if all the conditions are specifies, just like the and truth table

False False False
False True False
False True False
True True True

Similarly we can specify multiple conditions with the OR operator, in OR if one condition is true then the overall condition is accepted to be true no matter how false other conditions are as long as one condition is true

False False False
False True True
False True True
True True True

Now How to use the AND /OR in SQL

Syntax

  SELECT * FROM TABLE WHERE condtion 1 AND condition 2

Consider the following table

Firstname Lastname Age Maritalstatus Country
John Smith 40 Married Usa
Mathew Simon 30 Married UK
Bill Steve 20 Single Usa
Amanda Rogers 28 Married Germany
Steve Hills 30 Single france

Example

  SELECT * FROM Users WHERE country=”USA” AND Age=20

Firstname Lastname Age Maritalstatus Country
Bill Steve 20 Single Usa

Similarly

  SELECT * FROM Users WHERE country=”USA” OR Age=30

Firstname Lastname Age Maritalstatus Country
John Smith 40 Married Usa
Mathew Simon 30 Married UK
Bill Steve 20 Single Usa
Steve Hills 30 Single france



Comments

thanq sir.. thanq very much...

Add new comment

(If you're a human, don't change the following field)
Your first name.
(If you're a human, don't change the following field)
Your first name.

Filtered HTML

  • You may insert videos with [video:URL]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
To prevent automated spam submissions leave this field empty.