You are on page 1of 16

SQL functions

• Single Row Function


• Multi Row Function (Group Function)
Single Row Function
• Character Functions
• >>>Case Conversion Functions
• LOWER,UPPER,INITCAP
• >>>Character Manipulation Functions
• CONCAT, SUBSTR,LENGTH,INSTR, LPAD, RPAD, TRIM, REPLACE
• Numeric Function
• ROUND,TRUNC,MOD
• Date Function
• MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY, ROUND, TRUNC
• General Function
• NVL, NVL2, NULLIF, COALESCE
Case Conversion Functions
• LOWER,UPPER,INITCAP
• e.g:
• select lower(first_name)
from employee

• select upper(first_name)
from employee

• select initcap(first_name)
from employee
Character Manipulation Functions
• CONCAT, SUBSTR,LENGTH,INSTR, LPAD, RPAD, TRIM, REPLACE
• e.g:
• select concat(first_name,last_name)
from employee

• select first_name||' '||last_name


from employee

• select first_name||' '||last_name||' gets salary: '||salary


from employee

• select substr(first_name,1,4)
from employee
• select length(first_name)
from employee
• select instr(first_name,'d'),first_name
from employee
• select lpad(first_name,15,'*')
from employee
• select rpad(first_name,15,'*')
from employee
• select trim('A' from first_name)
from employee
• select replace(first_name,'R','M')
from employee
Numeric Function
• ROUND,TRUNC,MOD
• select round(1874.678,2)
from dual
• select trunc(1874.678,2)
from dual
• select mod(1600,300)
from dual
Date Function
• MONTHS_BETWEEN, ADD_MONTHS, NEXT_DAY, LAST_DAY, ROUND, TRUNC
• select join_date,sysdate
from employee
• select join_date,sysdate,(sysdate-join_date)/365
from employee
• select months_between(sysdate,'01-01-2020')
from employee
• select add_months(sysdate,2)
from dual
• select next_day(sysdate,'FRIDAY')
from dual
• select last_day(sysdate)
from dual
General Function
• NVL, NVL2, NULLIF, COALESCE
select first_name,nvl(salary,0)
from employee

• select first_name,nvl2(salary,'yes','no')
from employee

• select first_name,length(first_name),last_name,length(last_name),
nullif(length(first_name),length(last_name)) restlt
from employee

• select first_name,COALESCE(salary,salary,0)
from employee
Multirow Function (Group Function)
• AVG, COUNT, MAX, MIN, SUM
• select avg(salary)
from employee
• select count(first_name)
from employee
• select max(salary)
from employee
• select min(salary)
from employee
• select sum(salary)
from employee
Sequence
• Sequences are database objects from which multiple users can
generate unique integers. The sequence generator generates
sequential numbers, which can help to generate unique primary keys
automatically
• CREATE SEQUENCE new_seq01
MINVALUE 1
MAXVALUE 999999999
START WITH 1
INCREMENT BY 1

select new_seq01.NEXTVAL
from dual

insert into employee


values (new_seq01.NEXTVAL,'Abdul','karim','01-01-2015',35000,1001)

commit

select * from employee


order by employee_id desc

select new_seq01.CURRVAL
from dual
View
• A database view is a searchable object in a database that is defined by
a query. Though a view doesn’t store data, some refer to a views as
“virtual tables,” you can query a view like you can a table. A view can
combine data from two or more table, using joins, and also just
contain a subset of information.
• create view vemp_name
as select first_name||' '||last_name as "Name",salary
from employee

• select * from vemp_name

• create view vemp_detail


as
select first_name,salary,department_id,(select department_name from
department where department_id=e.department_id) as "DeptName"
from employee e

• select * from vemp_detail


Subquery
• A subquery is a SQL query nested inside a larger query.
• A subquery may occur in :
• - A SELECT clause
• - A FROM clause
• - A WHERE clause
• select first_name,salary,department_id,(select department_name from
department where department_id=e.department_id) as "DeptName"
from employee e

• select first_name,salary,d.department_name
from employee e, (select department_id,department_name from
department) d
where e.department_id=d.department_id

• select first_name,salary
from employee
where salary=(select max(salary) from employee)

You might also like