DBMS Queries
DBMS Queries
EXPERIMENT 2
Objective : Create a table department having following attributes
1. Department number ( as primary key)
2. Department name
3. Department location
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
Page 8
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
Page 10
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
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
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
Page 12
(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
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
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
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
Page 16
(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)
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
(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