You are on page 1of 7

DBMS Lab Assignment 4 (Join Based Queries)

1. SalaryGrade Table
GRADE LOWERSAL HIGHSAL

------ --------- ---------

1 700 1200

2 1201 1400

3 1401 2000

4 2001 3000

5 3001 9999

2. EMP Table
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

3. DEPT Table:

DEPTNO DNAME LOC

------- -------------- ----------

10 ACCOUNTING NEW YORK

20 RESEARCH DALLAS

30 SALES CHICAGO

40 OPERATIONS BOSTON

Dept.
create table dept(
deptno number(2,0),
dname varchar2(14),
loc varchar2(13),
constraint pk_dept primary key (deptno)
);

Emp..
create table emp(
empno number(4,0),
ename varchar2(10),
job varchar2(9),
mgr number(4,0),
hiredate date,
sal number(7,2),
comm number(7,2),
deptno number(2,0),
constraint pk_emp primary key (empno),
constraint fk_deptno foreign key (deptno) references dept (deptno)
);

Salarygrade
create table Salarygrade(
grade number,
losal number,
hisal number
);

Dept.

insert into dept values(10, 'ACCOUNTING', 'NEW YORK');


insert into dept values(20, 'RESEARCH', 'DALLAS');
insert into dept values(30, 'SALES', 'CHICAGO');
insert into dept values(40, 'OPERATIONS', 'BOSTON');

Emp.

insert into emp


values(
7839, 'KING', 'PRESIDENT', null,
to_date('17-11-1981','dd-mm-yyyy'),
5000, null, 10
);

insert into emp


values(
7698, 'BLAKE', 'MANAGER', 7839,
to_date('1-5-1981','dd-mm-yyyy'),
2850, null, 30);
insert into emp values(
7782, 'CLARK', 'MANAGER', 7839,
to_date('9-6-1981','dd-mm-yyyy'),
2450, null, 10
);

insert into emp


values(
7566, 'JONES', 'MANAGER', 7839,
to_date('2-4-1981','dd-mm-yyyy'),
2975, null, 20
);

insert into emp


values(
7788, 'SCOTT', 'ANALYST', 7566,
to_date('13-JUL-87','dd-mm-rr') - 85,
3000, null, 20
);

insert into emp


values(
7902, 'FORD', 'ANALYST', 7566,
to_date('3-12-1981','dd-mm-yyyy'),
3000, null, 20
);

insert into emp


values(
7369, 'SMITH', 'CLERK', 7902,
to_date('17-12-1980','dd-mm-yyyy'),
800, null, 20
);

insert into emp


values(
7499, 'ALLEN', 'SALESMAN', 7698,
to_date('20-2-1981','dd-mm-yyyy'),
1600, 300, 30
);

insert into emp


values(
7521, 'WARD', 'SALESMAN', 7698,
to_date('22-2-1981','dd-mm-yyyy'),
1250, 500, 30
);

insert into emp


values(
7654, 'MARTIN', 'SALESMAN', 7698,
to_date('28-9-1981','dd-mm-yyyy'),
1250, 1400, 30
);

insert into emp


values(
7844, 'TURNER', 'SALESMAN', 7698,
to_date('8-9-1981','dd-mm-yyyy'),
1500, 0, 30
);

insert into emp


values(
7876, 'ADAMS', 'CLERK', 7788,
to_date('13-JUL-87', 'dd-mm-rr') - 51,
1100, null, 20
);

insert into emp


values(
7900, 'JAMES', 'CLERK', 7698,
to_date('3-12-1981','dd-mm-yyyy'),
950, null, 30
);

insert into emp


values(
7934, 'MILLER', 'CLERK', 7782,
to_date('23-1-1982','dd-mm-yyyy'),
1300, null, 10
);
Salarygrade

insert into Salarygrade


values (1, 700, 1200);

insert into Salarygrade


values (2, 1201, 1400);

insert into Salarygrade


values (3, 1401, 2000);

insert into Salarygrade


values (4, 2001, 3000);

insert into Salarygrade


values (5, 3001, 9999);

Perform the following queries:


1. Display all employee name and their department name, in department name order.
2. Display all employee names, department number and name.
3. Display the name, location and department of employees whose salary is more than 1500
a month.
4. Produce a list showing employees salary grades.
5. Show only employees on Grade 3.
6. Show, all employees in dallas.
7. List the employee name, job, salary, grade and department name for everyone in the
company except clerks. Sort on salary, displaying the highest salary first.
8. List the following details for employees who earn $36000 a year or who are clerks.
9. Display the department that has no employees.
10. List all employees by name and number along with their managers name and number.
11. Modify solution to question 10 to display KING who has no manager.
12. Find the job that was filled in the first half of 1993, and the same job that waf filled
during the same period in 1994.
13. Find all employees who joined the company before their manager.
14. Find another query method for Question 9.
15. Find employee(s) who earn the highest salary in each job type. Sort in descending order
of salary.
16. Find the employees who earn the minimum salary for their job. Display the result in
ascending order of salary.
17. Find the most recently hired employees in each department. Order by hire_date.
18. Show the following details for any employee who earns a salary greater than the average
for their department. Sort in department number order.
19. List all the departments where there are no employees.(Using , a subquery this time)
20. Display the following information for the department with HIGHEST annual
remuneration bill.
21. Who are the top 3 earners in the company? Display their names and salary.
22. In which year did the most people join the company? Display the year and number of
employees.
23. Modify question 18 to also display the average salary figure for the department.
24. Write a query to display an *against the row of the most recently hired employee.
Display Ename,HIREDATE AND COLUMN(MAXDATE) SHOWING *.
25. CREATE A TABLE CALLED projects, with columns as specified below. In addition,
define PROJID as the primary key column, and ensure that P_END_DATE dates are not
earlier than P_START_DATES dates.

Column Name data type Size


PROJID NUMBER 4
P_DESC VARCHAR2 20
P_START_DATE DATE
P_END_DATE DATE
BUDGET_AMOUNT NUMBER 7,2
MAX_NO_STAFF NUMBER 2
26. CREATE ANOTHER table, ASSIGNMENTS. Define its PROJID column as a forign
keywhich

Column name data type Size


PROJID NUMBER 4
EMP NO NUMBER 4
A_START_DATE DATE
A_END_DATE DATE
BILL_RATE NUMBER 4,2
ASSIGN TYPE NUMBER 2
27. Add a LONG column called comments to your PROJECTS TABLE. Also, add a
NUMBER column called HOURS to the ASSIGNMENTS table.
28. Use the USER-OBJECTS dictionary view to list the objects owned by your Oracle
account. How many objects do you own?
29. Now define a constraint on the ASSIGNMENTS table to ensure unique combinations of
Project ID and Employee number.
30. Requery the USER_OBJECTS view; do you now own any further objects?
31. Look in the Data Dictionary for information about constraints on your tables(you can use
the view USER CONSTRAINTS
32. Use tha ALL-TABLES dictionary view to find the owners and names of tables you can
access where the tables name includes the characters EMP.
33. Insert into the PROJECTS table the following rows:
PROJID

P_DESC WRITE C030 COURSE PROOF RAED NOTES


P_START_DATE 02-JAN-88 01-JAN-89
P_END_DATE 07-JAN-88 01-JAN-89
BUDGET_AMOUNT 500 600
MAX_NO_STAFF

You might also like