SQL Where
Home
SQL Basics:
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 Test
Resources:
SQL Books
SQL Where Clause
The WHERE clause is used to specify a selection criteria.
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
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
Ståle
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
Ståle
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>1965This 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
Jump to : Top Of Page or HOME.
We Help you for free. Please Help us!
Help us correct errors
Help us with spelling and grammar
Tell us what you think
Link to us from your pages
All information in this Web is for training only. We do not make any warranties of its correctness or
its fitness to be used. The risk of using it remains with the user.
Copyright © 1999-2001 by Refsnes Data. All Rights Reserved.
More Tutorials:
HTML XHTML
XML XSL
CSS DTD
WAP WML
JavaScript
DHTML Flash
VBScript ASP
SQL AppML
News:
The Future ofProgramming
Web Security
Browser News
Web Standards
References:
HTML 4.01 Ref
XHTML 1.0 Ref
WML 1.1 Ref
CSS Reference
XSLT Elements
ASCII Reference
Entity Reference
HTML Colors
Color Values
Color Names
WAP Pages
Validation:
HTML XHTML
CSS DTD
XML WML
About W3Scripts
Please link to us
Larger Text?
XML Partners:
Wyszukiwarka
Podobne podstrony:
sql functions aspsql groupby aspsql alter aspsql orderby aspsql alias aspsql create aspsql quiz aspsql count aspsql select aspsql distinct aspsql insert aspsql join aspsql update aspsql intro aspsql tryit aspsql?lete aspsql and or aspsql framework aug94więcej podobnych podstron