HOME

SQL Basic
SQL HOME
SQL Introduction
SQL Select
SQL Where
SQL And & Or
SQL Between
SQL Distinct
SQL Order By
SQL Try It
SQL Insert
SQL Update
SQL Delete
SQL Count

SQL Advanced
SQL Functions
SQL Group By
SQL Aliases
SQL Join
SQL Create
SQL Alter

SQL Quiz
SQL Quiz Test

Please visit our sponsors !

SQL Where Clause

previous next

The WHERE clause is used to specify a selection criterion.


The WHERE Clause 

To conditionally select data from a table, a WHERE clause can be added to the SELECT statement with the following syntax:

SELECT column FROM table WHERE column condition value

With the WHERE clause, these conditions can be used:

Operator Condition
= Equal
<> Not equal
> Greater than
< Less than
>= Greater than or equal
<= Less than or equal
BETWEEN Between an inclusive range
LIKE Explained below

Note: In some versions of SQL the not equal operator <> can be written as !=


Example: Select Persons from a City

To select only the people that live in Sandnes, add a WHERE clause to the SELECT statement like this: 

SELECT * FROM Persons WHERE City='Sandnes'

The "Persons" Table:

LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980
Pettersen Kari Storgt 20 Stavanger 1960

The Result:

LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951
Svendson Tove Borgvn 23 Sandnes 1978
Svendson Stale Kaivn 18 Sandnes 1980


Using Quotes

Note that we have used single quotes around the conditional values in the examples. SQL uses single quotes around text values. Most database systems will also accept double quotes. Numeric values should not be enclosed in quotes.

For Text values:

This is correct:
SELECT * FROM Persons WHERE FirstName='Tove'
This is not correct:
SELECT * FROM Persons WHERE FirstName=Tove

For Numeric values:

This is correct:
SELECT * FROM Persons WHERE Year>1965
This is not correct:
SELECT * FROM Persons WHERE Year>'1965'


The LIKE Condition

The LIKE condition is used to specify a search for a pattern in a column.

The syntax is like this:

SELECT column FROM table WHERE column LIKE pattern

A "%" sign can be used to define wildcards (missing letters in the pattern) both before and after the pattern.


Example: Select Persons with a Name Pattern 

This SQL statement will return persons with a first name that start with an 'O'.

SELECT * FROM Persons WHERE FirstName LIKE 'O%'

This SQL statement will return persons with a first name that end with an 'a'.

SELECT * FROM Persons WHERE FirstName LIKE '%a'

This SQL statement will return persons with a first name that contains the pattern 'la'.

SELECT * FROM Persons WHERE FirstName LIKE '%la%'

All the examples above will return the following result:

LastName FirstName Address City Year
Hansen Ola Timoteivn 10 Sandnes 1951


previous next

Jump to: Top of Page or HOME or Printer Friendly Printer friendly page


Search W3Schools:


What Others Say About Us

Does the world know about us? Check out these places:

Dogpile Alta Vista MSN Google Excite Lycos Yahoo Ask Jeeves


We Help You For Free. You Can Help Us!


W3Schools is for training only. We do not warrant its correctness or its fitness for use. The risk of using it remains entirely with the user. While using this site, you agree to have read and accepted our terms of use and privacy policy.

Copyright 1999-2002 by Refsnes Data. All Rights Reserved


Validate How we converted to XHTML Validate

IISProtect
Password Protect
Your Web Pages



Your own Web Site?

Read W3Schools
Hosting Tutorial



$15 Domain Name
Registration
Save $20 / year!



Advertise
at W3Schools

Only 0.5 CPM



SELECTED LINKS

University Online
Master Degree
Bachelor Degree


Web Software

The Future of
Web Development


Jobs and Careers

Web Security
Web Statistics
Web Standards


PARTNERS

W3Schools
TopXML
VisualBuilder
XMLPitstop
DevelopersDex
DevGuru
Programmers Heaven
The Code Project
Tek Tips Forum
ZVON.ORG
TopXML Search