SQL Where Clause

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

In this tutorial, we will learn about where clause.

The WHERE statement limits the rows retrieved to those meeting the search condition.

SELECT column_name(s) FROM table_name WHERE column_name operator_value

ID Category Author Tutorial
1 PHP Hassan what is php
2 MySql James what is sql
3 Ajax Andrew what is ajax
4 ASP James ASP Introduction
5 C++ Andrew C++ Basics

Now we want to only records of an author whose name is jamaes, t.

select * from tutorials where author='James'

you will retrieve following result.

ID Category Author Tutorial
2 MySql James what is sql
4 ASP James ASP Introduction

Tip:Numerice values always used without quotes

Correct: Select * from tutorials where id=5;
In Correct: Select * from tutorials where id='5';

Tip: text values always used with quotes

Correct: Select * from tutorials where author='James';
In Correct: Select * from tutorials where id=James;

Operator Description
=

Equal

Example:
Select * from tutorials where author='James';

It will show only records related author james

<> Not equal
Select * from tutorials where author <> 'James';
It will show only records that is not related to author james
> Greater than
Select * from tutorials where id>2 ;
It will show only records whose are greater than 2
< Less than

Select * from tutorials where id<3 ;
It will show only records whose are less than 3
>= Greater than or equal

Select * from tutorials where id>=3 ;
It will show only records whose id is greater than or equal to 3
<= Less than or equal
Select * from tutorials where id<=3 ;
It will show only records whose id is less than or equal to 3
BETWEEN Between an inclusive range
Select * from tutorials WHERE date BETWEEN 'Jan-06-1999' AND 'Jan-10-1999'
It will show only records between Jan-06-1999 to Jan-10-1999
LIKE Search for a pattern
SELECT * FROM tutorial WHERE author LIKE '%an%'
It will show only records whose whose name contains 'an'
IN If you know the exact value you want to return for at least one of the columns
SELECT * FROM tutorials
WHERE author IN ('Andrew', 'Hassan')
It will show only records whose author name contains Andrew, Hassan

Bookmark This Page