You are on page 1of 12

SQL OPERATORS

An operator manipulates individual data items and returns a result. The data items are called
operands.

Operators are represented by special character or keywords.

Ex: *, +, AND

The following are the operators in SQL

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. Select -7 from dual;


o/p: -7

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;

o/p: Student Name is Rakesh


Student Name is Sourab
Student Name is Anil
Student Name is Akbar

4.1 Arithmetic Operators


Mathematical operations are performed using mathematic operators such as +, -, *, %

Ex:
1. select 5+2, 5-2, 5*2, 5/3 from dual;

2. Select m+p+c, (m+p+c)/3 from student;

4.2 Comparison Operators


Comparison operators are used to compare the column data with specific values in a condition.
Comparison operators are also used along with select statement to filter the data based on speicific
conditions.

The following are the comparison operators.

=, !=, <>, <, >, >=, <=, 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');

2. Select * from emp


where deptno in (10,40);

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;

4.3 LOGICAL OPERATORS


These are 3 logical operators namely AND, OR, NOT.

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

Condition 1 Condition 2 Result


T T T
T F T
F T T
F F F

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

For a row to be selected the specified condition must be false.

NOT
Condition Result
T F
F T

Select * from emp


where NOT job='MANAGER';
4. SET OPERATORS
Set Operators are called vertical joins (column). The following are the set operators.
1. Union
2. Union All
3. Intersect
4. Minus

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;

SQL NULL VALUES


SQL NULL value is the term used to represent the missing value. A null value in a table is a value in a
field that appears to be blank. A field with null value is a field with no value. It is very important to
understand that a null value is different than a zero vale or a field that contains spaces.

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(*)

SELECT col1, col2,... , coln


FROM <Table Name>
WHERE <Condition>
GROUP BY col
HAVING <condition>;

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)

Returns the total of values in column(x)


Ex:
Select sum(sal) from emp;

5. COUNT(X)

Returns the number of values in column(x) ignore the null values


Ex:
Select count(sal) from emp;

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;

select deptno, max(sal), sum(sal), count(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. Domain Level (column) -- Ex: Not Null, Default, Check


2. Entity Level (table) -- Ex: Unique Key, Primary Key
3. Referential Integrity (Multiple Tables) -- Ex: Foreign Key

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

Imposing Constraints at Column Level


( Not Null, Default, Check, Unique, Primary Key, Foreign Key )

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

Create table student1 ( rno number(2),


sname varchar2(10),
n number(3) default 0,
p number(3) default 0,
c number(3) default 0 );

3. Check

Create table student2 ( rno number(2),


sname varchar2(10),
m number(3),
p number(3),
c number(3),
result char(4) check ( result in ('PASS', 'FAIL') ) );
4. Unique

Create table student3 ( rno number(2) constraint rno_u unique,


sname varchar2(10),
m number(3),
p number(3),
c number(3) );

5. Primary Key

Create table student4 (


rno number(2) constraint rno_pk primary key,
sname varchar2(10),
m number(3),
p number(3),
c number(3) );
6. Foreign Key

Dept (Master Table)

create table dept1 (


deptno number(2) constraint deptno_pk primary key,
dname varchar2(10),
loc varchar2(10) );

EMP (Detail Table)


create table emp1 (
empno number(4) constraint empno_pk 1 primary key,
ename varchar2(10),
job varchar2(10),
mgr number(4),
hiredate date,
sal number(9,2),
comm number(7,2),
deptno number(2) constraint deptno_fk1 references dept(deptno) );

Imposing Constraints at Table Level


( Check, Unique, Primary Key, Foreign Key )

1. Check

create table student9 ( rno number(2),


sname varchar2(10),
m number(3),
p number(3),
c number(3),
result char(4) constraint res_ck check ( result in ( 'PASS', 'FAIL') ) );

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

create table student11 ( rno number(2),


sname varchar2(10),
m number(3),
p number(3),
c number(3),
constraint pk_rno primary key(rno) );
4. Table Level Foreign Key

Dept (Master Table)

create table dept2 (


deptno number(2),
dname varchar2(10),
loc varchar2(10),
constraint pk12_deptno primary key(deptno) );

Emp (Detail Table)

create table emp2 (


empno number(4),
ename varchar2(10),
job varchar2(10),
mgr number(4),
hiredate date,
sal number(9,2),
comm number(7,2),
deptno number(2),
constraint fk2_deptno foreign key(deptno) references dept2(deptno),
constraint pk2_empno primary key(empno) );

You might also like