VisualBuilder
  Home > Database > Tutorials > SQL AND - SQL tutorial
Tell a friend
Link to us
Total Members
      Members: 84648
     
Sitemap Forum Chat
Home
SQL Tutorial Tutorial Home
1 . History of SQL
2 . SQL Components: An Overview
3 . Data Types in SQL
4 . Planning a Database: An Example: -
5 . Creating Tables
6 . Inserting Data using Insert
7 . Getting Data with Select Statement
8 . Working with Operators
9 . Selecting Unique rows with Distinct
10 . Arraging Data with Order by
11 . Working with Strings
12 . SQL AND
13 . Basic SQL Funtion
14 . Sub-Queries, Nested Queries
15 . Constraints in the Table
16 . SQL UPDATE
17 . View
18 . Relationships in a RDBMS
19 . Structure Modification,Updation and Deletion
 
Database Group Home
Database Discussion
Database Members (506)
Database Resources
Database Source Code (0)
Database Articles (0)
Database Blogs
Database Jobs
Database Components (0)
Database Books
Database Websites (0)
Database News (0)
Database Q & A (14)
- Database Ask Question
- Database Questions
- Database Unanswered Questions
 
GROUPS
.NET
ASP.NET
.NET
C#
ASP
Visual Basic
Java
Java
JSP
EJB
Other
Delphi
C++
Ajax
UML
JavaScript
PHP
Web Design
Web Hosting
SQL Server
Oracle
Project Management
More Groups

 
LEARNING CENTER
TUTORIALS
.NET
.NET Tutorial
ASP Tutorial
ASP.NET Database Tutorial
ASP.NET Development Tips
ASP.Net Security,Internationalisation And Deployment
ASP.NET Server Controls Tips
ASP.NET Tutorial
C Sharp Tutorial
Web Development
Flex Tutorial
HTML Tutorial
Learn AJAX Tutorial
PHP Tutorial
Software Development
Database Tutorial
SQL Tutorial
UML Tutorial
Java
Ant Tutorial
EJB 3 Tutorial
Grails Tutorial
Hibernate Tutorial
Java 1.6 Tutorial
Java Tutorial
Java Web Component Tutorial
Java XML Tutorial
JDBC Tutorial
JDK1.5 Tutorial
JSF Tutorial
JSP And J2EE Design Tutorial
JSP Tutorial
Service-Oriented Architecture (SOA) Using Java Web Services Tutorial
Spring Tutorial
Struts Tutorial

RESOURCES
Q & A (436 )
Source Code (3275 )
Articles (11 )
Components (1589 )
News (888 )
Websites (1207 )

SUBMISSIONS
Submit Article
Submit Website
Submit News
Submit Source Code
Submit Component

COMMUNITY
Members Directory
Discussion Forum
Chat

SITE
About Us
Sitemap
Search
Contact Us
Link To Us
Feedback
Tell a Friend
Partners
Advertise


Database sql Tutorial
 SQL AND
  << Prev: Working with Strings Next: Basic SQL Funtion >>

The SQL AND clause is used when you want to specify more than one condition in your SQL WHERE clause, and at the same time you want all conditions to be true. For example if you want to select all customers with FirstName "John" and LastName "Smith", you will use the following SQL expression:


SELECT * FROM Customers WHERE FirstName = 'John' AND LastName = 'Smith'


The result of the SQL query above is:




















FirstName LastName Email DOB Phone
John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222


The following row in our Customer table, satisfies the second of the conditions (LastName = 'Smith'), but not the first one (FirstName = 'John'), and that's why it's not returned by our SQL query:


 

















FirstName LastName Email DOB Phone
James Smith jim@supergig.co.uk 20/10/1980 416 323-8888


SQL OR


The SQL OR statement is used in similar fashion and the major difference compared to the SQL AND is that OR clause will return all rows satisfying any of the conditions listed in the WHERE clause. If we want to select all customers having FirstName 'James' or FirstName 'Paula' we need to use the following SQL statement:


SELECT * FROM Customers WHERE FirstName = 'James' OR FirstName = 'Paula';


The result of this query will be the following:



























FirstName LastName Email DOB Phone
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Smith jim@supergig.co.uk 20/10/1980 416 323-8888


You can combine AND and OR clauses anyway you want and you can use parentheses to define your logical expressions. Here is an example of such a SQL query, selecting all customers with LastName 'Brown' and FirstName either 'James' or 'Paula':


SELECT * FROM Customers WHERE (FirstName = 'James' OR FirstName = 'Paula') AND LastName = 'Brown';


The result of the SQL expression above will be:




















FirstName LastName Email DOB Phone
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232


SQL IN


The SQL IN clause allows you to specify discrete values in your SQL WHERE search criteria. THE SQL IN syntax looks like this:


SELECT Column1, Column2, Column3, … FROM Table1 WHERE Column1 IN (Valu1, Value2, …) ;


Lets use the EmployeeHours table to illustrate how SQL IN works:


 





















































Employee Date Hours
John Smith 5/6/2004 8
Allan Babel 5/6/2004 8
Tina Crown 5/6/2004 8
John Smith 5/7/2004 9
Allan Babel 5/7/2004 8
Tina Crown 5/7/2004 10
John Smith 5/8/2004 8
Allan Babel 5/8/2004 8
Tina Crown 5/8/2004 9


Consider the following SQL query using the SQL IN clause:


SELECT * FROM EmployeeHours WHERE Date IN ('5/6/2004', '5/7/2004');


This SQL expression will select only the entries where the column Date has value of '5/6/2004' or '5/7/2004', and you can see the result below:








































Employee Date Hours
John Smith 5/6/2004 8
Allan Babel 5/6/2004 8
Tina Crown 5/6/2004 8
John Smith 5/7/2004 9
Allan Babel 5/7/2004 8
Tina Crown 5/7/2004 10

We can use the SQL IN statement with another column in our EmployeeHours table:


SELECT * FROM EmployeeHours WHERE Hours IN (9, 10);


The result of the SQL query above will be:

























Employee Date Hours
John Smith 5/7/2004 9
Tina Crown 5/7/2004 10
Tina Crown 5/8/2004 9

SQL BETWEEN


The SQL BETWEEN keyword define a range of data between 2 values. The SQL BETWEEN syntax looks like this:


SELECT Column1, Column2, Column3, … FROM Table1 WHERE Column1 BETWEEN Value1 AND Value2 ;


The values defining the range for SQL BETWEEN clause can be dates, numbers or just text. It gives you the ability to specify a range in your search criteria. We are going to use the Customers table to show how SQL BETWEEN works:









































FirstName LastName Email DOB Phone
John Smith John.Smith@yahoo.com 2/4/1968 626 222-2222
Steven Goldfish goldfish@fishhere.net 4/4/1974 323 455-4545
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Smith jim@supergig.co.uk 20/10/1980 416 323-8888


Consider the following SQL BETWEEN statement:


SELECT * FROM Customers WHERE DOB BETWEEN '1/1/1975' AND '1/1/2004' ;


The SQL BETWEEN statement above will select all Customers having DOB column between '1/1/1975' and '1/1/2004' dates. Here is the result of this SQL expression:


























FirstName LastName Email DOB Phone
Paula Brown pb@herowndomain.org 5/24/1978 416 323-3232
James Smith jim@supergig.co.uk 20/10/1980 416 323-8888

  << Prev: Working with Strings Next: Basic SQL Funtion >>
Database Sql Tutorial Home
Give feedback and win a prize.

 
   Printer Friendly
   Email to a friend
   Add to my Favourites    
  Download PDF version
   Report Bad Submissions
   Submit Feedback
 
  Delicious   Digg   Technorati   Blink   Furl   Reddit   Newsvine   Google Click each image to add
this page to each site.
 
 
Welcome Guest Signup
MEMBER'S PANEL
EMAIL
PASSWORD
Forgot your password?
New User? Click Here!
 
Resend Activation Email!
 
SEARCH
 
 
LINKS
VoIP Internettelefonie
cell phone batteries
conferencing web
Video Surveillance
Gift to Pakistan
 
ADVERTISEMENT
 
PARTNER LIST

More
 
 
 

Home | Login | About Us | Contact Us | Privacy Policy | Advertising