SQL And Or Operator

free php tutorials, php classes, php utilities, free tools

The AND & OR operators are used to filter records based on more than one condition.

  • The AND operator shows a record if both the first condition and the second condition is true.
  • The OR operator shows s a record if either the first condition or the second condition is true.

The Example of AND Operator

select * from tutorials where author='Andrew' and category='Ajax'

you will retrieve following result.

ID Category Author Tutorial
3 Ajax Andrew what is ajax

We tell query only show record author should be Andrewand category should be Ajax

The Example of Or Operator

select * from tutorials where author='Andrew' or category='PHP'

this will retrieve following result.

ID Category Author Tutorial
1 PHP Hassan what is php
3 Ajax Andrew what is ajax
5 C++ Andrew C++ Basics

We tell query only show record author should be Andrew or category should be PHP

And & OR Combination

You can also combine AND and OR (use parenthesis to form complex expressions).

Now we want to select only the persons with the last name equal to "Svendson" AND the first name equal to "Tove" OR to "Ola":

SELECT * FROM tutorials WHERE
author='Andrew'
AND (category='Ajax' OR FirstName='C++')
ID Category Author Tutorial
3 Ajax Andrew what is ajax
5 C++ Andrew C++ Basics

Bookmark This Page