Professional Documents
Culture Documents
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)
);
/*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)
);
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
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
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';
/*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);
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;
/*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;
/*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';
/*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;
/*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;
/*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;
/*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';