Exforsys.com
 
Home Tutorials Oracle 9i
 

Tutorial 3 : Restricting and Sorting Data

 
In this week, we will be learning Where and Order By clauses with Logical Operators and Comparison operators. If you haven't completed prior weeks articles, please review them before you read this.
 

 


Week 3: Restricting and Sorting Data

WHERE Clause –



Comparison Operators;
Logical Operator;
Using the Logical Operator, AND;
Using the logical Operator, OR;
Using the Logical Operator, NOT;
Nesting Logical Operators;
Other Comparison Keywords;
Using the Keyword, LIKE;
Using the Keywords, BETWEEN...AND;
Using the Keyword, IN;
Using the Keyword, IS NULL


ORDER BY Clause - Using the ORDER BY Clause
 

Logical Operators

There are six logical operators in SQL, and after introducing them, we'll see how they're used:

=
Equal
<> or != (see manual)
Not Equal
<
Less Than
>
Greater Than
<=
Less Than or Equal To
>=
Greater Than or Equal To


The WHERE clause is used to specify that only certain rows of the table are displayed, based on the criteria described in that WHERE clause. It is most easily understood by looking at a couple of examples.
 
If you wanted to see the EMPLOYEEIDNO's of those making at or over $50,000, use the following:

SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY >= 50000;
 
Notice that the >= (greater than or equal to) sign is used, as we wanted to see those who made greater than $50,000, or equal to $50,000, listed together. This displays:

EMPLOYEEIDNO
------------
010
105
152
215
244
 
The WHERE description, SALARY >= 50000, is known as a condition. The same can be done for text columns:

SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION = 'Manager';
 
This displays the ID Numbers of all Managers. Generally, with text columns, stick to equal to or not equal to conditions, and make sure that any text the appears in the statement is surrounded by single quotes (').
 
More Complex Conditions: Compound Conditions
 
The AND operator joins two or more conditions, and displays a row only if that row's data satisfies ALL conditions listed (i.e. all conditions hold true). For example, to display all staff making over $40,000, use:

SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY > 40000 AND POSITION = 'Staff';
 
The OR operator joins two or more conditions, but returns a row if ANY of the conditions listed hold true. To see all those who make less than $40,000 or have less than $10,000 in benefits, listed together, use the following query:

SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY < 40000 OR BENEFITS < 10000;

AND & OR can be combined, for example:

SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION = 'Manager' AND SALARY > 60000 OR BENEFITS > 12000;

First, SQL finds the rows where the salary is greater than $60,000 or the benefits is greater than $12,000, then taking this new list of rows, SQL then sees if any of these rows satisfies the condition that the Position column if equal to 'Manager'. Subsequently, SQL only displays this second new list of rows, as the AND operator forces SQL to only display such rows satisfying the Position column condition. Also note that the OR operation is done first.
 
To generalize this process, SQL performs the OR operation(s) to determine the rows where the OR operation(s) hold true (remember: any one of the conditions is true), then these results are used to compare with the AND conditions, and only display those remaining rows where the conditions joined by the AND operator hold true.
To perform AND's before OR's, like if you wanted to see a list of managers or anyone making a large salary (>$50,000) and a large benefit package (>$10,000), whether he or she is or is not a manager, use parentheses:
 
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION = 'Manager' OR (SALARY > 50000 AND BENEFIT > 10000);
 
IN & BETWEEN
 
An easier method of using compound conditions uses IN or BETWEEN. For example, if you wanted to list all managers and staff:
 
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE POSITION IN ('Manager', 'Staff');

or to list those making greater than or equal to $30,000, but less than or equal to $50,000, use:
 
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY BETWEEN 30000 AND 50000;

To list everyone not in this range, try:
 
SELECT EMPLOYEEIDNO
FROM EMPLOYEESTATISTICSTABLE
WHERE SALARY NOT BETWEEN 30000 AND 50000;
Similarly, NOT IN lists all rows excluded from the IN list.
 
Using LIKE
 
Look at the EmployeeStatisticsTable, and say you wanted to see all people whose last names started with "L"; try:
 
SELECT EMPLOYEEIDNO
FROM EMPLOYEEADDRESSTABLE
WHERE LASTNAME LIKE 'L%';

The percent sign (%) is used to represent any possible character (number, letter, or punctuation) or set of characters that might appear after the "L". To find those people with LastName's ending in "L", use '%L', or if you wanted the "L" in the middle of the word, try '%L%'. The '%' can be used for any characters, in that relative position to the given characters. NOT LIKE displays rows not fitting the given description. Other possiblities of using LIKE, or any of these discussed conditionals, are available, though it depends on what DBMS you are using; as usual, consult a manual or your system manager or administrator for the available features on your system, or just to make sure that what you are trying to do is available and allowed. This disclaimer holds for the features of SQL that will be discussed below. This section is just to give you an idea of the possibilities of queries that can be written in SQL.
 
 
Logical Operators and Aggregation Download Tutorial

Please review the following sections in detail after you complete the tutorial from the download link above.
 
In the previous section we saw how a single rule could be added to a query using a WHERE clause. While this is useful, usually more than a single rule is required to produce the correct result. To support multiple rules we need to make use of NOT, AND, OR and parentheses.
 

Read Next: Tutorial 4: SQL functions



 

 

Comments


durga_dash said:

  Restricting and sorting data, successfully completed.
November 8, 2005, 4:42 am

deeksha said:

  I am unable to go to the remaining lessons why so??
October 29, 2006, 7:42 pm

veronica said:

  thanku .your effort is great & easy on students to learn.
September 2, 2007, 12:01 am

raja naidu said:

  i am feeling it is used to learn oracle is good .
November 19, 2007, 5:20 am

sansaar said:

  Thank u. its very usefull notes to learn oracle. and examples r also so good and very easy to understand.
January 10, 2008, 3:47 am

Annadurai_Nandhika said:

  Very useful for beginners
February 16, 2008, 12:11 pm

pavani. ... said:

  Hi,

I have some confusion to select Oracle or .net,

Which one is better in these two careers.
February 17, 2008, 1:09 am

kammari venugopal said:

  this is very useful to beginners
March 11, 2008, 7:06 am

ankit said:

  hi,
this is a good site for learning Oracle,Sql.
It is of great use.Thanks.
November 28, 2008, 1:01 am

santhosh said:

  thanku .your effort is great & easy on students to learn
November 29, 2008, 7:17 pm

Ujjal B. Tripura said:

  Its a first time for me. Your tutorial system is very organized and may helpful for novice learner. As a new learner, I am very optimistic that I able improve on various subjects through this site.Thank you.
January 13, 2009, 3:45 am

Post Your Comment:

Members Please Login
Your Name:*
e-mail ID:(required for notification)*
Image Verification: 
 
 Subscribe    

Sponsored Links

 

Subscribe via RSS


Get Daily Updates via Subscribe to Exforsys Free Training via email


Get Latest Free Training Updates delivered directly to your Inbox...

Enter your email address:


 

Subscribe to Exforsys Free Training via RSS
 

 
Partners -  Privacy and Legal Policy -  Site News -  Contact   Sitemap  

Copyright © 2000 - 2009 exforsys.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape