Operators(Oracle)
Operators(Oracle)
Arithmetic operators
Assignment operator
Relational operators
Logical operators
Special Operators
Set Operators
Arithmetic operators:-
• The arithmetic operations can be used to create expressions on number and date data.
• The arithmetic operations can be Used to perform any Arithmetic Operations like
Addition,subtraction, Multiplication and Divided by.
• Sql * plus ignores the blank spaces before and after the arithmatic operator .
Example:-
Example:
Example:-
Display the details of the employees Incrementing their salary two times.
Used to perform Division test. Division will display only the Quotient value not the remainder
value. Example 6/2 gives 3 because 2 divides 6 by 3 times.
Example:-
Examples:-
Assignment operators:-
This operator is used for equality test. Used to test the equality of two operands.
Example:-
This operator is used for less than test. Example a<b checks that operand ‘a’ is less than ‘b’ or
not.
Example: Display the details of the employees whose salary is less than 3000.
---------------------------------------------------------------------------------------- ---------------------
Here if you observe we got a result values whose salary is less than the operand 3000.
This operator is used for Greater than test. For example a>b checks the operand ‘a’ is greater
than ‘b’ or not.
Example:
---------------------------------------------------------------------------------------------------
This operator is used for Less than or Equal to test. For example a<=b, here checks whether
operand ‘a’ is less than or equals to operand ‘b’. If a<b then condition is true and if a=b then also
condition is true but if a>b then condition is false.
Example :-
Display the details of Employees whose salary is less than or equal to 3000.
This operator is used to check the Greater than or equal test. For example a>=b checks the
operand ‘a’ is greater than operand ‘b’ or operand ‘a’ is equals to the operand ‘b’.
Example:-
Display the details of Employees whose salary is greater than or equal to 3000.
----------------------------------------------------------------------------------------------------------
Examples:
LOGICAL OPERATORS
AND operator:-
Returns ‘True’ if both component conditions are true. Returns ‘False’ if any one component
condition or Both Component conditions are False.
Example:-
Display the details of Employees whose salary is Greater than 1000 AND also whose salary is
less than 2000.
Sql> SELECT *FROM emp WHERE sal > 1000 AND sal <2000;
----------------------------------------------------------------------------------------------------
job='MANAGER';
Example:
Display the details of Employees whose salary is Greater than 1000 OR also whose salary is less
than 2000.
Sql> SELECT *FROM emp WHERE sal> 1000 OR sal < 2000;
Explanation:-whose salaries more than 1000 or less than 2000 that all emp table display.
The NOT operator returns ‘True’ if the condition is False and returns ‘False’ if the following
condition is True.
Example:
Display the details of employees whose salary is Greater than or Equals to 3000.
Explination:-whose salary less than 3000 that salaries all are comming.
SPECIAL OPERATORS
IN operator:-
In the below example only employees whose empno is (7125,7369,7782) are fetched.
--------------------------------------------------------------------------------------------------------
Not in operator:-
('SMITH','ALLEN','WARD');
Sql> DELETE FROM emp WHERE hiredate NOT IN ('22-DEC-82',' 17-NOV-81');
BETWEEN Operator:-
Example:- in this example all employee records are fetched whose salary is between 2000 and
3000
Sql> SELECT *FROM emp WHERE sal BETWEEN 2000 AND 3000;
------------------------------------------------------------------------------------------------------
Whenever lower bound value is larger than upper bound then it shows ‘no rows selected’
Example:-
Sql> SELECT *FROM emp WHERE sal BETWEEN 3000 AND 2000;
Output:
-- No rows selected
• Not between is an exclusive operator which eliminates range limits from Output.
Example:-
Sql> SELECT *FROM emp WHERE sal NOT BETWEEN 2000 AND 3000;
--------------------------------------------------------------------------------------------------------------
Note:-
Lower bound – ‘value ‘must be lower when compare to ‘upper bound ‘value
Upper bound- ‘value’ must be higher when compare to ‘lower bound ‘value
Syntax:-
Example:- Display the employees whose name is starting with ‘S’ in EMP table.
-----------------------------------------------------------------------------------------------------
Display the employees whose name ends with ‘S’ in EMP table
----------------------------------------------------------------------------------------------------------
Display the employees whose names are having second letter as ‘L’ in EMP table
-----------------------------------------------------------------------------------------------------
Sql> select ename ,hiredate from emp where hiredate like '%JAN%';
EX:
EX:
EX:
EX:
SELECT * FROM EMP WHERE HIREDATE LIKE '%FEB%' OR HIREDATE LIKE '%MAY
%';
SQL> select empno,ename,job ,hiredate from emp where hiredate like '%-FEB-81';
NOTE:
Generally oracle db server will treat these symbol(%, _ ) as "wildcard operators" but not
"special char's".to overcome this problem we must use a pre-defined statement is "escape '\' " .
syntax:-
Display the employees whose name is not ends with ‘S’ in EMP table?
-----------------------------------------------------------------------------------------------------
Display the employees whose names are not having second letter as ‘L’ in EMP table?
Display the employees whose names are not start with ‘S’ in EMP table.?
Sql> select ename ,hiredate from emp where hiredate not like '%JAN%';
Sql> select empno,ename,job from emp where ename not like '_O%';
Display the employees whose names are second letter start with ‘R’ from ending.?
-----------------------------------------------------------------------------------------------------
-----------------------------------------------------------------------------------------------------
syntax:
Ex:
Ex:
Ex:
Waq to display ename,job,sal,comm and also sal+comm from emp table whose ename is
"SMITH" ?
Note: If any arithmetic operator is performing some operation with NULL then it agian returns
NULL only.
Ex: If x=1000;
- To overcome the above problem we should use a predefined function is called as “NVL()”
Nvl(Exp1,Exp2):
Nvl stands for NULL VALUE. It is pre-defined function. is used to replace a user defined value
in place of NULL in the expression. This function is having two arguments are Expression1 and
Expression2.
> If Exp1 is null ------------> returns Exp2 value (user defined value)
Ex:
NVL(NULL,0)
-----------
NVL(NULL,100)
-------------
100
NVL(0,100)
----------
SQL> SELECT ENAME, JOB, SAL, COMM, NVL (COMM,0)+SAL FROM EMP WHERE
ENAME='SMITH';
Pre-defined function which is an extension of NVL () having 3 arguments are Exp1, Exp2,
Exp3.
- If Exp1 is Null -----------> Exp3 value (User Defined value)
- If Exp1 is not null ------> Exp2 value (User Defined value)
Ex: Waq to update all employee commissions in a table based on the following conditions?
ii) IF employee comm is not null then update those employees comm as comm+500.
SET OPERATORS
SQL set operators allows combine results from two or more SELECT statements. At first
sight this looks similar to SQL joins although there is big difference. SQL joins tends to combine
columns i.e. with each additionally joined table it is possible to select more and more columns.
SQL set operators on the other hand combine rows from different queries with strong
preconditions - all involved SELECTS must. Joins we are collecting the data from two tables
when there is a common data. But in set operators the data is not joined, in this the data is
merged
• The data types of corresponding columns in each involved SELECT must be compatible (either
the same or with possibility implicitly convert to the data types of the first SELECT statement).
Operator Returns
MINUS All distinct rows selected by the first query but not the second
You can combine multiple queries using the set operators UNION, UNION ALL, INTERSECT,
and MINUS. All set operators have equal precedence. If a SQL statement contains multiple set
operators, then Oracle Database evaluates them from the left to right unless parentheses
explicitly specify another order.
The corresponding expressions in the select lists of the component queries of a compound query
must match in number and must be in the same datatype group
If component queries select character data, then the datatype of the return values are determined
as follows:
• If both queries select values of datatype CHAR of equal length, then the returned values
have datatype CHAR of that length. If the queries select values of CHAR with different lengths,
then the returned value is VARCHAR2 with the length of the larger CHARvalue.
• If either or both of the queries select values of datatype VARCHAR2, then the returned
values have datatype VARCHAR2.
In queries using set operators, Oracle does not perform implicit conversion across datatype
groups. Therefore, if the corresponding expressions of component queries resolve to both
character data and numeric data, Oracle returns an error.
• The expressions in the SELECT lists must match in number and data type.
These operators are used to combine the information of similar data type from One or
more than one table.
• The ORDER BY clause doesn't recognize the column names of the second SELECT
• The set operators are not valid on columns of type BLOB, CLOB, BFILE, VARRAY, ornested
table.
• The UNION, INTERSECT, and MINUS operators are not valid on LONG columns.
• Set operations are not allowed on SELECT statements containing TABLE collection
expressions.
• SELECT statements involved in set operations can't use the FOR UPDATE clause.
SQL statements containing these set operators are referred to as compound queries, and
each SELECT statement in a compound query is referred to as a component query. Two
SELECTs can be combined into a compound query by a set operation only if they satisfy the
following two conditions:
1. The result sets of both the queries must have the same number of columns.
2. The datatype of each column in the second result set must match the datatype of its
corresponding column in the first result set.
<component query>
<component query>
• UNION:-
UNION Operator combines the results of two select statements into one result set, and then
eliminates any duplicates rows from the final result set.
Example:-
• union
Explination:- The above statement combines the results of two queries with the UNION
operator, which eliminates duplicate selected rows. This statement shows that you must match
datatype (using the TO_CHAR function) when columns do not exist in one or the other table:
Where dname='SALES')
union
Union All:-
UNION ALL Operator combines the results of two select statements into one result set
including Duplicates.
Example:-
union all
order by 1;
Explination:- The UNION operator returns only distinct rows that appear in either result, while
the UNION ALL operator returns all rows. The UNION ALL operator does not eliminate
duplicate selected rows:
union all
INTERSECT:-
INTERSECT Operator returns only those rows that are common in both tables.
Sql> select empno,ename from emp where deptno=10
intersect
order by 1;
Explination:- The above statement combines the results with the INTERSECT operator, which
returns only those rows returned by both queries.
intersect
MINUS:-
MINUS Operator takes the result set of first select statement and removes those rows that
are returned by a second select statement.
Example:-
Where deptno=10
minus
Explination:- The above statement combines results with the MINUS operator, which returns
only unique rows returned by the first query but not by the second:
minus
Developers, and even DBAs, occasionally need to compare the contents of two tables to
determine whether the tables contain the same data. The need to do this is especially common in
test environments, as developers may want to compare a set of data generated by a program
under test with a set of "known good" data. Comparison of tables is also useful for automated
testing purposes, when we have to compare actual results with a given set of expected results.
SQL's set operations provide an interesting solution to this problem of comparing two tables.
The following query uses both MINUS and UNION ALL to compare two tables for equality.
The query depends on each table having either a primary key or at least one unique index.
Example:-
Sql>DESC CUSTOMER_KNOWN_GOOD
CUST_NBR NAME
----------- ------------------------------
1 Sony
1 Sony
2 Samsung
3 Panasonic
3 Panasonic
3 Panasonic
Sql>DESC CUSTOMER_TEST
CUST_NBR NAME
----------- ------------------------------
1 Sony
1 Sony
2 Samsung
2 Samsung
3 Panasonic
Example:-
MINUS
UNION ALL
MINUS
Explination:-Let's talk a bit about how this query works. We can look at it as the union of two
compound queries. The parentheses ensure that both MINUS operations take place first before
the UNION ALL operation is performed. The result of the first MINUS query will be those rows
in CUSTOMER_KNOWN_GOOD that are not also in CUSTOMER_TEST. The result of the
second MINUS query will be those rows in CUSTOMER_TEST that are not also in
CUSTOMER_KNOWN_GOOD. The UNION ALL operator simply combines these two result
sets for convenience. If no rows are returned by this query, then we know that both tables have
identical rows. Any rows returned by this query represent differences between the
CUSTOMER_TEST and CUSTOMER_KNOWN_GOOD tables.
FROM CUSTOMER_KNOWN_GOOD C1
MINUS
FROM CUSTOMER_TEST C2
UNION ALL
FROM CUSTOMER_TEST C3
MINUS
FROM CUSTOMER_KNOWN_GOOD C4
Explination:- These results indicate that one table (CUSTOMER_KNOWN_GOOD) has one
record for "Samsung", whereas the second table (CUSTOMER_TEST) has two records for the
same customer. Also, one table (CUSTOMER_KNOWN_GOOD) has three records for
"Panasonic", whereas the second table (CUSTOMER_TEST) has one record for the same
customer. Both the tables have the same number of rows (two) for "Sony", and therefore "Sony"
doesn't appear in the output.
TIP: Duplicate rows are not possible in tables that have a primary key or at least one unique
index. Use the short form of the table comparison query for such tables.
union
where deptno=20
union
where deptno=30;
We discussed union compatibility conditions at the beginning of this chapter. The union
compatibility issue gets interesting when NULLs are involved. As we know, NULL doesn't have
a datatype, and NULL can be used in place of a value of any datatype. If we purposely select
NULL as a column value in a component query, Oracle no longer has two datatypes to compare
in order to see whether the two component queries are compatible. For character columns, this is
no problem. For example:
UNION
OUTPUT:
---------- --------
1 DEFINITE
Notice that Oracle considers the character string 'DEFINITE' from the first component
query to be compatible with the NULL value supplied for the corresponding column in the
second component qery. However, if a NUMBER or a DATE column of a component query is
set to NULL, we must explicitly tell Oracle what "flavor" of NULL to use. Otherwise, we'll
encounter errors. For
example:
sql>SELECT 1 NUM, 'DEFINITE' STRING FROM DUAL
UNION
ERROR at line 1:
Note that the use of NULL in the second component query causes a datatype mismatch between
the first column of the first component query, and the first column of the second component
query. Using NULL for a DATE column causes the same problem, as in the following
Example:-
UNION
ERROR at line 1:
In these cases, we need to cast the NULL to a suitable datatype to fix the problem, as in
the following examples:-
UNION
NUM STRING
---------- --------
1 DEFINITE
UNKNOWN
UNION
SELECT 2 NUM, TO_DATE(NULL) DATES FROM DUAL;
NUM DATES
---------- ---------
1 06-JAN-02
This problem of union compatibility when using NULLs is encountered in Oracle8i. However,
there is no such problem in Oracle9i, as we can see in the following examples generated from an
Oracle9i database.
UNION
NUM STRING
---------- --------
1 DEFINITE
UNKNOWN
UNION
NUM DATES
---------- ---------
1 06-JAN-02
EX:-
EX:-
EX: Waq to display all employee details who are working in NARESHIT organization?
(OR)
EX: Waq to display employee details who are working in both branchs ?
EX: Waq to display employee details who are working in HYD BUT NOT IN CHENNAI
branchs ?
SELECT * FROM EMP_HYD MINUS SELECT * FROM EMP_CHENNAI;
EX: Waq to display employee details who are working in CHENNAI BUT NOT IN HYD
branchs ?