dbms lab 2

Employee Database

EMP:
EMPNO ENAME JOB MGR HIREDATE SAL COMM DEPTNO
7369 SMITH CLERK 7902 17-DEC-80 800 20
7499 ALLEN SALESMAN 7698 20-FEB-81 1600 300 30
7521 WARD SALESMAN 7698 22-FEB-81 1250 500 30
7566 JONES MANAGER 7839 02-APR-81 2975 20
7654 MARTIN SALESMAN 7698 28-SEP-81 1250 1400 30
7698 BLAKE MANAGER 7839 01-MAY-81 2850 30
7782 CLARK MANAGER 7839 09-JUN-81 2450 10
7788 SCOTT ANALYST 7566 19-APR-87 3000 20
7839 KING PRESIDENT 17-NOV-81 5000 10
7844 TURNER SALESMAN 7698 08-SEP-81 1500 0 30
7876 ADAMS CLERK 7788 23-MAY-87 1100 20
7900 JAMES CLERK 7698 03-DEC-81 950 30
7902 FORD ANALYST 7566 03-DEC-81 3000 20
7934 MILLER CLERK 7782 23-JAN-82 1300 10


DEPT:

DEPTNO DNAME LOC
10 ACCOUNTING NEW YORK
20 RESEARCH DALLAS
30 SALES CHICAGO
40 OPERATIONS BOSTON



SALGRADE:

GRADE LOSAL HISAL
1 700 1200
2 1201 1400
3 1401 2000
4 2001 3000
5 3001 9999



CYCLE - 02

Problem NO: 01

STATEMENT: BASIC QUERIES ON EMPLOYEE, DEPARTMENT TABLES USING BUILT-IN FUNCTIONS.

1. Display the department names in the lower case but the initial must be in uppercase.
2. Determine the ‘ename’, ‘job’, ‘sal’ rename the title as Job-sal the output must be
Job-Sal as SMITH[CLERKRS.2000]
3. Count the number of times S occurs in department names.
4. Write a query to display the department name which does not contains any employees.
5. Write a query to display all employee details where employee was joined in year date wise 1980 and 1990 and 2nd week of every month
6. Write an SQL statement to convert the current date to new date picture
ex: MONDAY 10th 2005 10:30.00 PM
7. Write a query to display details of all employees who joined on last Wednesday of every month and whose experience is more than 20 months.
8. Write a query to calculate the service of employees rounded to years.
9. Write a query that will display all different job types.
10. Write a query that will display list of employees and their salary and the comments as follows.
If the salary is more than 1500 then display “above target”
if the salary is equal to 1500 then display “on the target”
if the salary is less than 1500 then display “below the target”
11. Display all employee names, employee number, department names & salary grades for all employees who are working in department 30.
12. Display the time of day.













Problem NO: 02

STATEMENT: QUERIES WITH GROUP FUNCTIONS AND NESTED QUERIES.

1 Find all employees who earn a salary greater than the average salary of their departments.
2 Write a query to sort names of employees in department 10 & 20 in alphabetical order
3 Write a query to display all job types.
4 Calculate the number of employees and average salary of each department.
5 Write a query to find the name of the manager and number of sub-ordinates.
6 Write a query to find out the manager having Maximum number of sub-ordinates.
7 (a) Write a query to find out the top three earners.
(b) Write a query to find out the least three earners.
8 Display the average, monthly salary bill for each job type with in department.
9 Find all employees whose dept is not in dept table.
10 (a) Write a query to find out the employees who have joined before their managers.
(b) Write a query to find out the year, where most people join in the company displays the year and No. of Employees.
11 (a) List the department names of each employee.
(b) Find all employees who earn lowest salary in each department.
12 (a) Display all employees who earn more than the lowest salary of department 30.
(b) Display all departments, which have the average salary more than average salary of department.
13 Write a query which will return the DAY of the week.(ie. MONDAY), for any date entered in the format: DD.MM.YY.



Quaries:::

1sol:
SQL> select initcap(dname) from dept;

INITCAP(DNAME)
--------------
Accounting
Research
Sales
Operations

2sol:
SQL> select ename ||'[' || job||'rs'||sal||']' as job_sal from emp;

JOB_SAL
---------------------------------------------------------------
SMITH[CLERKrs800]
ALLEN[SALESMANrs1600]
WARD[SALESMANrs1250]
JONES[MANAGERrs2975]
MARTIN[SALESMANrs1250]
BLAKE[MANAGERrs2850]
CLARK[MANAGERrs2450]
SCOTT[ANALYSTrs3000]
KING[PRESIDENTrs5000]
TURNER[SALESMANrs1500]
ADAMS[CLERKrs1100]

JOB_SAL
---------------------------------------------------------------
JAMES[CLERKrs950]
FORD[ANALYSTrs3000]
MILLER[CLERKrs1300]

14 rows selected.

3sol:
SQL> select count (instr(dname,'s')) from dept;

COUNT(INSTR(DNAME,'S'))
-----------------------
4


4sol:
SQL> select dname from dept where deptno not in ( select deptno from emp);

DNAME
--------------
OPERATIONS


5sol:
SQL> select empno,ename,hiredate from emp where to_char(hiredate,'YYYY')between '1980' and'1990' and to_char(hiredate,'w')=2;

EMPNO ENAME HIREDATE
---------- ---------- ---------
7782 CLARK 09-JUN-81
7844 TURNER 08-SEP-81


6sol:
SQL> select to_char(sysdate,'day MM YYYY HH:MM:SS pm') from dual;

TO_CHAR(SYSDATE,'DAYMMYYYYHH:
-----------------------------
monday 08 2009 01:08:54 pm

7sol:










8sol:SQL> select ename,round((Months_between(sysdate,hiredate)/12))as service from emp;

ENAME SERVICE
---------- ----------
SMITH 29
ALLEN 29
WARD 29
JONES 28
MARTIN 28
BLAKE 28
CLARK 28
SCOTT 22
KING 28
TURNER 28
ADAMS 22

ENAME SERVICE
---------- ----------
JAMES 28
FORD 28
MILLER 28

14 rows selected.

9sol:
SQL> select distinct job from emp;

JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN


10 sol:
SQL> (select ename,sal||'above target' from emp where sal>1500)union(select ename,sal||'on the target' from emp where sal=1500) union( select ename,sal||'below target'from emp wher
e sal< 1500);

ENAME SAL||'ABOVETARGET'
---------- -----------------------------------------------------
ADAMS 1100below target
ALLEN 1600above target
BLAKE 2850above target
CLARK 2450above target
FORD 3000above target
JAMES 950below target
JONES 2975above target
KING 5000above target
MARTIN 1250below target
MILLER 1300below target
SCOTT 3000above target

ENAME SAL||'ABOVETARGET'
---------- -----------------------------------------------------
SMITH 800below target
TURNER 1500on the target
WARD 1250below target

14 rows selected.

11sol:
SQL> select ename,empno,dname,grade from emp e,dept d,salgrade where e.deptno=d.deptno and d.deptno=30;

ENAME EMPNO DNAME GRADE
---------- ---------- -------------- ----------
ALLEN 7499 SALES 1
BLAKE 7698 SALES 1
MARTIN 7654 SALES 1
JAMES 7900 SALES 1
TURNER 7844 SALES 1
WARD 7521 SALES 1
ALLEN 7499 SALES 2
BLAKE 7698 SALES 2
MARTIN 7654 SALES 2
JAMES 7900 SALES 2
TURNER 7844 SALES 2

ENAME EMPNO DNAME GRADE
---------- ---------- -------------- ----------
WARD 7521 SALES 2
ALLEN 7499 SALES 3
BLAKE 7698 SALES 3
MARTIN 7654 SALES 3
JAMES 7900 SALES 3
TURNER 7844 SALES 3
WARD 7521 SALES 3
ALLEN 7499 SALES 4
BLAKE 7698 SALES 4
MARTIN 7654 SALES 4
JAMES 7900 SALES 4

ENAME EMPNO DNAME GRADE
---------- ---------- -------------- ----------
TURNER 7844 SALES 4
WARD 7521 SALES 4
ALLEN 7499 SALES 5
BLAKE 7698 SALES 5
MARTIN 7654 SALES 5
JAMES 7900 SALES 5
TURNER 7844 SALES 5
WARD 7521 SALES 5

30 rows selected.

12sol:
SQL> select to_char(sysdate,'HH:MI:SS') from dual;

TO_CHAR(
--------
01:26:58









second query:


1sol:

SQL> select ename,sal from emp where sal>(select avg(sal) from emp);

ENAME SAL
---------- ----------
JONES 2975
BLAKE 2850
CLARK 2450
SCOTT 3000
KING 5000
FORD 3000
2sol:
SQL> select ename from emp where deptno in(10,20) order by ename;

ENAME
----------
ADAMS
CLARK
FORD
JONES
KING
MILLER
SCOTT
SMITH

3sol:
SQL> select distinct job from emp;

JOB
---------
ANALYST
CLERK
MANAGER
PRESIDENT
SALESMAN

4sol:
SQL> select count(*),avg(sal)from emp group by deptno;

COUNT(*) AVG(SAL)
---------- ----------
3 2916.66667
5 2175
6 1566.66667

5sol:
SQL> select e1.ename,count(*) from emp e1,emp e2 where e2.mgr=e1.empno group by e1.ename;

ENAME COUNT(*)
---------- ----------
BLAKE 5
CLARK 1
FORD 1
JONES 2
KING 3
SCOTT 1

6 rows selected.

1 comments:

Unknown said...

bro where is 7 query solution

Post a Comment