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

Lab Practice 3, Functions

1. Write a query to display the current date. Label the column Date.
Query:
SQL> select sysdate AS Date from dual;
Date
--------28-AUG-13
2. Display the employee number, name, salary and salary increase by 15% expressed as a whole
number. Label the column New Salary.
Query:
SQL> select empno,ename,sal,round(((15/100)*sal)+sal) as New_Salary from emp;
EMPNO ENAME
SAL NEWSALARY
---------- ---------- ---------- ---------7369 SMITH
800
920
7499 ALLEN
1600
1840
7521 WARD
1250
1438
7566 JONES
2975
3421
7654 MARTIN
1250
1438
7698 BLAKE
2850
3278
7782 CLARK
2450
2818
7788 SCOTT
3000
3450
7839 KING
5000
5750
7844 TURNER
1500
1725
7876 ADAMS
1100
1265
EMPNO ENAME
SAL SALARY
---------- ---------- ---------- ---------7900 JAMES
950
1093
7902 FORD
3000
3450
7934 MILLER
1300
1495
14 rows selected.

3. Modify above to add a column that will subtract old salary from new one. Label the column
Increment.
Query:
SQL> select empno,ename,sal,round(((15/100)*sal)+sal) as Salary,
round(((15/100)*sal)+sal)-sal as Increment from emp;
From Introduction to Oracle: SQL and PL/SQL

EMPNO ENAME
SAL SALARY
INC
---------- ---------- ---------- ---------- ---------7369 SMITH
800
920
120
7499 ALLEN
1600
1840
240
7521 WARD
1250
1438
188
7566 JONES
2975
3421
446
7654 MARTIN
1250
1438
188
7698 BLAKE
2850
3278
428
7782 CLARK
2450
2818
368
7788 SCOTT
3000
3450
450
7839 KING
5000
5750
750
7844 TURNER
1500
1725
225
7876 ADAMS
1100
1265
165
EMPNO ENAME
SAL SALARY
---------- ---------- ---------- ---------- ---------7900 JAMES
950
1093
143
7902 FORD
3000
3450
450
7934 MILLER
1300
1495
195

INC

14 rows selected
4. Display the employees name, hiredate and salary review date, which is the first Monday after six
months of service. Label the column Review.
Query:
SQL> select ename,hiredate,Next_Day(Add_Months(hiredate,6),'monday') as Review from emp;
ENAME
HIREDATE REVIEW_DA
---------- --------- --------SMITH 17-DEC-80 22-JUN-81
ALLEN
20-FEB-81 24-AUG-81
WARD
22-FEB-81 24-AUG-81
JONES
02-APR-81 05-OCT-81
MARTIN 28-SEP-81 29-MAR-82
BLAKE
01-MAY-81 02-NOV-81
CLARK
09-JUN-81 14-DEC-81
SCOTT 19-APR-87 26-OCT-87
KING
17-NOV-81 24-MAY-82
TURNER 08-SEP-81 15-MAR-82
ADAMS
23-MAY-87 30-NOV-87
ENAME
HIREDATE REVIEW_DA
---------- --------- --------JAMES
03-DEC-81 07-JUN-82
FORD
03-DEC-81 07-JUN-82
From Introduction to Oracle: SQL and PL/SQL

MILLER

23-JAN-82 26-JUL-82

14 rows selected.
5. For each employee display the name and calculate the number of months between today and the date
the employee was hired. Label the column MONTHS_WORKED. Order your result by the number
of months employed. Round the number of months up to the closest whole number.
Query:
SQL> select ename,ROUND(Months_Between(sysdate,hiredate)) as months_Worked from emp
order by months_Worked;
ENAME
MONTHS_WORKED
---------- ------------ADAMS
315
SCOTT
317
MILLER
379
JAMES
381
FORD
381
KING
382
MARTIN
383
TURNER
384
CLARK
387
BLAKE
388
JONES
389
ENAME
MONTHS_WORKED
---------- ------------ALLEN
390
WARD
390
SMITH
393
14 rows selected.

6. Write a query that produces the following for each employee:


<employee name> earns <salary> monthly but wants <3 times salary>. Label the column Dream
Salaries. Sort the rows in descending of Dream Salary.
Example, Ward earns 1250 monthly but wants 3750.
Query:
SQL> select ename ||' '|| 'earns' ||' '|| sal ||' '|| 'monthly but wants' ||' '|| (sal*3) as dream
_salaries from emp;
DREAM_SALARIES
-------------------------------------------------------------------------------SMITH earns 800 monthly but wants 2400
From Introduction to Oracle: SQL and PL/SQL

ALLEN earns 1600 monthly but wants 4800


WARD earns 1250 monthly but wants 3750
JONES earns 2975 monthly but wants 8925
MARTIN earns 1250 monthly but wants 3750
BLAKE earns 2850 monthly but wants 8550
CLARK earns 2450 monthly but wants 7350
SCOTT earns 3000 monthly but wants 9000
KING earns 5000 monthly but wants 15000
TURNER earns 1500 monthly but wants 4500
ADAMS earns 1100 monthly but wants 3300
DREAM_SALARIES
-------------------------------------------------------------------------------JAMES earns 950 monthly but wants 2850
FORD earns 3000 monthly but wants 9000
MILLER earns 1300 monthly but wants 3900
14 rows selected.
7. Create a query to display name and salary for all employees. Format the salary to be 15 characters
long, left-padded with $. Label the column salary.
Query:
SQL> select ename,sal,Lpad(sal,15,'$') as salary from emp;
ENAME

SAL SALARY

---------- ---------- --------------SMITH

800 $$$$$$$$$$$$800

ALLEN

1600 $$$$$$$$$$$1600

WARD

1250 $$$$$$$$$$$1250

JONES
MARTIN

2975 $$$$$$$$$$$2975
1250 $$$$$$$$$$$1250

BLAKE

2850 $$$$$$$$$$$2850

CLARK

2450 $$$$$$$$$$$2450

SCOTT

3000 $$$$$$$$$$$3000

KING

5000 $$$$$$$$$$$5000

TURNER

1500 $$$$$$$$$$$1500

ADAMS

1100 $$$$$$$$$$$1100

ENAME

SAL SALARY

---------- ---------- --------------From Introduction to Oracle: SQL and PL/SQL

JAMES

950 $$$$$$$$$$$$950

FORD

3000 $$$$$$$$$$$3000

MILLER

1300 $$$$$$$$$$$1300

14 rows selected.
SQL>

From Introduction to Oracle: SQL and PL/SQL

You might also like