Vous êtes sur la page 1sur 11

DATABASE MANAGEMENT SYSTEMS LAB

EXPERIMENT 2
Objective : Create a table department having following attributes
1. Department number ( as primary key)
2. Department name
3. Department location

CREATE TABLE depart


(dept_no number primary key,
dept_name varchar2(10) NOT NULL,
location varchar2(10)
);
DESC depart;
Lengt
h

Precisio
n

Scal
e

Number

DEPT_NA
ME

Varchar2

10

LOCATION

Varchar2

10

Table

Col umn

DEPART

DEPT_NO

Data
Type

Primary
Key

Nullab
le

Def au
lt

Comme
nt

1-3

insert into depart values (10,'accounting','new york');


insert into depart values (20,'research','dallas');
insert into depart values(30,'sales','chicago');

Page 8

DATABASE MANAGEMENT SYSTEMS LAB


Objective : Create a table employee having following attributes
1.
2.
3.
4.
5.
6.

Employee number ( as primary key)


Employee name
Employee designation
Employee salary
Employee joining date
Department number with references from table department

Create table employ


(emp_no number(10) primary key,
name varchar2(15) not null,
designation varchar2(10),
mgr_no number,
joining date not null,
salary number(6),
commission number(7),
dept_no references depart
);
Desc employ;

Table
EMPLOY

Col umn

Data
Type

Leng
th

Precisi
on

Sca
le

Primary
Key

Nulla
ble

Def a
ult

Comm
ent

EMP_NO

Number

10

NAME

Varchar2

15

DESIGNAT
ION

Varchar2

10

MGR_NO

Number

JOINING

Date

SALARY

Number

COMMISSI
ON

Number

DEPT_NO

Number

1-8

Page 9

DATABASE MANAGEMENT SYSTEMS LAB


insert into employ values(7369, 'smith', 'clerk', 7902,'17-dec-1980',800,null,20);
insert into employ values(7499, 'allen', 'salesman', 7698,'20-feb-1981',1600,300,30);
insert into employ values(7521, 'ward', 'salesman', 7698,'22-feb-1981',1250,500,30);
insert into employ values(7566, 'jones', 'manager', 7839,'02-apr-1981',2975,null,20);
insert into employ values(7654, 'martin', 'salesman', 7698,'28-sep-1981',1250,1400,30);
insert into employ values(7698, 'blake', 'manager', 7839,'01-may-1981',2850,null,30);
insert into employ values(7782, 'clark', 'manager', 7839,'09-jun-1981',2450,null,10);
insert into employ values(7788, 'scott', 'analyst', 7566,'09-dec-1982',3000,null,20);
insert into employ values(7839, 'king', 'president',null,'17-nov-1981',5000,null,10);
insert into employ values(7844, 'turner', 'salesman', 7698,'08-sep-1981',1500,0,30);
insert into employ values(7876, 'adams', 'clerk', 7788,'12-jan-1983',1100,null,20);
insert into employ values(7900, 'james', 'clerk', 7698,'03-dec-1981',950,null,30);
insert into employ values(7902, 'ford', 'analyst', 7566,'04-dec-1981',3000,null,20);
insert into employ values(7934, 'miller', 'clerk', 7782,'23-jan-1982',1300,null,10);
insert into employ values(7903, 'James', 'clerk', 7689,'23-feb-1986',1300,null,30);

Page 10

DATABASE MANAGEMENT SYSTEMS LAB

QUERIES :
(1)Write the query to view the above created table.
select * from depart;
DEPT _NO

DEPT _NAME

LOC ATION

10

accounting

new york

20

research

dallas

30

sales

chicago

Select * from employ;


EMP_NO

NAME

DESI GN ATION

MGR_NO

JOINI NG

SAL ARY

COMMI SSION

DEPT _NO

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

09-DEC-82

3000

20

7839

king

president

17-NOV-81

5000

10

7844

turner

salesman

7698

08-SEP-81

1500

30

7876

adams

clerk

7788

12-JAN-83

1100

20

7900

james

clerk

7698

03-DEC-81

950

30

7902

ford

analyst

7566

04-DEC-81

3000

20

7934

miller

clerk

7782

23-JAN-82

1300

10

7903

James

clerk

7689

23-FEB-86

1300

30

(2) List the name and salary of employee whose salary is more than 2500.
Select name, salary from employ
Where salary > 2500;
NAME

SAL ARY

jones

2975

blake

2850

scott

3000

king

5000

ford

3000

Page 11

DATABASE MANAGEMENT SYSTEMS LAB


(3) List the name of designation, salary whose working department number is 10 .
Select designation, salary from employ
Where dept_no = 10;
DESI GN ATION

SAL ARY

manager

2450

president

5000

clerk

1300

(4) List the name of employees whose salary is 1600 or having no commission.
Select name from employ
Where salary = 1600 OR commission = 0;
NAME
allen
turner

(5) List the name of employees whose salary is between 2000 and 5000.
Select name from employ
Where salary between 2000 and 5000;
NAME
jones
blake
clark
scott
king
ford

(6) List the name of employees who are not clerk.


Select name from employ
Where designation != clerk;

Page 12

DATABASE MANAGEMENT SYSTEMS LAB


NAME
allen
ward
jones
martin
blake
clark
scott
king
turner
ford

(7) List the details of employee who have joined before the end of april,1981 .
Select * from employ
Where joining < 30-APR-81 ;
EMP_NO

NAME

DESI GN ATION

MGR_NO

JOINI NG

SAL ARY

COMMI SSION

DEPT _NO

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

(8) List the name of employees whose manager number are 7782, 7566.
Select name from employ
Where mgr_no in (7782, 7566) ;
NAME
scott
ford
miller

(9) List the details of employees whose salary is greater than 2500 and department number
is 30.
Select * from employ
Where salary >2500 and dept_no =30 ;

Page 13

DATABASE MANAGEMENT SYSTEMS LAB


EMP_NO

NAME

DESI GN ATION

MGR_NO

JOINI NG

SAL ARY

COMMI SSION

DEPT _NO

7698

blake

Manager

7839

01-MAY-81

2850

30

(10) List the name and perks ( 10% of salary) of all employees.
Select name, salary*0.1 perks from employ;
NAME

Per ks

smith

80

allen

160

ward

125

jones

297.5

martin

125

blake

285

clark

245

scott

300

king

500

turner

150

adams

110

james

95

ford

300

miller

130

James

130

(11) List name, employment number, salary of all employees in ascending order of salary.
Select name, emp_no, salary from employ
Order by salary;
NAME

EMP_NO

SAL ARY

smith

7369

800

james

7900

950

adams

7876

1100

ward

7521

1250

martin

7654

1250

James

7903

1300

miller

7934

1300

turner

7844

1500

allen

7499

1600

clark

7782

2450

blake

7698

2850

Page 14

DATABASE MANAGEMENT SYSTEMS LAB


jones

7566

2975

scott

7788

3000

ford

7902

3000

king

7839

5000

(12) List name, employment number of all employees in ascending order of salary and
descending order of department number if salary is same.
Select name, emp_no, salary, dept_no from employ
Order by salary asc, dept_no desc ;
NAME

EMP_NO

SAL ARY

DEPT _NO

smith

7369

800

20

james

7900

950

30

adams

7876

1100

20

ward

7521

1250

30

martin

7654

1250

30

James

7903

1300

30

miller

7934

1300

10

turner

7844

1500

30

allen

7499

1600

30

clark

7782

2450

10

blake

7698

2850

30

jones

7566

2975

20

scott

7788

3000

20

ford

7902

3000

20

king

7839

5000

10

(13) List the details of employees not belonging to department 10 and 20.
Select * from employ where dept_no not in (10, 20) ;
EMP_NO

NAME

DESI GN ATION

MGR_NO

JOINI NG

SAL ARY

COMMI SSION

DEPT _NO

7499

allen

salesman

7698

20-FEB-81

1600

300

30

7521

ward

salesman

7698

22-FEB-81

1250

500

30

7654

martin

salesman

7698

28-SEP-81

1250

1400

30

7698

blake

manager

7839

01-MAY-81

2850

30

7844

turner

salesman

7698

08-SEP-81

1500

30

7900

james

clerk

7698

03-DEC-81

950

30

7903

James

clerk

7689

23-FEB-86

1300

30

Page 15

DATABASE MANAGEMENT SYSTEMS LAB


(14) List the number of all employees.
Select count(*) "No. of workers" from employ;
No. Of Workers
15

(15) List the details of employees whose name is starting with J.


Select * from employ
Where name like 'j%' or name like 'J%' ;
EMP_NO

NAME

DESI GN ATION

MGR_NO

JOINI NG

SAL ARY

COMMI SSION

DEPT _NO

7566

jones

manager

7839

02-APR-81

2975

20

7900

james

clerk

7698

03-DEC-81

950

30

7903

James

clerk

7689

23-FEB-86

1300

30

(16) List the details of employees whose name is a 5 letter string, starting with J and ending
with S.
Select * from employ
Where name like 'j___s' ;
EMP_NO

NAME

DESI GN ATION

MGR_NO

JOINI NG

SAL ARY

COMMI SSION

DEPT _NO

7566

jones

manager

7839

02-APR-81

2975

20

7900

james

clerk

7698

03-DEC-81

950

30

(17) List the maximum salary offered to employees.


Select max(salary) from employ;
MAX( SAL ARY)
5000

(18) List the minimum salary offered to employees denote as min_salary.


Select min(salary) from employ ;
MIN(SAL ARY)
800

Page 16

DATABASE MANAGEMENT SYSTEMS LAB


(19) List the total salary offered to employees.
Select sum(salary) from employ ;
SUM( SAL ARY)
30325

(20) List the number of different departments.


Select count(distinct dept_no)"Total departments" from employ ;
Total Departments
3

(21) List the number of jobs available in the employee database.


Select count(distinct designation)"Total jobs" from employ ;
Total Jobs
5

(22) List the total salary, maximum salary, minimum salary of all employees according to
their jobs.
Select sum(salary), max(salary), min(salary) from employ
Group by designation ;
SUM( SAL ARY)

MAX( SAL ARY)

MIN(SAL ARY)

5600

1600

1250

5450

1300

800

5000

5000

5000

8275

2975

2450

6000

3000

3000

(23) List the average salary of all departments having more than three people.
Select avg(salary) from employ
Group by dept_no having count(emp_no) >3 ;

Page 17

DATABASE MANAGEMENT SYSTEMS LAB


AVG( SAL ARY)
1528.57142857142857142857142857142857143
2175

(24) List the average salary of all departments having three or less than three people.
Select avg(salary) from employ
Group by dept_no having count(emp_no) <= 3 ;
AVG( SALARY)
2916.66666666666666666666666666666666667

Page 18