Operators (Oracle)
Operators (Oracle)
Operators (Oracle)
Arithmetic operators
Assignment operator
Relational operators
Logical operators
Special Operators
Set Operators
Arithmetic operators:-
• Sql * plus ignores the blank spaces befor 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:-
Assignment operators:-
This operator is used for equality test. Used to test the equality of two
operands.
Example:-
---------------------------------------------------------------------------------------------------------
Relational Operator
Lessthan:-
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:-
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;
Explination:-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.
IN operator:-
--------------------------------------------------------------------------------------------------------
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
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
LIKE operator:-
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
-----------------------------------------------------------------------------------------------------
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
Sql> select ename ,hiredate from emp where hiredate like '%JAN%';
SQL> select empno,ename,job ,hiredate from emp where hiredate like '%-FEB-
81';
Sql> select *from dept where dname like '__/_%' 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.?
-----------------------------------------------------------------------------------------------------
IS NULL operator:-
Syntax:-
Example:-
Output:-
-- No rows selected;
AS there IS salary available FOR every employee.
Syntax:-
-----------------------------------------------------------------------------------------------------
Sql> select empno,ename,job,sal ,deptno from emp where mgr is not null;
Set Operators
Introduction
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
----------------------------------------------------------------------------
TableA
<set operator>
TableB
<set operator>
Tablec
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.
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.
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.
Set Operator Guidelines:-
• 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.
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
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 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 Operator returns only those rows that are common in both
tables.
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
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:-
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
(SELECT C3.*, COUNT(*)
FROM CUSTOMER_TEST C3
MINUS
FROM CUSTOMER_KNOWN_GOOD C4
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.
Example:-
union
where deptno=20
union
where deptno=30;
UNION
NUM STRING
---------- --------
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:
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
SELECT TO_NUMBER(NULL) NUM, 'UNKNOWN' STRING FROM DUAL;
NUM STRING
---------- --------
1 DEFINITE
UNKNOWN
UNION
NUM DATES
---------- ---------
1 06-JAN-02
UNION
NUM STRING
---------- --------
1 DEFINITE
UNKNOWN
UNION
NUM DATES
---------- ---------
1 06-JAN-02
EX:-
union