You are on page 1of 44

# ASSIGNMENTS

DAY-1

## insert data into the tables

Solution:
Day-2
Solution:

//Cdac DATABASE//

1.

/*SELECT *

from course

2.

from course

## WHERE cname <>'dac'; */

3.

/*SELECT rollNo,concat(name, ' ', 'has completed his ', ' ', degree,' ','in year'),year,sex,deptNo,advisor

FROM student

WHERE rollNo>=1;*/

4.

FROM professor

## where phone is null;*/

5.

/*SELECT startYear

FROM professor

## WHERE startYear BETWEEN '2017-01-01' AND '2017-12-31';*/

6.

/*SELECT startYear
FROM professor

WHERE startYear>='2017-01-01'

AND startYear<='2017-12-31';*/

7.

/*SELECT cname

FROM course

WHERE credits='d'

## AND (courseId=1 AND deptNo=1);*/

8.

/*SELECT cname

FROM course

WHERE credits='b'

and deptNo>1;*/

9.

/*SELECT cname

FROM course

WHERE credits='b'

and deptNo=5;*/

10.

/*CREATE VIEW [UNIQUE name] AS //SELECT DISTINCT name ,deptId,hod why not this we can write */

## SELECT DISTINCT name

from department

WHERE deptId>=1;
11.

/*SELECT hod.department

from department

WHERE deptNo<4;

12.

## WHERE deptNo <3;*/

13.

/*SELECT name

FROM student

WHERE sex='female';*/

14.

/*SELECT cname

FROM course;*/

15.

FROM student

WHERE rollNo>=1;*/
Day-3

## job varchar(50) not null,

mgr int(4),

hiredate date,

sal decimal(10,2),

comm decimal(10,2),

## insert into employee values

(7369,'SMITH','CLERK',7902,'93/6/13',800,0.00,20);

## insert into employee values

(7499,'ALLEN','SALESMAN',7698,'98/8/15',1600,300,30);

## insert into employee values

(7521,'WARD','SALESMAN',7698,'96/3/26',1250,500,30);

## insert into employee values

(7566,'JONES','MANAGER',7839,'95/10/31',2975,null,20);

## insert into employee values

(7698,'BLAKE','MANAGER',7839,'92/6/11',2850,null,30);

## insert into employee values

(7782,'CLARK','MANAGER',7839,'93/5/14',2450,null,10);

## insert into employee values

(7788,'SCOTT','ANALYST',7566,'96/3/5',3000,null,20);

## insert into employee values

(7839,'KING','PRESIDENT',null,'90/6/9',5000,0,10);
insert into employee values
(7844,'TURNER','SALESMAN',7698,'95/6/4',1500,0,30);

## insert into employee values

(7900,'JAMES','CLERK',7698,'00/6/23',950,null,30);

## insert into employee values

(7934,'MILLER','CLERK',7782,'00/1/21',1300,null,10);

## insert into employee values

(7902,'FORD','ANALYST',7566,'97/12/5',3000,null,20);

## insert into employee values

(7654,'MARTIN','SALESMAN',7698,'98/12/5',1250,1400,30);

## grade int(4) not null primary key,

losal decimal(10,2),

hisal decimal(10,2));

## insert into salgrade values (5,3001,99999);

commit;

1. Write a query to display the last name, department number, and department name for all employees.

2. Create a unique listing of all jobs that are in department 80. Include the location of the department in
the output.

3. Write a query to display the employee last name, department name, location ID, and city of all
employees who earn a commission.

4. Display the employee last name and department name for all employees who have a (lowercase) in
their last names.

5. Write a query to display the last name, job, department number, and department name for all
employees who work in Toronto.

6. Display the employee last name and employee number along with their manager’s last name and
manager number. Label the columns Employee, Emp#, Manager, and Mgr#, respectively.

7. display all employees including King, who has no manager. Order the results by the employee
number.

8. Create a query that displays employee last names, department numbers, and all the employees who
work in the same department as a given employee. Give each column an appropriate label.

9. Show the structure of the JOB_GRADES table. Create a query that displays the name, job,

## department name, salary, and grade for all employees.

10. Create a query to display the name and hire date of any employee hired after employee Davies.
11. Display the names and hire dates for all employees who were hired before their managers,

along with their manager’s names and hire dates. Label the columns Employee, Emp

1.

or

or

2.

e.deptno=20

3.

## select ename,dname,d.location_id,city from employee e inner join dept d on e.deptno=d.deptno inner

join locations l on d.location_id=l.location_id;

or

## select e.ename,d.dname,d.location_id,l.city from employee e,dept d,locations l where

d.location_id=l.location_id AND e.comm IS NOT NULL

or

## select e.ename,d.dname,d.location_id,l.city from employee e,dept d,locations l where e.comm IS NOT

NULL
4.

select e.ename,d.dname from employee,dept where e.deptno=d.deptno AND ename LIKE %A%;

5.

## select e.ename,e.job,d.dname,d.deptno from locations l,employee e, dept d where e.deptno=d.deptno

AND d.location_id=l.location_id AND l.city='Banglore'

6.

## select e.ename AS 'Employee' ,e.empno AS 'Emp#',d.dname AS 'Department',d.deptno AS 'dept#' FROM

employee e inner join dept d on e.deptno=d.deptno;

or

## select e.ename AS 'Employee' ,e.empno AS 'Emp#',m.empno AS '#man',m.ename AS 'Manager' FROM

employee e inner join employee m on e.empno=m.empno;

or

## select e.ename AS 'Employee' ,e.empno AS 'Emp#',m.empno AS '#man',m.ename AS 'Manager' FROM

employee e Right join employee m on e.mngno=m.empno;

7.

8.

## select e.ename AS 'Employee_name',d.dname AS 'Department_name',d.deptno AS 'Department_no'

from employee e inner join dept d on e.deptno=d.deptno ORDER BY d.deptno;

or

## select e.ename AS 'Employee_name', e.deptno AS 'Department_no' from employee e inner join

employee on e.deptno=c.deptno ORDER BY d.deptno;

or
select e.ename,e.deptno,c.ename,c.deptno

9.

or

or

or

10.

FROM employee e

## JOIN employee SMITH

ON e.empno=SMITH.empno

## AND SMITH.hiredate< e.hiredate;

or
SELECT e.ename, e.hiredate

## FROM employee e where e.hiredate>(select e.hiredate from employee e where e.ename='WARD')

ORDER BY e.hiredate;

or

select e.ename,e.hiredate

## from employee e, employee x

where x.ename='FORD'

and x.hiredate<e.hiredate;

11.

## SELECT e.ename AS 'Employee_name', e.hiredate AS 'EMP_Hired',MANAGER.ename AS

'Manager_name',MANAGER.hiredate AS 'Mang_hired'

FROM employee e

## JOIN employee MANAGER

ON (MANAGER.empno = '7566')

Day-4

## ANS: select max(sal)-min(sal) 'Salary(Highest-Lowest)' from employee;

5.display the manager id & salary of the lower-paid employee that manager.Exclude where mgrno is not
known.

from employee

## where mgr IS NOT NULL

group by mgr

having min(sal)<6000

Day-5

## SUB QUERY ASSIGNMENT

Ques1:Display the name of employee who are getting the salary more than x.

Ans: select ename,job,sal from employee where sal>(select sal from employee where ename='BLAKE');

Ques2: Display the emp list who work in the same dept of x.

ANS2:select ename,job,sal from employee where deptno=(select deptno from employee where
ename='BLAKE');

or

select ename,job,sal from employee where deptno=(select deptno from employee where
ename='BLAKE');

or

select ename,job,sal

from employee

## where sal>(select max(sal) from employee group by deptno );

ques3:Display the emp name & job who work are same job of x.

from employee

## where job=(select job from employee where ename='BLAKE' );

or

select ename,job,sal

from employee

## where job=(select job from employee where ename='BLAKE' )

AND ename<>'BLAKE';
Ques4:Print the details of emp who’s job id is like empid x and salary is greater than equal to salary of
emp id y.

from employee

## where empno='7839' AND sal>=(select sal from employee where empno='7698');

or

select ename,job,sal

from employee

## AND sal>=(select sal from employee where ename='CLARK');

or

select ename,job,sal

from employee

from employee ;

or

select ename,sal

from employee

## where sal=(select min(sal)

from employee)
Ques6:select the name of emp which getting max salary.

from employee ;

or

select ename,sal

from employee

## where sal=(select max(sal)

from employee)

Ques7: select the name of emp which getting 2nd max salary.

## Ques8:Display top five salary.

Ans8. select sal from (select sal from employee) employee LIMIT 5;

or

select sal from (select sal from employee) employee ORDER BY sal LIMIT 5;

Ques9:Display all dept that have min salary greater than the min salary of dept 30.

Ans:9.

select min(sal),deptno

from employee

group by deptno

## having min(sal)>(select min(sal) from employee where deptno=30);

Ques10:Find the job with lowest average salary.

Ans:10.

## (select avg(sal) 'avgsal',job

from employee

group by job) e;

Ques11:Display the name of the employees who earn same salary as the min salary of each dept.

Ques12:Display employee who are not programmer and who’ salary is less than any of the IT prog.

## Ques14:Display employee who do not have subbody’s.

Ques15:Display the detail of the employee who are manager and work in the same department as the
employee with empno=7499 and empno=7521.
Day-6

Assignment on PL/SQL

## Ques1: Write PL/SQL procedure to display the employee list.

Ques2: Write PL/SQL block to display the employee details according to their deptno. Will be a user choice.

Ques3: Write a PL/SQL procedure to display the details of the employee who’s salary is greater than amount
entered by user.

Ques4: Write PL/SQL procedure to display high salary of a particular dept of user choice.

Ques5: Write a PL/SQL procedure to display no. of records or rows entered by the user.

Ques6: Write a PL/SQL procedure to display the no. of employee in a particular job category.

Solution:

/*delimiter \$\$

## create procedure print4(in a int(2),in b int(2),out c int(2))

begin

set c=a+b;

end \$\$

*/

call print4(23,34,@c);

select @c;
/*delimiter \$\$

begin

select max(sal)

## from employee where deptno=d1;

end \$\$

*/

call getdata(10);

/*delimiter \$\$

begin

## select max(sal) into top from employee where deptno=d1;

end \$\$

*/

call getdata3(20,@top)

select @top;

1.

/*delimiter \$\$

begin

end\$\$

*/

call display();

2.

/*delimiter \$\$

begin

select *

## from employee where deptno=d1;

end \$\$

*/

call display1(20);

3.

/*delimiter \$\$

begin

select *

## from employee where sal>salary;

end \$\$

*/

call display2(2000);
4.

/*delimiter \$\$

begin

end \$\$

*/

5.

/*delimiter \$\$

begin

select max(sal)

## from employee where deptno=d1;

end \$\$

*/

call display3a(20);

6.

/*delimiter \$\$

## create procedure display4(in l1 int(2))

begin

select *
from employee LIMIT l1;

end \$\$

*/

call display4(5);

7.

/*delimiter \$\$

## create procedure display5()

begin

select count(ename)

end \$\$

*/

call display5();

8.

/*delimiter \$\$

## create procedure display7(in jobx varchar(50))

begin

select count(ename)

## from employee where job=jobx;

end \$\$

*/

call display7('MANAGER');
Day-7

/*delimiter \$\$

begin

select a;

if mod(a,2) then

else

end if;

end \$\$

*/

call Q1(24);

/*delimiter \$\$

## create procedure Q1a(in a int(10))

begin

select a;

if mod(a,2)=0 then
select "no. is even";

else

end if;

end \$\$

*/

/*delimiter \$\$

begin

select b;

then

else

## select "year is no leap year ";

end if;

end \$\$

*/

Q3: create a procedure to display or find the seniority of the two employee according to their date of
joining.

/*delimiter \$\$

begin

declare h1 date;

declare h2 date;

where empno=e1;

where empno=e2;

if h1>h2 then

else

## select emane from employee where empno=e2;

end if;

end \$\$

*/

Q4: Write PL/SQL procedure that will accept the emp no. and find the salary of that person. If the
salary of that person is less than 1000 then hike 30% ,if the salary is b/w 1001-2000 give him 20% hike
& give 10% if other.

/*delimiter \$\$

begin

## select sal into sal1 from employee where empno=eno;

if sal1<1000

then
update employee set sal=sal1+sal1*0.10 where empno=eno;

then

else

end if;

end \$\$

*/

/*delimiter \$\$

begin

case sal1

else

## select "10% hike in salary";

end case;
end \$\$

*/

Q6:Write PL/SQL block to find the grade of a particular employee if the grade is 1 or A then print
Excellent. 2->very good etc.

/*delimiter \$\$

## create procedure q7(in hisal1 int(4))

begin

declare g int(4);

select hisal1;

if(g=1)

then

select "Excellent";

elseif(g=2)

then

select "Good";

else

select "Average";

end if;

end \$\$

*/
DAY-8 PL/SQL Function And Procedure

/*delimiter \$\$

## create procedure Test1()

begin

declare x int(5);

set x=1;

Repeat

select x;

set x=x+1;

until x>10

end Repeat;

end \$\$

*/

/*delimiter \$\$

## create procedure Test2()

begin

declare x int(5);

set x=1;

while x<5

do

select x;

set x=x+1;
end while;

end \$\$

*/

/*delimiter \$\$

## create procedure Test2a(in y int(5))

begin

declare x int(5);

set x=1;

while (x<=y)

do

select x;

set x=x+1;

end while;

end \$\$

*/

## /*create procedure Test5()

begin
declare fact int(100);

set fact=1;

set i=1;

while i<=3 do

select fact;

select i;

set fact=fact*i;

set i=i+1;

end while;

select fact;
end \$\$

*/

/*delimiter //

returns int(10)

DETERMINISTIC

begin

set fact=1;

set i=1;

while i<=7 do

return fact;

return i;

set fact=fact*i;

set i=i+1;

end while;
return fact;

end //

*/

call Test6(6);

*/

/*

delimiter //

## create function f1()

returns int

DETERMINISTIC
begin

declare x int(10);

set str='';

set x=1;

xyz:loop

if x<=10 then

## set str =concat(str,x,',');

set x=x+1;

iterate xyz;

else
leave xyz;

end if;

end loop;

return str;

end //

*/

/*delimiter //
create function f3b()

returns varchar(20)

DETERMINISTIC

begin

declare x int(10);

set str='';

set x=1;

xyz:loop

if x<=10 then

set x=x+1;

iterate xyz;

else

leave xyz;

end if;

end loop;

return str;

end //

*/
/*delimiter //

## create function f3e()

returns varchar(20)

DETERMINISTIC

begin

declare x int(5);

set str='';

set str2='';

set str1='A';

set x=1;

xyz:loop

if x<=10 then

set x=x+1;

iterate xyz;

else
leave xyz;

end if;

end loop;

return str2;

end //

*/

select f3e();