You are on page 1of 16

WEST BENGAL STATE UNIVERSITY

BSc. COMPUTER SCIENCE HONORS


(SEMESTER 4)

Practical Assignment

Code: Core-CMSACOR10P
NAME: Subhamoy Ghosh
ROLL: 6035
SYLLABUS: DBMS
CREATE database assignment2;
USE assignment2;

/*Creating department*/
CREATE TABLE department
(
Dno int NOT NULL,
Dname varchar(50) DEFAULT NULL,
Location varchar(50) DEFAULT 'New Delhi',
PRIMARY KEY (Dno)
);

/*Inserting Data into department*/


INSERT INTO department VALUES
(1,'Accounting','Chennai'),
(2,'Research','Howrah'),
(3,'Sales','Mumbai'),
(4,'Programming','Kolkata'),
(5,'Marketing','New Delhi');

/*Creating Employee*/
CREATE TABLE employee
(
Eno char(3) NOT NULL,
Ename varchar(50) NOT NULL,
Job_type varchar(50) NOT NULL,
Manager char(3) DEFAULT NULL,
Hire_date date NOT NULL,
Dno int DEFAULT NULL,
Commission decimal(10,2) DEFAULT NULL,
Salary decimal(7,2) NOT NULL,
PRIMARY KEY (Eno),
CONSTRAINT Dno FOREIGN KEY (Dno) REFERENCES department (Dno),
CONSTRAINT Manager FOREIGN KEY (Manager) REFERENCES employee (Eno)
);

/*Inserting Data into employee*/


insert into employee values
('83','Subhamoy','President',NULL,'1981-11-01',1,0.00,2950.00),
('56','Rahul','Manager','83','1981-04-02',2,0.00,2300.00),
('69','Ronit','Manager','83','1981-05-01',5,0.00,280.00),
('84','Anshu','Clerk','83','1983-01-12',2,0.00,1150.00),
('88','Swastik','Analyst','56','1982-12-09',2,0.00,2850.00),
('90','Wribhu','Clerk','69','1981-12-03',3,0.00,950.00),
('82','Sandip','Manager','83','1981-06-09',1,0.00,2900.00),
('65','Amit','Sales_man','84','1981-04-22',3,1400.00,1250.00),
('52','Aritro','Sales_man','69','1981-02-22',3,500.00,1300.00),
('49','Poulastaa','Sales_man','69','1981-02-20',3,300.00,2000.00),
('36','Sonu','Clerk','90','1981-12-01',4,0.00,1000.00);
/*1. Query to display Employee Name, Job, Hire Date, Employee Number; for each employee with
the Employee Number appearing first.*/
SELECT Eno, Ename, Job_type, Hire_date FROM employee;

Hire_dat
Eno Ename Job_type
e
12/1/198
36 Sonu Clerk
1
Sales_ma 2/20/198
49 Poulastaa
n 1
Sales_ma 2/22/198
52 Aritro
n 1
56 Rahul Manager 4/2/1981
Sales_ma 4/22/198
65 Amit
n 1
69 Ronit Manager 5/1/1981
82 Sandip Manager 6/9/1981
Subhamo 11/1/198
83 President
y 1
1/12/198
84 Anshu Clerk
3
12/9/198
88 Swastik Analyst
2
12/3/198
90 Wribhu Clerk
1

/*2. Query to display unique Jobs from the Employee Table.*/


SELECT DISTINCT Job_type FROM employee;

Job_type
Clerk
Sales_ma
n
Manager
President
Analyst

/*3. Query to display the Employee Name concatenated by a Job separated by a comma.*/
SELECT CONCAT(Ename, ',', Job_type) AS Name_Job FROM employee;

Name_Job
Sonu,Clerk
Poulastaa,Sal
es_man
Aritro,Sales_
man
Rahul,Manag
er
Amit,Sales_
man
Ronit,Manag
er
Sandip,Mana
ger
Subhamoy,Pr
esident
Anshu,Clerk
Swastik,Anal
yst
Wribhu,Clerk

/*4. Query to display all the data from the Employee Table. Separate each Column by a comma and
name the said column as THE_OUTPUT.*/
SELECT CONCAT(Eno , ', ', Ename, ',', Job_type, ', ',Manager, ',' ,Hire_date, ',' ,Dno, ',' ,Commission,
',' ,Salary) AS THE_OUTPUT FROM employee;

THE_OUT
PUT
36,
Sonu,Cler
k,
90,1981-
12-
01,4,0.00
,1000.00
49,
Poulasta
a,Sales_
man,
69,1981-
02-
20,3,300.
00,2000.
00
52,
Aritro,Sal
es_man,
69,1981-
02-
22,3,500.
00,1300.
00
56,
Rahul,Ma
nager,
83,1981-
04-
02,2,0.00
,2300.00
65,
Amit,Sale
s_man,
84,1981-
04-
22,3,140
0.00,125
0.00
69,
Ronit,Ma
nager,
83,1981-
05-
01,5,0.00
,280.00
82,
Sandip,M
anager,
83,1981-
06-
09,1,0.00
,2900.00
NULL
84,
Anshu,Cl
erk,
83,1983-
01-
12,2,0.00
,1150.00
88,
Swastik,A
nalyst,
56,1982-
12-
09,2,0.00
,2850.00
90,
Wribhu,C
lerk,
69,1981-
12-
03,3,0.00
,950.00

/*5. Query to display the Employee Name and Salary of all the employees earning more than
$2850.*/
SELECT Ename, Salary FROM employee WHERE ( Salary + Commission ) > 2850;

Ename Salary
Sandip 2900
Subhamo
2950
y

/*6. Query to display Employee Name and Department Number for the Employee No= 790.*/
SELECT Ename,Dno FROM employee WHERE Eno='790';

Ename Dno

/*7. Query to display Employee Name and Salary for all employees whose salary is not in the range
of $1500 and $2850.*/
SELECT Ename,Salary FROM employee WHERE Salary NOT BETWEEN 1500 AND 2850;

Ename Salary
Sonu 1000
Aritro 1300
Amit 1250
Ronit 280
Sandip 2900
Subhamo 2950
y
Anshu 1150
Wribhu 950

/*8. Query to display Employee Name and Department No. Of all the employees in Dept 10 and
Dept 30 in the alphabetical order by name.*/
SELECT Ename,Dno FROM employee WHERE Dno=10 OR DNO=30 ORDER BY Ename;

Ename Dno

/*9. Query to display Name and Hire Date of every Employee who was hired in 1981.*/
SELECT Ename,Hire_date FROM EMPLOYEE WHERE Hire_date LIKE '1981%';

Ename Hire_date
Sonu 12/1/1981
Poulastaa 2/20/1981
Aritro 2/22/1981
Rahul 4/2/1981
Amit 4/22/1981
Ronit 5/1/1981
Sandip 6/9/1981
Subhamoy 11/1/1981
Wribhu 12/3/1981

/*10. Query to display Name and Job of all employees who don’t have a current Manager.*/
SELECT Ename,Job_type FROM employee WHERE Manager IS NULL;

Ename Job_type
Subhamo Presiden
y t

/*11. Query to display the Name, Salary and Commission for all the employees who earn
commission.*/
SELECT Ename,Salary,Commission FROM employee WHERE Commission > 0.00 ;

Commissio
Ename Salary
n
Poulasta
2000 300
a
Aritro 1300 500
Amit 1250 1400

/*12.Sort the data in descending order of Salary and Commission.*/


SELECT Ename,Salary,Commission FROM employee WHERE Commission > 0.00 ORDER BY Salary
DESC,Commission DESC;

Commissio
Ename Salary
n
Poulasta
2000 300
a
Aritro 1300 500
Amit 1250 1400

/*13. Query to display Name of all the employees where the third letter of their name is ‘A’.*/
SELECT Ename FROM employee WHERE Ename LIKE '__A%';

Ename
Swastik

/*14. Query to display Name of all employees either have two ‘R’s or have two ‘A’s in their name
and are either in Dept No = 30 or their Manger’s Employee No = 778.*/
SELECT Ename,Dno,Manager FROM employee WHERE Ename LIKE '%A%A%' OR Ename LIKE '%R%R%'
AND Dno=30 OR Manager='778';

Ename Dno Manager


Poulasta
3 69
a

/*15. Query to display Name, Salary and Commission for all employees whose Commission Amount
is greater than their Salary increased by 5%.*/
SELECT Ename,Salary,Commission FROM employee WHERE Commission > (Salary+Salary*0.05);

Ename Salary Commissio


n
Amit 1250 1400

/*16. Query to display the Current Date.*/


SELECT CURDATE();

CURDATE()
6/12/2023

/*17. Query to display Name, Hire Date and Salary Review Date which is the 1st Monday after six
months of employment.*/
SELECT Ename,Hire_date,date_add(date_add(Hire_date,INTERVAL 6 MONTH),INTERVAL (7-
WEEKDAY(date_add(Hire_date,INTERVAL 6 MONTH))) DAY) AS REVIEW_DATE FROM employee;

Hire_da REVIEW_D
Ename
te ATE
12/1/19
Sonu 6/7/1982
81
Poulast 2/20/19
8/24/1981
aa 81
2/22/19
Aritro 8/24/1981
81
4/2/198
Rahul 10/5/1981
1
4/22/19
Amit 10/26/1981
81
5/1/198
Ronit 11/2/1981
1
6/9/198
Sandip 12/14/1981
1
Subham 11/1/19
5/3/1982
oy 81
1/12/19
Anshu 7/18/1983
83
12/9/19
Swastik 6/13/1983
82
12/3/19
Wribhu 6/7/1982
81

/*18. Query to display Name and calculate the number of months between today and the date each
employee was hired.*/
SELECT Ename,12 * (YEAR(curdate())-YEAR(Hire_date)) + (MONTH(CURDATE())-MONTH(Hire_date))
AS MONTHS FROM employee;

Ename MONTHS
Sonu 498
Poulastaa 508
Aritro 508
Rahul 506
Amit 506
Ronit 505
Sandip 504
Subhamo
499
y
Anshu 485
Swastik 486
Wribhu 498

/*19. Query to display the following for each employee:- <E-Name> earns < Salary> monthly but
wants < 3 * Current Salary >. Label the Column as Dream Salary.*/
SELECT CONCAT(Ename,' earns ',Salary,' monthly but wants ',3*Salary) AS DREAMY_SALARY FROM
employee;

DREAMY
_SALARY
Sonu
earns
1000.00
monthly
but
wants
3000.00
Poulasta
a earns
2000.00
monthly
but
wants
6000.00
Aritro
earns
1300.00
monthly
but
wants
3900.00
Rahul
earns
2300.00
monthly
but
wants
6900.00
Amit
earns
1250.00
monthly
but
wants
3750.00
Ronit
earns
280.00
monthly
but
wants
840.00
Sandip
earns
2900.00
monthly
but
wants
8700.00
Subhamo
y earns
2950.00
monthly
but
wants
8850.00
Anshu
earns
1150.00
monthly
but
wants
3450.00
Swastik
earns
2850.00
monthly
but
wants
8550.00
Wribhu
earns
950.00
monthly
but
wants
2850.00

/*20. Query to display Name with the 1st letter capitalized and all other letter lower case and
length of their name of all the employees whose name starts with ‘J’, ’A’ and ‘M’.*/
SELECT CONCAT( UPPER(SUBSTRING(Ename,1,1)) , LOWER(SUBSTRING(Ename,2,50))) AS
NAME,LENGTH(Ename) AS LENGTH FROM employee WHERE Ename LIKE 'J%' OR Ename LIKE 'A%' OR
Ename LIKE 'M%';

NAME LENGTH
Aritro 6
Amit 4
Anshu 5

/*21. Query to display Name, Hire Date and Day of the week on which the employee started.*/
SELECT Ename, Hire_date, DAYNAME(Hire_date) AS WEEK_DAY FROM employee;

Ename Hire_date WEEK_DAY


Sonu 12/1/1981 Tuesday
Poulastaa 2/20/1981 Friday
Aritro 2/22/1981 Sunday
Rahul 4/2/1981 Thursday
Amit 4/22/1981 Wednesday
Ronit 5/1/1981 Friday
Sandip 6/9/1981 Tuesday
Subhamoy 11/1/1981 Sunday
Anshu 1/12/1983 Wednesday
Swastik 12/9/1982 Thursday
Wribhu 12/3/1981 Thursday

/*22. Query to display Name, Department Name and Department No for all the employees.*/
SELECT e.Ename,d.Dname,e.Dno FROM employee AS e,department AS d WHERE e.Dno=d.Dno;

Ename Dname Dno


Sandip Accounting 1
Subham
Accounting 1
oy
Rahul Research 2
Anshu Research 2
Swastik Research 2
Poulasta
Sales 3
a
Aritro Sales 3
Amit Sales 3
Wribhu Sales 3
Programmi
Sonu 4
ng
Ronit Marketing 5

/*23. Query to display Unique Listing of all Jobs that are in Department # 30.*/
SELECT DISTINCT Job_type FROM employee WHERE Dno=30;

Job_type

/*24. Query to display Name, Dept Name of all employees who have an ‘A’ in their name.*/
SELECT e.Ename,d.Dname FROM employee AS e,department as d WHERE e.Ename LIKE '%A%' AND
e.Dno=d.Dno;

Ename Dname
Poulasta
Sales
a
Aritro Sales
Rahul Research
Amit Sales
Accounti
Sandip
ng
Subham Accounti
oy ng
Anshu Research
Swastik Research

/*25. Query to display Name, Job, Department No. And Department Name for all the employees
working at the Dallas location.*/
SELECT e.Ename,e.Job_type,e.Dno,d.Dname FROM employee AS e,department as d WHERE
e.Dno=d.Dno AND d.Location='Dallas';

Ename Job_type Dno Dname

/*26. Query to display Name and Employee no. Along with their Manger’s Name and the Manager’s
employee no; along with the Employees’ Name who do not have a Manager.*/
SELECT e.Ename,e.Eno,d.Ename,d.Eno FROM employee AS e LEFT OUTER JOIN employee as d ON
e.Eno=d.Manager;

Ename Eno Ename Eno


Sonu 36 NULL NULL
Poulastaa 49 NULL NULL
Aritro 52 NULL NULL
Rahul 56 Swastik 88
Amit 65 NULL NULL
Poulasta
Ronit 69 49
a
Ronit 69 Aritro 52
Ronit 69 Wribhu 90
Sandip 82 NULL NULL
Subhamo
83 Rahul 56
y
Subhamo
83 Ronit 69
y
Subhamo
83 Sandip 82
y
Subhamo
83 Anshu 84
y
Anshu 84 Amit 65
Swastik 88 NULL NULL
Wribhu 90 Sonu 36
/*27. Query to display Name, Dept No. And Salary of any employee whose department No. And
salary matches both the department no. And the salary of any employee who earns a
commission.*/
SELECT Ename,Dno,Salary FROM employee WHERE (Dno,Salary) IN (SELECT Dno,Salary FROM
employee WHERE Commission>0);

Ename Dno Salary


Poulasta
3 2000
a
Aritro 3 1300
Amit 3 1250

/*28. Query to display Name and Salaries represented by asterisks, where each asterisk (*) signifies
$100.*/
SELECT Ename,REPEAT ('*',(Salary/100)) AS SALARY_IN_STAR FROM employee;

En
SALARY_IN_ST
am
AR
e
So
**********
nu
Po
ula ************
sta ********
a
Ari ************
tro *
Ra ************
hul ***********
Am ************
it *
Ro
***
nit
Sa ************
ndi ************
p *****
Su
************
bh
************
am
******
oy
An
************
shu
Sw ************
asti ************
k *****
Wr **********
ibh
u

/*29. Query to display the Highest, Lowest, Sum and Average Salaries of all the employees*/
SELECT MAX(Salary),MIN(Salary),SUM(Salary),AVG(Salary) FROM employee;

MAX(Sala MIN(Sala SUM(Sala AVG(Salar


ry) ry) ry) y)
1720.909
2950 280 18930
091

/*30. Query to display the number of employees performing the same Job type functions.*/
SELECT job_type,COUNT(*) FROM employee GROUP BY Job_type;

COUNT(*
job_type
)
Clerk 3
Sales_ma
3
n
Manager 3
President 1
Analyst 1

/*31. Query to display the no. Of managers without listing their names.*/
SELECT COUNT(DISTINCT Manager) FROM employee;

COUNT(DISTINCT Manager)
5

/*32. Query to display the Department Name, Location Name, No. Of Employees and the average
salary for all employees in that department.*/
SELECT department.Dname,department.Location,AVG(employee.Salary),COUNT(*) FROM
employee,department WHERE department.Dno=employee.Dno GROUP BY department.Dname;

Locati AVG(employee. COUN


Dname
on Salary) T(*)
Accounti Chenn
2925 2
ng ai
Howra
Research 2100 3
h
Mumb
Sales 1375 4
ai
Program Kolkat
1000 1
ming a
Marketin New
280 1
g Delhi

/*33. Query to display Name and Hire Date for all employees in the same dept. As Blake.*/
SELECT Ename,Hire_date FROM employee WHERE Dno=(SELECT Dno FROM employee WHERE
Ename='Blake');

Hire_dat
Ename
e

/*34. Query to display the Employee No. And Name for all employees who earn more than the
average salary.*/
SELECT Eno,Ename FROM employee WHERE Salary > (Select AVG(Salary) FROM employee);

Eno Ename
49 Poulastaa
56 Rahul
82 Sandip
Subhamo
83
y
88 Swastik

/*35. Query to display Employee Number and Name for all employees who work in a department
with any employee whose name contains a ‘T’.*/
SELECT e.Eno,e.Ename FROM employee AS e ,employee as d WHERE e.Manager=d.Eno AND d.Ename
LIKE '%T%';

Eno Ename
Poulasta
49
a
52 Aritro
90 Wribhu

/*36. Query to display the names and salaries of all employees who report to King.*/
SELECT Ename,Salary FROM employee WHERE Manager=(SELECT Eno FROM employee WHERE
Ename='King');

Ename Salary

/*37. Query to display the department no, name and job for all employees in the Sales
department.*/
SELECT e.Dno,e.Ename,e.Job_type FROM employee AS e,department as d WHERE d.Dno=e.Dno AND
d.Dname='Sales';

Dno Ename Job_type


Poulasta Sales_ma
3
a n
Sales_ma
3 Aritro
n
Sales_ma
3 Amit
n
3 Wribhu Clerk
/*Changing Default sql_mode to use group by.*/
SELECT @@sql_mode;
set sql_mode = '';

You might also like