Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Lab 05

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 15

LAB NO.

6
Built-In Functions

To learn and practice built-in functions using DML commands


Objectives

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

Procedure for doing the experiment


Step Detail
1 Built-in Functions
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.
Operators differ from functional in that they follow the format of function
name (arg..). An argument is a user defined variables or constants. Most
operators accept at most 2 arguments while the structure of functions permit
to accept 3 or more arguments. Function can be classifies into single row
function and group functions.

2 Single Row functions


A single row function or scalar function returns only one value for every row
queries in table. Single row function can appear in a select command and can
also be included in a where clause. The single row function can be broadly
classified as, o Date Function o Numeric Functiono Character Function o
Conversion Functiono Miscellaneous FunctionThe example that follows
mostly uses the symbol table “dual”. It is a table, which is automatically
created by oracle along with the data dictionary.
3 Date Function
The date function operates on date values and produce outputs, which also
belong to date data type except for months, between, date function returns a
number.
4 Aggregate Functions
A group function returns a result based on group of rows
SQL COMMANDS

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

Command Query Output


initcap(char); select initcap(‘hello’) from dual; Hello
lower(char); select lower (‘HELLO’) from dual; hello
upper (char); select upper (‘hello’) from dual; HELLO
ltrim(char,[set]); select ltrim (‘cseit’, ‘cse’) from dual; it
rtrim(char,[set]); select rtrim (‘cseit’, ‘it’) from dual; cse
substr(char,m,n); select substr (‘information’, 3, 4) from dual; form
replace(char, select replace(‘jack and jue’,’j’,’bl’) from dual; black
search string,
and blue
replace string);

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%’.

SQL> select * from emp where ename like ‘A%’;

EMPNO ENAME JOB DEPTNO SAL


---------- -------------------- ------------- ---------- ---------------
2 Ali ASP 2 15000
5 Ayesha AP 1 10000

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%';

EMPNO ENAME JOB DEPTNO SAL


---------- -------------------- ------------- ---------- ---------------
1 Mehboob AP 1 10000
3 Gulfam ASP 1 15000
4 Kareem PROF 2 30000
Q3: Display the rows whose salary ranges from 15000 to 30000.

Answer:
SQL> select * from emp where sal between 15000 and 30000;

EMPNO ENAME JOB DEPTNO SAL


---------- -------------------- ------------- ---------- ---------------
2 Ali ASP 2 15000
3 Gulfam ASP 1 15000
4 Kareem PROF 2 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;

EMPNO ENAME JOB DEPTNO SAL


---------- -------------------- ------------- ---------- ---------------
1 Mehboob AP 1 10000
2 Ali ASP 2 15000
3 Gulfam ASP 1 15000
4 Kareem PROF 2 30000
5 Ayesha AP 1 10000

SQL> select count(*) 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.

SQL> select max(sal) as max_salary, min(sal) as min_salary from emp;


MAX_SALARY MIN_SALARY
-------------------- -------------------
30000 10000

Q7: Display month(s) between today and hire date.


Answer:
1. Use sysdate for today date
SQL>select month_between (sysdate, hiredate) from emp;

Q8: Display the last day of that month in 15-Oct-14.

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.

Write two types of functions.


Single row functions
Aggregate functions

What are single row functions?


A single row function or scalar function returns only one value for every row queries in table.
Single row function can appear in a select command and can also be included in a where clause.
The single row function can be broadly classified as:
Date Functions
Numeric Functions
Character Functions
Conversion Functions
Miscellaneous Functions

List some character functions.


initcap(char);
lower (char);
upper (char);
ltrim (char,[set]);
rtrim (char,[set]);
Extra Tasks
Q1. Write a SQL statement to find those salesmen with all information who come from the city
either Islamabad or Rawalpindi.

Paste code and screenshot here

Q2. Write a SQL statement to find the total purchase amount of all orders.

Paste code and screenshot here

Q.3 Write a SQL statement to find the average purchase amount of all orders.

Paste code and screenshot here

Q4. Write a SQL statement to find the number of salesmen currently listing for all of their
customers. 

Paste code and screenshot here


Q5. Write a SQL statement know how many customer have listed their names.

Paste code and screenshot here

Q.6 Write a SQL statement find the number of customers who gets at least a gradation for his/her
performance.

Paste code and screenshot here

Q7. Write a SQL statement to get the maximum purchase amount of all the orders.

Paste code and screenshot here

You might also like