You are on page 1of 7

EXPERIMENT NO. - 2 Q.1.

Create table Employee with following structure : Field Emp_no Emp_name Designation Date_of_join Salary Dept_no Dept_loc Type number(5) varchar(30) varchar(10) date float integer varchar(20) Null no yes yes yes yes yes yes Key Primary Default NULL NULL NULL NULL NULL NULL

Ans. SQL>Create table employee( Emp_no number(5) Not Null Primary key , Emp_name varchar(30), Designation varchar(10), Date_of_join date, Salary float, Dept_no integer, Dept_loc varchar(20)); SQL>insert into employee values ('10001', 'Robert', 'Officer', '01-dec-85', '1000', '10','London'); SQL>insert into employee values ('10002', 'Allan', 'Clerk', '14-may-82', '5000', '10','London'); SQL>insert into employee values ('10003', 'Martin', 'Manager', '23-dec-84', '3500', '20','America'); SQL>insert into employee values ('10004', 'James', 'Analyst', '22 -jul -90', '5000', '30','New York'); SQL>insert into employee values ('10005', 'John', 'Analyst', '22 -jul -90', '4900', '30','New York'); SQL>insert into employee values ('10006', 'Jones', 'Clerk', '16 -apr -86', '950', '30','New York'); SQL>insert into employee values ('10007', 'Albert', 'Analyst', '20 -jun - 88', '1500', '60','Germany'); SQL>insert into employee values ('10008', 'Jerry', 'Manager', '19 -sep - 89', '6000', '80','Africa'); SQL>insert into employee values ('10009', 'Lisa', 'Officer', '22 -jan - 85', '7500', '90','Africa'); SQL>insert into employee values ('10010', 'Jack', 'Clerk', '10 -feb - 87', '4500', '20','America');

SQL>select * from employee; Emp_no 10001 10002 10003 10004 10005 10006 10007 10008 10009 10010 Emp_name Robert Allan Martin James John Jones Albert Jerry Lisa Jack Designation Officer Clerk Manager Analyst Analyst Clerk Analyst Manager Officer Clerk Date_of_join 01 -dec -85 14 -may -82 23 -dec -84 22 -jul -90 22 -jul -90 16 -apr -86 20 -jun - 88 19 -sep - 89 22 -jan - 85 10 -feb - 87 Salary 1000 5000 3500 5000 4900 950 1500 6000 7500 4500 Dept_no 10 10 20 30 30 30 60 80 90 20 Dept_loc London London America New York New York New York Germany Africa Africa America

Q.2. Create another table called 'Department' with the following structure : Field Dept_no Dept_name Dept_loc Type number(5) varchar(30) varchar(20) Key Primary Null no yes yes Default null null

Ans. SQL>Create table department( Dept_no number(5) Primary key , Dept_name varchar(30), Dept_loc varchar(20)); SQL>insert into department values ('10', 'marketing', 'London'); SQL>insert into department values ('20', 'accounts', 'America'); SQL>insert into department values ('30', 'sales', 'New York'); SQL>insert into department values ('40', 'software', 'Boston'); SQL>insert into department values ('50', 'production', 'Boston'); SQL>insert into department values ('60', 'marketing', 'Germany'); SQL>insert into department values ('70', 'accounts', 'China'); SQL>insert into department values ('80', 'software', 'Africa'); SQL>insert into department values ('90', 'sales', 'Africa'); SQL>insert into department values ('100', 'production', 'India'); SQL>select * from department;

Dept_no 10 20 30 40 50 60 70 80 90 100

Dept_name marketing accounts sales software production marketing accounts software sales production

Dept_loc London America New York Boston Boston Germany China Africa Africa India

Q.3. Create table Allowance : Field Designation Sp_allowance Conveyance Type varchar(10) Float Float

Ans. SQL>Create table allowance( Designation varchar(10) , Sp_allowance float, Conveyance float); SQL>insert into allowance values ('Manager', '1000', '500'); SQL>insert into allowance values ('Officer', '800', '400'); SQL>insert into allowance values ('Analyst', '1200', '500'); SQL>insert into allowance values ('Clerk', '500', '300'); SQL>insert into allowance values ('Analyst', '1500', '500'); SQL>insert into allowance values ('Officer', '900', '300'); SQL>insert into allowance values ('Officer', '700', '300'); SQL>insert into allowance values ('Manager', '1200', '400'); SQL>insert into allowance values ('Clerk', '400', '200'); SQL>insert into allowance values ('Manager', '1100', '400'); SQL>select * from allowance;

Designation Manager Officer Analyst Clerk Analyst Officer Officer Manager Clerk Manager

Sp_allowance 1000 800 1200 500 1500 900 700 1200 400 1100

Conveyance 500 400 500 300 500 300 300 400 200 400

Q.4. List the number of employee along with their department numbers in each department. Ans:SQL>select * count(Emp_name),Dept_name from Emp,Department where Department.Dept_no=Emp.Dept_no groupby Dept_name;
Dept_no

10 20 30 40 50 60 70 80 90 100

Dept_name Marketing Accounts Sales Software Production Marketing Accounts Software Sales Production

Count(Emp_name) 2 2 3 0 0 1 0 1 1 0

Q.5. List the department wise total salary. Ans.SQL>select * sum(Salary) from emp group by dept_no;
Dept_no

sum(Salary)

10 20 30 40 50 60 70 80 90 100

6000 8000 10850 0 0 1500 0 6000 7500 0

Q.6.List the number of employees in each designation in descending order. Ans:SQL>seect * designation,count(Emp_no)Emp_count from emp groupby designation orderby count(emp_no) desc; Designation Clerk Analyst Officer Manager Count(Emp_no) 3 3 2 2

Q.7.List the total salary,maximum salary and minimum salary along with the average salary of each employee greater than 1000 designation wise. SQL>select Designation,sum(Salary),max(Salary),min(Salary) from emp where Dept_no group by Desination having avg(Salary)>1000; Ans Designation Officer Clerk Manager Analyst Sum(Salary) 8500 10450 9500 11400 max(Salary) 7500 5000 6000 5000 min(Salary) 1000 950 3500 1500 Average 4250 3483 4750 3800

Q.8. List the total salary, maximum and minimum along with the average salary of each employee designation wise for department no 30. Ans:SQL>select * sum(Salary) from emp group by Designation where

Dept_no=30; Designation Sum(Salary) max(Salary) min(Salary) Average Clerk 950 950 950 950 Analyst 9900 5000 4900 4950

Query 6. List the total salary, maximum and minimum along with the average

salary of each employee designation wise for department no 30 and display only those rows that have their average greater than 1000. SQL> select sum(Salary),max(Salary),min(Salary),avg(Salary) where Dept_no=30 group by Designation having avg(Salary)>1000; Ans Designation Sum(Salary) max(Salary) min(Salary) Average Analyst 9900 5000 4900 4950 Query 7.List total salary of employees for each designation department wise. SQL> select Dept_no,sum(Salary),Designation from employee group by Designation order by Dept_no; Dept_no Sum(Salary) Designation 10 8500 Officer 20 10450 Clerk 30 9500 Manager 60 11400 Analyst

Query 8. List employee details such as employee no,name,date of joining,basic salary and designation for department=Marketing. SQL> select Emp_no,Emp_name, Designation, Date_of_join,Salary from employee,department where department.Dept_no=employee.Dept_no and Dept_name=marketing; Ans

Emp_no Emp_name Designation Date_of_join Salary

10001 10002 10007

Robert Allan Albert

Officer Clerk Analyst

01 -dec -85 1000 14 -may -82 5000 20 -jun - 88 1500

Query 9. List employee details such as employee no,name,date of joining,basic salary and designation working in location=America. SQL> select Emp_no,Emp_name, Designation, Date_of_join,Salary from employee where Dept_loc=America; Ans Emp_no Emp_name Designation Date_of_join Salary 10003 Martin Manager 23 -dec -84 3500 10010 Jack Clerk 10 -feb - 87 4500

Query 10. List the department where there are no employees functioning. SQL> select Dept_name,Dept_no from department where not exists(select Dept_no from employee); Ans Dept_no Dept_name 40 Software 50 Production 70 Accounts 100 Production

You might also like