Lab 05
Lab 05
Lab 05
6
Built-In Functions
Instructions
This is individual Lab work/task.
Complete this lab work within lab timing.
Discussion with peers is not allowed.
You can consult any book, notes & Internet.
Copy paste from Internet will give you negative marks.
Lab work is divided into small tasks, complete all tasks sequentially.
Show solution of each lab task to your Lab Instructor.
Paste your solution (i.e. code) in given space under each task.
Also make a zip/rar archive of all lab tasks and upload this file on LMS before leaving
lab.
In-Lab Exercises/Tasks
.
FACILITIES REQUIRED AND PROCEDURE
Facilities required to do the experiment
SNo Facilities Required Quantity
1 System 1
2 Operating System Windows 10
3 Front End Java
4 Back End Oracle 11g, MySQL
Date Function
add_month
This function returns a date after adding a specified date with specified number of months.
Syntax: Add_months(d,n); where d-date n-number of months
Example: Select add_months(sysdate,2) from dual;
last_day
It displays the last date of that month.
Syntax: last_day (d); where d-date
Example: Select last_day („1-jun-2009‟) from dual;
months_between
It gives the difference in number of months between d1 & d2.
Syntax: month_between (d1,d2); where d1 & d2 –dates
Example: Select month_between („1-jun-2009‟,‟1-aug-2009‟) from dual;
next_day
It returns a day followed the specified date.
Syntax: next_day (d,day);
Example: Select next_day (sysdate,‟wednesday‟) from dual
round
This function returns the date, which is rounded to the unit specified by the format model. Syntax
: round (d,[fmt]);
where d- date, [fmt] – optional. By default date will be rounded to the nearest day
Example: Select round (to_date(„1-jun-2009‟,‟dd-mm-yy‟),‟year‟) from dual;
Select round („1-jun-2009‟,‟year‟) from dual;
Numeric Functions
Command Query Output
abs(n) select abs(-15) from dual; 15
ceil(n) select ceil(55.67) from dual; 56
exp(n) select exp(4) from dual; 54.59
floor(n) select floor(100.2) from dual; 100
power(m,n) select power(4,2) from dual; 16
mod(m,n) select mod(10,3) from dual; 1
round(m,n) select round(100.256,2) from dual; 100.26
trunc(m,n) select trunc(100.256,2) from dual; 100.23
sqrt(m,n) select sqrt(16) from dual; 4
Character Functions
Conversion Functions
to_char()
Syntax: to_char(d,[format]);This function converts date to a value of varchar type in a form
specified by date format. If format is negelected then it converts date to varchar2 in the default
date format. Example: select to_char (sysdate, ‟dd-mm-yy‟) from dual;
to_date()
Syntax: to_date(d,[format]);
This function converts character to date data format specified in the form character.
Example: select to_date(„aug 15 2009‟,‟mm-dd-yy‟) from dual;
Miscellaneous Functions
uid – This function returns the integer value (id) corresponding to the user currently logged in.
Example: select uid from dual;
user – This function returns the logins user name.
Example: select user from dual;
nvl - The null value function is mainly used in the case where we want to consider null values as
zero.
Syntax; nvl(exp1, exp2)If exp1 is null, return exp2. If exp1 is not null, return exp1.
Example: select custid, shipdate, nvl(total,0) from order;
vsize: It returns the number of bytes in expression.
Example: select vsize(„tech‟) from dual;
Group Functions
A group function returns a result based on group of rows.
count(distinct col_name) – It avoids the repeated and null values.
Example: select count(distinct ordid) from order;
GROUP BY Clause
This allows us to use simultaneous column name and group functions.
Example: Select max(percentage), deptname from student group by deptname;
HAVING Clause
This is used to specify conditions on rows retrieved by using group by clause.
Example: Select max(percentage), deptname from student group by deptname having
count(*)>=50;
Special Operators
In / not in – used to select a equi from a specific set of values
Any - used to compare with a specific set of values
Between / not between – used to find between the ranges
Like / not like – used to do the pattern matching
Tasks
Q1: Display all the details of the records whose employee name starts with
‘A’.
Answer:
1. Use SELECT FROM WHERE syntax.
2. select should include all in the given format.
3. from should include employee
4. where should include condition on empname like ‘A%’.
Q2: Display all the details of the records whose employee name does not starts
with ‘A’.
Answer:
SQL> select * from emp where ename not like 'A%';
Answer:
SQL> select * from emp where sal between 15000 and 30000;
Q4: Calculate the total and average salary amount of the emp table.
Answer:
SQL> select sum(sal), avg(sal) from emp;
SUM(SAL) AVG(SAL)
-------------- ---------------
80000 16000
Q5: Count the total records in the emp table.
Answer:
SQL>select * from emp;
COUNT(*)
--------------
5
Q6: Determine the max and min salary and rename the column as max_salary
and min_salary.
Answer:
1. Use the MIN & MAX aggregate function in select clause.
2. Rename the column as min_sal and max_sal.
Answer:
SQL> Select last_day ('15-oct-2014') from dual;
LAST_DAY
--------------
31-OCT-14
Q9: Find how many job titles are available in employee table.
Answer:
1. Use select from clause.
2. Use count function to get the result.
SQL> select count(job) from emp;
COUNT(JOB)
------------------
4
SQL> select count(distinct job) from emp;
COUNT(DISTINCT JOB)
--------------------------------
2
Q10: Display annual salary of employees along with ename, sal, and comm.
Answer:
1. Use select from clause.
2. Use nvl function to assign 0 to null in comm.
SQL> select ename, sal, comm, (sal*12) +nvl(comm, 0) as annualsal from emp;
OUTCOME
Thus the queries using the different intrinsic functions were executed successfully.
QUESTIONS AND ANSWERS
Define function?
Function is a group of code that accepts zero or more arguments and both return one or more
results. Both are used to manipulate individual data items.
Q2. Write a SQL statement to find the total purchase amount of all orders.
Q.3 Write a SQL statement to find the average purchase amount of all orders.
Q4. Write a SQL statement to find the number of salesmen currently listing for all of their
customers.
Q.6 Write a SQL statement find the number of customers who gets at least a gradation for his/her
performance.
Q7. Write a SQL statement to get the maximum purchase amount of all the orders.