Class-10(Operators)
-------------------
Operators
-------------
> It is used to perform an operation
> A operator is of two types
(1) Unary Operator
(2) Binary Operator
(1) Unary Operator
------------------
> The operator which requires only one operand is known as Unary Operator
Ex: +34,-45,+23
Here +,- are the operators and 34,45,23 are the operands
(2) Binary Operator
-------------------
> The operator which needs more than one operand is known as Binary operators
> It requires minium two operands
Ex: 3 + 5, 5 - 3, 2 * 5
3 + 5 : Here 3 and 5 are the operands and '+' is a operator
There are various operators exists
(1) Arithmatic Operators
(2) Character Operators
(3) Comparison Operators
(4) Logical Operators
(5) Set Operators
(6) Other Operators
(1) Arithmatic Operators
-------------------------
+ -- Addition
- -- Substraction
* -- Multiplication
/ -- Division
Q-1: WAQ to Add,Substract,Multiply and divide the salary column with 10 along with
display empno
A-1: SELECT EMPNO,ENAME,SAL,SAL+10,SAL-10,SAL*10,SAL/10 FROM EMP;
(2) Character Operators
------------------------
|| -- Concatenation Operator
> It is used to combine two value
Syntax
------
<Value 1>||<Value 2>
Q-1: WAQ to display empno and concatenate ename and job and display it
A-1: SELECT EMPNO,ENAME||'->'||JOB FROM EMP;
(3) Comparison Operators
--------------------------
=
!=
<>
>
<
>=
<=
IN
NOT IN
BETWEEN
NOT BETWEEN
ANY
ALL
IS NOT NULL
IS NULL
LIKE
NOT LIKE
EXISTS
=
--
SELECT * FROM EMP WHERE SAL=3000;
!=
--
SELECT * FROM EMP WHERE SAL != 3000;
<>
---
SELECT * FROM EMP WHERE SAL <> 3000;
>
--
SELECT * FROM EMP WHERE SAL > 2000;
<
--
SELECT * FROM EMP WHERE SAL < 1000;
>=
--
SELECT * FROM EMP WHERE SAL >= 3000;
<=
---
SELECT * FROM EMP WHERE SAL <= 1000;
IN
---
> It takes list of values
> It can take any types values (Character,Number,etc)
> It searches that value from the table and display which is matched with values
mentioned in the List
Syntax
------
<Column Name> IN(<Val 1>,<Val 2>,...,<Val N>)
Q-1: Display the employee details who are working under the department no 10 or 20
A-1: SELECT * FROM EMP WHERE DEPTNO IN (10,20);
NOT IN
------
> It filter out the values given in the NOT IN clause
Q-1: Display all the employee details who is not working under the department 10 or
20
A-1: SELECT * FROM EMP WHERE DEPTNO NOT IN (10,20);
Q-2: Display all the details of the emp table whose job is not a MANAGER
A-2: SELECT * FROM EMP WHERE JOB NOT IN ('MANAGER','CLERK');
BETWEEN
-------
> It takes range value
> It must have start range and end range
Syntax
------
<Value> BETWEEN <Start Range> AND <End Range>
Q-1: Display those employees who is getting salary in between 1000 and 2500
A-1: SELECT * FROM EMP WHERE SAL BETWEEN 1000 AND 2500;
NOT BETWEEN
------------
> It restricts value given in the range
> It must have start range and end range
Syntax
------
<Value> NOT BETWEEN <Start Range> AND <End Range>
Q-1: Display those employees who is not getting salary in between 1000 and 2500
A-1: SELECT * FROM EMP WHERE SAL NOT BETWEEN 1000 AND 2500;
ANY
---
> Compares a value to each value in a list returned by a query
> Here we can use relational operator such are >,<,>=,<=,etc
> It is similar with IN operator
Syntax
------
<Column Name> <Operator> ANY(<Value List)>
Q-1: Display all the emp datail working under deptno 10 or 20
A-1: SELECT * FROM EMP WHERE DEPTNO = ANY(10,20);
ALL
---
> Here we can use relational operator such are >,<,>=,<=,etc
> It is similar with IN operator
> Here you can give only single value
Syntax
------
<Column Name> <Operator> ALL(<Value)>
Q-1: Display employees working under deptno 10
A-1: SELECT * FROM EMP WHERE DEPTNO = ALL(10);
IS NULL
-------
> It searches for the values which are NULL
> It is applied on a column
Syntax
------
<Column Name> IS NULL
Q-1: Display all the details who is not getting any commission from the emp table
A-1: SELECT * FROM EMP WHERE COMM IS NULL;
IS NOT NULL
-----------
> It searches for the values which are NOT NULL
> It is applied on a column
Syntax
------
<Column Name> IS NOT NULL
Q-1: Display all the details who is getting some commission from the emp table
A-1: SELECT * FROM EMP WHERE COMM IS NOT NULL;
LIKE
----
> It is used for pattern matching
> If you want to search some text from a string , we need to use this operator
> It have two wild card óperators that is '_' , '%'
_ -> It searches one character at a time(Specific Position)
% -> For entire string matching
Syntax
-------
<Column Name> LIKE < String Pattern>
Q-1: WAQ to check whose name starts with character 'A'
A-1: SELECT * FROM EMP WHERE ENAME LIKE 'A%';
Q-2: WAQ to print whose name having only 4 characters
A-2: SELECT * FROM EMP WHERE ENAME LIKE '____';
Q-3: WAQ to display those names which have character 'I'
A-3: SELECT * FROM EMP WHERE ENAME LIKE '%I%';
Q-4: WAQ to display those name whose first character may be any character but
second character must be 'D'
A-4: SELECT * FROM EMP WHERE ENAME LIKE '_D%';
NOT LIKE
--------
> It is the reverse of LIKE operator
Syntax
-------
<Column Name> NOT LIKE < String Pattern>
Q-1: WAQ to check whose name not starting with character 'A'
A-1: SELECT * FROM EMP WHERE ENAME NOT LIKE 'A%';
Q-1: WAQ to display whose name is not having 4 character
A-1: SELECT * FROM EMP WHERE ENAME NOT LIKE '____';
EXISTS
------
> We will discuss it later chapter
(4) Logical Operators
----------------------
AND --If all the condition is satisfied then AND operator becomes true
OR --If any of the condition is satisfied then OR operator becomes true
NOT --Negation of a Condition
Q-1: WAQ to check whose name is starts with character A along with getting salary
more than 1000
A-1: SELECT * FROM EMP WHERE ENAME LIKE 'A%' AND SAL>1000;
Q-2: WAQ to check whose salary is greater than 1000 or getting some commission
A-2: SELECT * FROM EMP WHERE SAL>1000 OR COMM IS NOT NULL;
(5) SET Operator
-----------------
UNION
UNION ALL
INTERSECT
MINUS
UNION
-----
> It combines result of two outputs
> The no of columns and there data types should be same for the both the SELECT
statement
> It removes duplicate value
> It is slower as compared to UNION ALL
> We can include any number of Select statement
Syntax
-------
<Select Statement 1>
UNION
<Select Statement 2>
UNION
....
UNION
<Select Statement N>
Ex-1
----
SELECT * FROM EMP
UNION
SELECT EMPNO,ENAME,SAL FROM EMP;--ORA-01789: query block has incorrect number of
result columns
Ex-2
----
SELECT EMPNO,ENAME,SAL FROM EMP
UNION
SELECT EMPNO,SAL,ENAME FROM EMP;--ORA-01790: expression must have same datatype as
corresponding expression
Ex-3
----
SELECT * FROM EMP WHERE ENAME LIKE 'A%'
UNION
SELECT * FROM EMP WHERE ENAME NOT LIKE 'A%';
Ex-4
----
SELECT * FROM EMP WHERE ENAME LIKE 'A%'
UNION
SELECT * FROM EMP WHERE ENAME LIKE 'A%';
UNION ALL
----------
> It combines result of two outputs
> The no of columns and there data types should be same for the both the SELECT
statement
> It doesn't removes duplicate value
> It is faster as compared to UNION
> We can include any number of Select statement
Syntax
-------
<Select Statement 1>
UNION ALL
<Select Statement 2>
Ex-1
----
SELECT * FROM EMP WHERE ENAME LIKE 'A%'
UNION ALL
SELECT * FROM EMP WHERE ENAME LIKE 'A%';
INTERSECT
---------
> It retrieves the common result in both the select statements
> The no of columns and there data types should be same for the both the SELECT
statement
Ex-1
----
SELECT * FROM EMP WHERE ENAME LIKE 'A%'
INTERSECT
SELECT * FROM EMP WHERE ENAME LIKE 'A%' AND SAL >1600;
MINUS
-----
> It removes record from the first select statement if it exists in the second
select statement
> The no of columns and there data types should be same for the both the SELECT
statement
Ex-1:
-----
SELECT * FROM EMP WHERE ENAME LIKE 'A%'
MINUS
SELECT * FROM EMP WHERE ENAME LIKE 'A%' AND SAL >1500;
Other Operator
------------------
(+) -- It is used during join