Home SQL Basic SQL AND OR Operator
09.09.2010
Main Menu
Programming
.NET Controls
Operating System
Databases
Articles
Add to: Digg Add to: Del.icoi.us Add to: Reddit Add to: StumbleUpon Add to: Slashdot Add to: Yahoo Add to: Technorati Add to: Google
SQL AND OR Operator

In the previous tutorial you have learnt how to use the WHERE keyword, in this tutorial we will expand the usage of th WHERE keyword in conjunction of the OR/AND keyword. SQL statement can have compound condition and we use the OR/AND to form a compound condition. You can have as many compound condition as you like as there is no limit to it.

SYNTAX:

SELECT column_name
FROM table_name
WHERE your condition
{[AND|OR] your condition}

Table: Student 

StudentID LastName FirstName DOB CourseFees
1 John Astone 07/15/1976 1000.00
2 Bob Eastwood 02/13/1935 1500.00
3 Jane Hollywood 03/23/1939 2000.00
4 Bob Eastwood 03/19/1980 3000.00

EXAMPLE #1

This SQL statement will return all student record with first name equal to Eastwood and that they pay their course fees more then $2000.

SELECT     StudentID, LastName, FirstName, DOB, CourseFees
FROM         Student
WHERE     (FirstName = 'Eastwood' AND CourseFees > 2000)

RESULT:

StudentID LastName FirstName DOB CourseFees
4 Bob Eastwood 03/19/1980 3000.00

 
EXAMPLE #2

This SQL statement will return all student record with first with first name equal to Eastwood or the course fees is greater then $1500.

SELECT     StudentID, LastName, FirstName, DOB, CourseFees
FROM         Student
WHERE     (FirstName = 'Eastwood' OR CourseFees > 1500)

RESULT:

StudentID LastName FirstName DOB CourseFees
2 Bob Eastwood 02/13/1935 1500.00
3 Jane Hollywood 03/23/1939 2000.00
4 Bob Eastwood 03/19/1980 3000.00


Note: You can  also use the keyword AND/OR in one statement.



Comments (0)
Write comment
Your Contact Details:
Comment:
[b] [i] [u] [url] [quote] [code] [img]   
:D:angry::angry-red::evil::idea::love::x:no-comments::ooo::pirate::?::(
:sleep::););)):0
Security
Please input the anti-spam code that you can read in the image.

!joomlacomment 4.0 Copyright (C) 2009 Compojoom.com . All rights reserved."