1. Create the following tables concedring the constraints
mentioned in the constraint field
Table Name Columns Type Constraints
Student Roll Varchar2(10) Primary Key Name Varchar2(20) Not null Sex Char(1) Must be Mor Fdefault M Address Varchar2(50) Default Thapar University Campus Dname Varchar2(12) Refer its value from Dname field of Dept table
Table Name Columns Type Constraints
Dept Dname Varchar2(12) Primary key,value must be either Mathematicsor Physics,or Chemistry or Engineering HOD Varchar2(15) Phone Number(8) Unique
create table Dept ( Dname Varchar2(12)primary key check (Dname='Mathematics' or Dname='Physics' or Dname='Chemistry' or Dname='Engineering' ), HOD Varchar2(15), Phone Number(8) unique );
create table Student ( Roll Varchar2(10)primary key, Name Varchar2(20) not null, Sex char(1)default 'M' check (Sex='M' or Sex='F' ), Address Varchar2(50) default 'Thapar University Campus', Dname Varchar2(12) REFERENCES Depttt(Dname) );
Assignment-3
Create and insert records into EMP and DEPT tables as shown below.Then solve the folllowing queries on the basis of these tables:
INSERT INTO DEPT VALUES(10,'Accounting','Boston'); INSERT INTO DEPT VALUES(20,'Research','New York'); INSERT INTO DEPT VALUES(30,'Sales','Chicago');
INSERT INTO EMP VALUES(7369,'Smith','Clerk',7902,TO_DATE('17/12/80','dd/mm/yy'),800,20);
INSERT INTO EMP VALUES(7499,'Allen','Salesman',7698,TO_DATE('20/02/81','dd/mm/yy'),1600, 30);
INSERT INTO EMP VALUES(7521,'Ward','Salesman',7698,TO_DATE('22/02/81','dd/mm/yy'),1250,3 0);
INSERT INTO EMP VALUES(7566,'Jones','Manager',7839,TO_DATE('2/04/81','dd/mm/yy'),2975,20 ); INSERT INTO EMP VALUES(7654,'Martin','Salesman',7698,TO_DATE('28/09/81','dd/mm/yy'),1250 ,30);
INSERT INTO EMP VALUES(7698,'Blake','Manager',7839,TO_DATE('01/05/81','dd/mm/yy'),2850,3 0);
INSERT INTO EMP VALUES(7782,'Clark','Manager',7839,TO_DATE('09/06/81','dd/mm/yy'),2450,1 0);
INSERT INTO EMP VALUES(7788,'Scott','Analyst',7566,TO_DATE('19/04/87','dd/mm/yy'),3000,2 0);
INSERT INTO EMP(Emp_no,Ename,Job,Hiredate,Sal,Dept_no) VALUES(7839,'King','President',TO_DATE('17/11/81','dd/mm/yy'),5000,10);
INSERT INTO EMP VALUES(7844,'Turner','Salesman',7698,TO_DATE('08/09/81','dd/mm/yy'),1500 ,30);
INSERT INTO EMP VALUES(7876,'Adams','Clerk',7787,TO_DATE('23/05/87','dd/mm/yy'),1100,20) ;
INSERT INTO EMP VALUES(7900,'James','Clerk',7698,TO_DATE('03/12/81','dd/mm/yy'),950,30);
INSERT INTO EMP VALUES(7902,'Ford','Analyst',7566,TO_DATE('03/12/81','dd/mm/yy'),3000,20 );
INSERT INTO EMP VALUES(7934,'Miller','Clerk',7782,TO_DATE('23/01/82','dd/mm/yy'),1300,10 );
Table:DEPT
Table:EMP
1. List all the records in EMP table
SELECT * FROM EMP;
2. List all the employee names and their corresponding salary from EMP table.Use Employee name and Salary as the alias of Ename and Sal respectively
SELECT Ename as Employeename,Sal as Salary from EMP;
3. List the distinct Department number and Salary from EMP table SELECT distinct (Dept_no),Sal from EMP;
4.List the employee number,name and job of the employees who are working as manager and salary is greater than 1500 SELECT Emp_no,Ename,Job from EMP where Job='Manager'and Sal>1500;
5.List the names and department number of employees not belonging to department 10 or 30 SELECT Ename,Dept_no from EMP WHERE Dept_no<>10 AND Dept_no<>30 ;
6.List the employee number of employees whose name is not Smith,Clark,Ford SELECT Emp_no from EMP where Ename<>'Smith'and Ename<>'Clark' and Ename<>'Ford';
7.List the employee names and salary from EMP table whose salary is between 1200 and 2000 SELECT Ename,Sal from EMP where Sal>1200 and Sal<2000;
8.List the employee names having cas the second character SELECT Ename,Sal from EMP where Ename like '_c%';
9.List the employee names ending with h SELECT Ename,Sal from EMP where Ename like '%h';
10.List the employee names having two s in their name SELECT Ename,Sal from EMP where Ename like '%s%s%';
11.List the employee name and job of employee who do not have manager SELECT Ename,Job from EMP where Mgr is NULL;
12.List the employee name and salary of employees who do the job clerk and them by their salary in ascending order SELECT Ename,Sal from EMP where Job='clerk' order by sal ;
13.List the employee name ,salary,hiredate of employees and sort the result by their hiredate in ascending order and salary in descending order SELECT Ename,Sal,Hiredate from EMP order by hiredate , Sal desc ;
14.list the jobs which are exist in department 20 only Select distinct Job from Emp where Dept_no=20 MINUS Select distinct Job from Emp where Dept_no<>20;
15.List the jobs common to department all department Select Job FROM EMP WHERE Dept_no=10 intersect Select Job FROM EMP WHERE Dept_no=20 intersect Select Job FROM EMP WHERE Dept_no=30
Assignment-4
Perform the following query based on the EMP and DEPT tables
1. Count the no of employees working in each department select count(Emp_no),D_name from EMP,DEPT where EMP.Dept_no=DEPT.Dept_no group by D_name
2. List the number of employees doing the job of Clerk select count(Emp_no) from EMP where Job='Clerk';
3.List the length of names of employees in the EMP table
SELECT length(Ename) FROM EMP ;
4.List the name of the employees earning minimum salary Select Ename from EMP where Sal = (SELECT min(Sal) FROM EMP) ;
5.List the name of employees who earn salary greater than the average salary.Also count their number Select Ename from EMP where Sal > (SELECT avg(Sal) FROM EMP);
Select count(Emp_no) from EMP where Sal > (SELECT avg(Sal) FROM EMP);
6.List the department number where maximum salary is greater than 2900 select Dept_no from EMP group by Dept_no having max(Sal)>2900 ;
7.List the name of employee working in accounting department select Ename from EMP,DEPT where EMP.Dept_no=DEPT.Dept_no and D_name='Accounting';
8.List the job,total and average salary of employees working in the job for department 20 and having average salary greater than 1000 select Job,sum(Sal)as Sal_sum ,avg(Sal) as Sal_avg from EMP where Dept_no=20 group by (Job) having avg(Sal)>1000
9.Find the name of employee who are working in more than 30 years
select Ename from EMP where floor(sysdate-Hiredate)>30*365 ;
10. Find the name of employee working in boston select Ename from EMP,DEPT where EMP.Dept_no=DEPT.Dept_no and Location='Boston';
11.List the department number,job and total salary being paid to each job within each department SELECT EMP.Dept_no,Job,sum(Sal)from EMP,DEPT where EMP.Dept_no=DEPT.Dept_no group by EMP.Dept_no,Job
12.Concider the following employee database schema EMPLOYEE(ESSN,ENAME,DEPT_NO,SALARY) DEPENDENT(ESSN,DEPEND_NAME,RELATION,DOB) DEPARTMENT(DEPT_NO,DEPT_NAME,MANAGER) Create the appropriate database using ORACLE.Perform the following queries using sql create table DEPARTMENT( DEPT_NO int primary key, DEPT_NAME varchar2(20), MANAGER varchar2(20) ) create table EMPLOYEE ( ESSN int, ENAME varchar2(20), DEPT_NO int REFERENCES DEPARTMENT (DEPT_NO), SALARY int, ) create table DEPENDENT( ESSN int references EMPLOYEE(ESSN), DEPEND_NAME varchar2(20), RELATION varchar2(20), DOB date )
Table:DEPARTMENT
Table:EMPLOYEE
Table:DEPENDENT
i) Find details of dependent for employee having name AJAY SELECT DEPEND_NAME,RELATION,DOB FROM EMPLOYEE,DEPENDENT WHERE EMPLOYEE.ESSN=DEPENDENT.ESSN AND ENAME='AJAY'
ii)Find the name of the manager of the department in which employee with ESSN code 5078 works
SELECT MANAGER FROM DEPARTMENT WHERE DEPT_NO IN(SELECT DEPT_NO FROM EMPLOYEE WHERE ESSN=5078 AND SALARY IS NOT NULL)
iii)Find DOB of son of the employee having employee code ESSN 5078 SELECT DOB FROM DEPENDENT WHERE RELATION='SON' and ESSN=5078
iv)Find the name of dependent whose age is less than 20 years select DEPEND_NAME from DEPENDENT where floor(sysdate-DOB)<20*365 ; V)Find the details of the departments in which the employee ESSN 5078 has worked SELECT DEPT_NO,DEPT_NAME, MANAGER FROM DEPARTMENT WHERE DEPT_NO IN(SELECT DEPT_NO FROM EMPLOYEE WHERE ESSN= 5078 AND SALARY IS NULL)