Professional Documents
Culture Documents
An operator manipulates individual data items and returns a result. The data items are called
operands.
Ex: *, +, AND
1. Unary Operator
2. Binary Operator
3. Character Operator
4. Other Operators
4.1 Arithmetic Operators
4.2 Comparison Operators
4.3 Logical Operators
4.4 Set Operators
1. Unary Operator
A unary operator uses only one operand.
Syntax:
Operator Operand
Ex:
1. Select +9 from dual;
o/p: 9
2. Binary Operators
A Binary operator uses two operands. A binary operator appears with its operands in the following
format.
Ex:
Select 7+5, 9/3 from dual;
o/p: 12 3
3. Character Operator
Character operators are used in expression to manipulate character strings. (||)
Syntax:
character string || character string
Ex:
Select 'Student Name is ' || sname from dual;
Ex:
1. select 5+2, 5-2, 5*2, 5/3 from dual;
=, !=, <>, <, >, >=, <=, IN, ANY | SOME, NOT IN, ALL, BETWEEN X
AND Y, EXISTS, IS NULL
1. Equality ( = )
To select a student name from student table where Rno= 'ABC123XYZ'
Ex:
Select sname "Student Name"
from student
where rno = 'ABC123XYZ';
To select all the rows from emp table where salary = 1500
select * from emp
where sal = 1500;
2. IN
Equaling to any members of test.
Eg:
1. Select * from student
where sname in ('ANIL','ROBERT');
3. ANY | SOME
Compares a value to each value in a list written by a query. Must be preceded by =, !=, <, >, <=,
>=
Eg:
Select * from emp
where deptno= some(10,40);
4. NOT IN
Equaling to !=ANY
Eg:
Select * from emp
where deptno = NOT IN (10,40);
5. ALL
Compares a value with every value in a list or returned by query. Must be preceded by =, !=, <, >,
<=, >=.
Eg:
select * from emp
where sal >= ALL ( 1400, 3000);
6. BETWEEN X AND Y
Grete than or equal to x and less than equal to y.
Eg:
select * from emp
where sal between 1400 and 3000;
7. EXISTS
True if sub-query returns at least one row.
Eg:
Select ename, job from emp
where ename in ( select ename from emp where sal = 3000 );
8. IS NULL
Test for Nulls. This is only operator that should be used to test for nulls.
Eg:
select * from emp
where comm is null;
1. AND
For rows to be selected all the specified conditions must be true.
AND
Condition 1 Condition 2 Result
T T T
T F F
F T F
F F F
Eg:
update the student table rows with the result as pass where M>40, P>40 and C>40 marks.
update student
set result = 'PASS'
where M>40 AND P>40 AND C>40;
2. OR
For rows to be selected any one of the specified conditions must be TRUE.
OR
Eg:
update the student table result as 'FAIL' where M<41, P<41 and C<41 marks
update student
set result = 'FAIL'
where M<41 OR P<41 OR C<41;
3. NOT
NOT
Condition Result
T F
F T
1. Union
Returns all distinct values selected by either query.
Ex:
Select deptno from dept
union
select deptno from emp;
2. Union All
Returns are values selected by either query including all duplicates.
Ex:
Select deptno from dept
union all
select deptno from emp;
3. Intersect
Returns all distinct values selected by both queries.
Ex:
Select deptno from dept
intersect
select deptno from emp;
4. Minus
Returns all distinct rows selected by the first query but not the second query.
A - B ≠ B - A
Ex:
select deptno from dept
minux
select deptno from emp;
Null values represent missing unknown data by default a table column can hold null values.
We have to use IS NULL and IS NOT NULL operator for retrieving NULL values.
Aggregate Functions
These operate on set of rows. The following are the aggregate functions.
1. MIN (X)
2. MAX(X)
3. AVG(X)
4. SUM(X)
5. COUNT(X)
6. COUNT(*)
1. MIN(X)
Returns the smallest value in column(x)
Ex:
Select min(sal) from emp;
2. MAX(X)
Returns the largest value in column(x)
Ex:
Select max(sal) from emp;
3. AVG(X)
Returns the average in column(x)
Ex:
Select avg(sal) from emp;
4. SUM(X)
5. COUNT(X)
6. COUNT(*)
Returns the number of values in table including null.
Ex:
Select count(*) from emp;
Deptwise
select deptno, min(sal) from emp
group by deptno;
Integrity Constraints
Integrity constraints are used to business rules for the database tables. These are 3 types of
constraints.
1. Not Null
Null values are not accepted
2. Default
Assigns the default value no value is inserted
3. Check
Check for specified condition
4. Unique Key
No duplicate values are allowed. Null values are accepted
5. Primary Key
No duplicate. No Null values are accepted.
6. Foreign Key
The constraints identified referencing the primary key in another table. It validates the data from
master table to detailed table.
Imposing Constraints
1. Column Level
2. Table Level
1. Not Null
Syntax
Column_name datatype(size) constraint <constraint_name) constraint_type
Ex:
Create table Student ( rno number(2),
sname varchar2(10) constraint sname_nn not null,
m number(3), p number(3), c number(3) );
2. Default
3. Check
5. Primary Key
1. Check
2. Unique
create table student10 ( rno number(2),
sname varchar2(10),
m number(3),
p number(3),
c number(3),
constraint un_rno unique(rno) );
3. Primary Key