Abir Chakraborty - 22312204
Abir Chakraborty - 22312204
Abir Chakraborty - 22312204
ROLL NO:-11
PAPER CODE:-CMSPAEC01P
Assignment No-: 01 (Basic SQL)
i. Write SQL query to select all employee whose salary is greater than 12000.
(sql query) :
Alter the table employee adds a new column category (char (1)).
(sql query) :
vii. Alter the table employee increase the width of the column empno to 8. (sql
query) :
ALTER TABLE EMPLOYEE_DATA MODIFY empno VARCHAR(8); viii.
x. Create a read only view that will show empno, empname, address, salary
(sql query) :
xi. Write a SQL Query to find the Average and Minimum and Sum of Salary of
the Employee.
(sql query) :
xii. Write a SQL Query to find the Maximum Salary of the employees and it will
display Column asMaximum Salary.
(sql query) :
xiv. Write a SQL Query which will show all employees (the empno, empname,
Address) whose Salary is greater than the average Salary of the employee.
(sql query) :
xv. Write a SQL Query to count the number of employee whose salary is greater
than Rs.10000.
(sql query) :
xvi. Write a SQL Query to find the names of all employees whose name start with
‘S’and ends with‘n’.
(sql query) :
SELECT NOW();
xix. Write a SQL Query to list all employees in ascending order of their salary.
(sql query) :
xxi. Write a SQL Query to find all employee names in whose name there are two
Consecutive ‘o’.
(sql query) :
xxii. Write a SQL Query to find sum of salary of employee grouped by category with
a minimum sum of 30000.
(sql query) :
SELECT
CASE
WHEN salary > 20000 THEN 'A'
WHEN salary >= 10000 AND salary <= 20000 THEN 'B'
WHEN salary >= 7000 AND salary < 10000 THEN 'C'
ELSE 'D'
END AS category,
SUM(salary) AS total_salary
FROMEMPLOYEE_DATA
GROUP BY category
HAVING SUM(salary) >= 30000; xxiii. Write a SQL Query to show the list of
employee whose salary is between 7000 and 15000.
(sql query) :
xxv. Write a SQL Query to show the fields of employee separated by ‘,’.
(sql query) :
SELECT CONCAT(empno, ',', empname, ',', address, ',', dob, ',', doj, ',', salary) AS
employee_fields FROMEMPLOYEE_DATA;
xxvi. Write a SQL Query to show employee list where empname=B.Jana and
dob=07/05/73 will be displayed as ‘B. Jana born on seventh May 1973’.
(sql query) :
xxvii. Create a table empp with same attributes length as employee and copy
thecontent of employee into empp.
(sql query) :
EMPLOYEES
DEPARTMENTS
i. List the names of analysts and salesmen.
ii. List details of employees who have joined before 30 Sep 81.
vi. List employee names for those who have joined between 30 June and 31 Dec. ‘81.
(sql query)SELECT ENAME FROM EMPLOYEES WHERE HIREDATE BETWEEN '1981-
06-30' AND '1981-12-31';
viii. List the names of employees who are not eligible for commission.
(sql query)
SELECT ENAME FROM EMPLOYEES WHERE COMM IS NULL;
ix. List the name and designation of the employee who does not report to anybody.
(sql query)
SELECT ENAME, JOB FROM EMPLOYEES WHERE MGR IS NULL;
(sql query)
SELECT ENAME FROM EMPLOYEES WHERE COMM IS NOT NULL;
xii. List employee’s hose names either start or end with “S”.
(sql query)
SELECT ENAME FROM EMPLOYEES WHERE ENAME LIKE 'S%' OR ENAME
LIKE '%S';
xiii. List names of employees whose names have “i” as the second character.
(sql query)
SELECT ENAME FROM EMPLOYEES WHERE ENAME LIKE '_i%'; xiv.
(sql query)
SELECT COUNT(*) FROM EMPLOYEES;
(sql query)
SELECT SUM(SAL) FROM EMPLOYEES;
xvii. List the maximum, minimum and average salary in the company.
(sql query)
SELECT MAX(SAL), MIN(SAL), AVG(SAL) FROM EMPLOYEES; xviii.
(sql query)
SELECT MAX(SAL) FROM EMPLOYEES WHERE JOB = 'Salesman';
3. A. Write a PL/SQL that will take a number as input and calculate sum of its digits .
[PL/SQL]:-
Drop procedure sumofdigits; Delimiter
//
create procedure sumofdigits(in n int(4))
begin declare r integer; declare s
integer; set @s = 0; while @n<>0 do set
@r=@n%10; set @s=@s+@r;
set@n=@n div 10; end while; select
@s; end // delimiter ; set @n=12345;
call sumofdigits(@n);
[OUTPUT]:-
3.B. Write a PL/SQL that will generate the natural series from 1 to 10.
[PL/SQL-1]:-
Drop procedure Natural_number; Delimiter
//
create procedure Natural_number() begin
Declare n INT;
Declare str VARCHAR(255);
set n=1; set str='';
label1: Loop
IF n>10 THEN
LEAVE label1;
END IF;
set str= CONCAT(str,n,','); set
n=n+1;
END LOOP;
SELECT str;
END //
Delimiter ; call
Natural_number;
[OUTPUT 1]:-
[PL/SQL-2]:-
Drop procedure Natural_number;
Delimiter // create procedure
Natural_number () begin declare n
integer; declare str
VARCHAR(255);
set str = ''; set n=0; while
n<10 do set n= n+1; set
str=CONCAT(str,n,',');
end while; select str; end
// Delimiter ; call
Natural_number();
[OUTPUT 2]:-
3.C. Write a PL/SQL that will generate the Fibonacci series upto7 terms.
[PL/SQL]:-
Drop Procedure Fibo;
Delimiter //
Create Procedure Fibo()
begin declare f INT; declare
f1 INT; declare f2 INT;
declare n INT; declare str
VARCHAR(255); set f1=0;
set f2=1; set n=0; set
str='0,1,';
while n<5 do set f=f1+f2;
set f1=f2; set f2=f; set
str=CONCAT(str,f,',');
set n=n+1; end while;
select str; end // Delimiter
; call Fibo();
[OUTPUT]:-
3.D. Write a PL/SQL that will accept a string and reverse it.
[PL/SQL]:-
Drop procedure rString; Delimiter
//
create procedure rString (in str varchar(20), inout rstr
varchar(20)) begin
set rstr = reverse(@str); end
//
delimiter ; set
@str='Abir
chakraborty'; set
@rstr='';
call rString(@str,@rstr); select
@rstr;
[OUTPUT]:-
3. E. WAP which accepts the student rollno and returns the highest percent
and name of that student to thecalling block.
STUDENT(Stud_ID,Stud_name,percent);
[PL/SQL]:-
create table student (studId integer primary key,studName varchar(25),percent
decimal);
insert into student values(1,'Abir roy',65.25);
insert into student values(2,'Sushant',72.85); insert
into student values(3,'Dikvijay',82.74);
select * from student; Drop
procedure findStudent;
Delimiter $$
create procedure findStudent (in roll int(4), out sname varchar(25),out per decimal)
begin select studName into sname from student where studId=roll; select percent
into per from student where studId=roll; end $$ delimiter ; set @r=2; set @sn=' ';
set @p=1.0;
call findStudent(@r,@sn,@p); select
@sn,@p;
[OUTPUT]:-
3.F. Write a procedure which accepts the empno and returns the associated
empname. If empno does notexist than give proper error message.
EMP(Empno, Empname).
[PL/SQL]:-
Drop table emp;
create table emp(empno int ,empname varchar(35));
insert into emp values(01,'Abir roy'),(02,'Ram Prasad'),(03,'Ujjal Karmakar'),(04,'shyam
Roy');
[OUTPUT- 1]:-
[OUTPUT- 2]:-
4.A. Create a cursor for the emp table. Produce the output in following format:
{empname} employee working in department {deptno} earns Rs. {salary}.
EMP(empno, empname, salary, deptno);
[PL/SQL]:-
Drop table emp;
create table emp(eno int primary key,ename varchar(35),salary decimal(10,2),deptno int);
call showRecord();
[OUTPUT]:-
4.B. Create a cursor for updating the salary of emp working in deptno 10 by
20%. If any rows are affected than display the no of rows affected. Use implicit
cursor.
[PL/SQL]:- select
* from emp;
Drop procedure salaryUpdate(); delimiter
//
create procedure salaryUpdate() begin
declare count int;
declare id int; declare sal
decimal(10,2); declare
finished int default 0;
declare emp_cursor cursor for select eno,salary from emp where deptno=10;
declare continue handler for not found set finished=1; open emp_cursor; set
count =0; get_record: loop fetch emp_cursor into id,sal;
if finished=1 then LEAVE
get_record;
end if;
update emp set salary=salary*1.2 where eno=id;
set count=count+1; end loop; close emp_cursor;
select count; end // delimiter ; call
salaryUpdate();
select * from emp;
[OUTPUT]:-
Before Update
After Update
5.A. Write a Trigger that stores the old data table of student table in
student_backup while updating thestudent table.Student_backup (Stud_ID,
Stud_name, Address, Contact_no, Branch, Operation_date)
Student (Stud_ID, Stud_name, Address, Contact_no, Branch)
[PL/SQL]:-
[OUTPUT]:-
5.B. Write a trigger which checks the age of employee while inserting the
record in emp table. If it is negativethan generate the error and display proper
message.
[PL/SQL]:-
create table employee(empId integer primary key,empName varchar(25),empAge
integer);
insert into employee values(1,'gk',25);
select * from employee; drop trigger
checkTrigger; delimiter // create
trigger checkTrigger before insert on
employee for each row begin declare
msg varchar(50); if NEW.empAge <
0 then
set NEW.empAge = 0;
end if; end
//
delimiter ;
insert into employee values(3,'cd',-5);
select * from employee; [OUTPUT]:-
Filename: DBMS ASSIGNMENT2
Directory: C:\Users\Abir chakraborty\Documents
Template: C:\Users\Abir
chakraborty\AppData\Roaming\Microsoft\Templates\Normal.dotm
Title:
Subject:
Author: Deep Das
Keywords:
Comments:
Creation Date: 22-02-2023 00:09:00
Change Number: 1
Last Saved On: 22-02-2023 00:09:00
Last Saved By:
Total Editing Time: 28 Minutes
Last Printed On: 22-02-2023 00:42:00
As of Last Complete Printing
Number of Pages: 25
Number of Words: 2,398 (approx.)
Number of Characters: 13,673 (approx.)