Apart from standard relational operators (= and >), SQL has some other operators that can be used in conditions.
Operator What it does?
BETWEEN value-1 AND value-2 Checks whether the value is in the given range. The range is inclusive of the given values.
IN(list) Checks whether the value is matching with any one of the values given in the list. List contains values separated by comma(,).
LIKE pattern Checks whether the given string is matching with the given pattern. More on this later.
IS NULL and IS NOT NULL Checks whether the value is null or not null.
Table 2: SQL Operators.
Now, let us see how to use these special operators of SQL.
BETWEEN ... AND Operator
Checks whether value is in the given range. The range includes all the values in the range including the min and max values. This supports DATE type data also.
To display the list of course where DURATION is in the range 20 to 25 days, enter:
where duration between 20 and 25;
Note: BETWEEN.. AND is alternative to using >= and <= operators.
Compares a single value with a list of values. If the value is matching with any of the values given in the list then condition is taken as true.
The following command will retrieve all courses where duration is either 20 or 30 days.
where duration in (20,30);
The same condition can be formed even without IN operator using logical operator OR as follows:
Select name from courses where duration = 20 or duration = 30;
However, it will be more convenient to user IN operator compared with multiple conditions compared with OR operator.
This operator is used to search for values when the exact value is not known. It selects rows that match the given pattern. The pattern can contain the following special characters.
% Zero or more characters can take the place of %.
_ (underscore) Any single character can take the place of underscore. But there must be one letter.
To select the courses where the course name contains pattern .NET, enter:
select name,duration, fee from courses
where name like '%.NET%'
NAME DURATION FEE
-------------------- --------- ---------
VB.NET 30 5500
ASP.NET 25 5000
The following example selects courses where second letter in the course code is “b” and column PREREQUISITE contains word “programming”.
select * from courses
where ccode like '_b%' and prerequisite like '%programming%';
CCODE NAME DURATION FEE PREREQUISITE
----- -------------------- --------- --------- ------------------------
vbnet VB.NET 30 5500 Windows and programming
Remember LIKE operator is case sensitive. In the above example, if CCODE contains value in uppercase (VB), then it won’t be a match to the pattern.
IS NULL and IS NOT NULL operators
These two operators test for null value. If we have to select rows where a column is containing null value or not null value then we have to use these operators.
For example the following SELECT command will select all the courses where the column FEE is null.
select * from courses
where fee is null;
Though Oracle provides NULL keyword, it cannot be used to check whether the value of a column is null. For example, the following condition will always be false as Oracle treats two null values as two different values.
select * from courses
where fee = null;
The above command does NOT work as fee though contains null value will not be equal to NULL. SO, we must use IS NULL operator.
Selecting distinct values
DISTINCT clause of SELECT command specifies only distinct values of the specified column must be selected.
The following SELECT command will display only distinct course fee values from COURSES table.
select distinct fee from courses;
Whereas the same query without DISTINCT clause will select the following.
select fee from courses;
Editing SQL Buffer
Whenever you enter an SQL command in SQL*Plus, it is stored in an area in the memory called as SQL Buffer. It is possible to edit the command that is stored in SQL Buffer using a set of commands provided by SQL*Plus. All these commands are SQL*Plus commands.
Note: SQL*PLUS commands like DESCRIBE are not stored in the buffer. Only SQL commands are stored in the buffer.
The following list of SQL*PLUS commands are used for editing and other operations related to SQL buffer.
1. What is DBMS? A Database Management System (DBMS) is a program that controls creation, maintenance and use of a database. DBMS can be...
1.Write a program to print the following format WELCOME TO PL/SQL PROGRAMMING BEGIN DBMS_OUTPUT.PUT_LINE('WELCOME TO PL/SQL PROGRAMMING...
DECLARE emp_name_c CHAR(32); emp_name_v VARCHAR(32); BEGIN emp_name_c := 'sateesh.bagadhi'; emp_name_v := 'sateesh.ba...
Step1) Write the PL/SQL code in the note pad and save it as with extension name .sql ex: "test.sql" DECLARE x NUMBER; BEGI...