Operators and Functions
Operators and Functions
Arithmetic operators:
Operator ‘+’:
Command: Select salary, salary+500 from employees;
Result: Salary of employees are incremented by 500 and displayed.
Operator ‘-’:
Command: Select salary, salary-300 from employees;
Result: Salary of employees are decremented by 300 and displayed.
Operator ‘*’:
Command: Select salary, salary*600 from employees;
Result: Salary of employees are multiplied by 600 and displayed.
Operator ‘/’:
Command: Select salary, salary/100 from employees;
Result: Salary of employees are divided by 100 and displayed.
Concatenation:
Command: Select emp_name || emp_id as "empns" from employees;
Result: emp_name and emp_id are concatenated and displayed.
Comparison operator:
Operator ‘=’:
Command: Select salary from employees where salary =6000000;
Result: Salary of employee having 6000000 is displayed.
Operator !=:
Command: Select salary from employees where salary !=6000000;
Result: Salaries of employees not having 6000000 are displayed.
Operator ‘<’:
Command: Select salary from employees where salary < 8000000;
Result: Salaries of employees having less than 8000000 are displayed.
Operator ‘>’:
Command: Select emp_id from employees where emp_id > 1;
Result: Employee ids of employees having greater than 1 are displayed.
Operator ‘>=’:
Command: Select emp_id from employees where emp_id >= 3;
Result: Employee ids of employees having greater than or equal to 3 are displayed.
Operator ‘<=’:
Command: Select salary from employees where salary <= 700000;
Result: Salaries of employees having less than or equal to 700000 are displayed.
BETWEEN:
Command: Select * from employees where salary between 400000 and 700000;
Result: Data of employees having salaries between 400000 and 700000 are displayed.
NOT BETWEEN:
Command: Select * from employees where salary not between 400000 and 700000;
Result: Data of employees having salaries not between 400000 and 700000 are displayed.
IN:
Command: Select country from employees where country in('India', 'UK');
Result: Employee ids and countries of those employees belonging to India ad UK are
displayed.
NOT IN:
Command: Select country from employees where country not in('India', 'UK');
Result: Countries of those employees not belonging to India ad UK are displayed.
LIKE:
Command: Select emp_name from employees where emp_name like ‘S%’;
Result: Employees whose names begin with S are displayed.
NOT LIKE:
Command: Select emp_name from employees where emp_name not like ‘S%’;
Result: Employees whose names do not begin with S are displayed.
IS NULL:
Command: Select phone_num from employees where phone_num is null;
Result: None of the values in column phone_num is a null value. Hence no rows are
displayed.
NOT NULL:
Command: Select phone_num from employees where phone_num is not null;
Result: None of the values in column phone_num is a null value. Hence all rows are
displayed.
Logical operators:
AND:
Command: Select emp_id, emp_name, salary from employees where salary>=10000 and
emp_id > 3;
Result: Data of employees having salary greater than or equal to 10000 and employee id
greater than 3 are displayed.
OR:
Command: Select emp_id, emp_name, salary from employees where salary>=10000 or
emp_id > 3;
Result: Data of employees having salary greater than or equal to 10000 or employee id greater
than 3 are displayed.
NOT:
Command: Select emp_name from employees where emp_name not in('Sam', 'Stacy');
Result: Employee names that are not Sam and Stacy are displayed.
Sorting:
Ascending order:
Command: SELECT * FROM employees ORDER BY country;
Result: Data from table are arranged in ascending order of country name by default and
displayed.
Descending order:
Command: SELECT * FROM employees ORDER BY country DESC;
Result: Data from table are arranged in descending order of country name and displayed.
Concatenation:
Command: SELECT CONCAT(‘Global’,’Warming’) FROM DUAL;
Result: Global Warming is concatenated to a single word.
Replacing:
Command: SELECT replace(‘DATA SYSTEMS’, ‘DATA’, ‘DATABASE’) FROM DUAL;
Result: The word DATA in DATA SYSTEMS is replaced with DATABASE.
Substrings:
Command: SELECT substr(‘DATA SCIENCE’,6,3) FROM DUAL;
Result: A substring SCI is displayed from DATA science.
Left pad:
Command: SELECT lpad(‘200’,6,’$’) FROM DUAL;
Result: $ symbol is padded on the right side of 200.
Right pad:
Command: SELECT rpad(‘200’,6,’$’) FROM DUAL;
Result: $ symbol is padded on the left side of 200.
Length of the string:
Command: SELECT length(‘Akshaya Suresh’) FROM DUAL;
Result: Length of the string is displayed.
Numeric functions:
Command: SELECT round(533.55) FROM DUAL;
Result: The number 533.55 is rounded to 534 and displayed.
SysDate functions:
Command: SELECT sysdate FROM DUAL;
Result: Present date is displayed.
Group functions:
Number of rows:
Command: SELECT COUNT(emp_name) FROM employees;
Result: Returns number of rows.
Grouping by clause:
Creating a table with department code and employee ids belonging to a
department and inserting values.
Command:
create table dept(dept_code number(3), empid number(3));
insert into dept values(300, 3);
insert into dept values(300, 5);
insert into dept values(300, 7);
insert into dept values(300, 21);
insert into dept values(300, 55);
insert into dept values(100, 2);
insert into dept values(100, 4);
insert into dept values(100, 6);
insert into dept values(200, 11);
insert into dept values(200, 12);
Result: The table is created with values inserted.
Grouping based on number of employees in a department:
Command:
SELECT dept_code "Department Code",
2 COUNT(*) "No of Employees"
3 FROM dept
4 GROUP BY dept_code;
Result: The table is grouped based on number of employees in a particular department.