Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 12

IT22312 – DATABASE CONCEPTS LABORATORY

EX.NO: 3
DATE:
Queries on Joins and Nested Queries

AIM:

QUERIES:
1. Retrieve SSN and project location for all employees.
mysql> select ssn,plocation from employee_45,project_45 where dnum=dno;
+-----------+-----------+
| ssn | plocation |
+-----------+-----------+
| 123456789 | bellaire |
| 333445555 | bellaire |
| 453453453 | bellaire |
| 666884444 | bellaire |
| 123456789 | sugarland |
| 333445555 | sugarland |
| 453453453 | sugarland |
| 666884444 | sugarland |
| 123456789 | houston |
| 333445555 | houston |
| 453453453 | houston |
| 666884444 | houston |
| 987654321 | stafford |
| 987987987 | stafford |
| 999887777 | stafford |
| 888665555 | houston |
| 987654321 | stafford |
| 987987987 | stafford |
| 999887777 | stafford |
+-----------+-----------+

2. Retrieve the name and address of all employees who work for the 'Research'
department.
mysql> select e.fname,e.address from employee_45 e JOIN department_45 d ON
e.dno=d.dnumber where dname='reserch';

+----------+------------------------+
| fname | address |
+----------+------------------------+
| john | 731 fondren,houston,tx |
| franklin | 638 voss,houston,tx |
| joyce | 5631 rice,houston,tx |
| ramesh | 975 fire oak,humble,tx |
+----------+------------------------+

ROLL NO:2127220801045 PAGE NO:1


3. Retrieve the name and ssn of all employees who have a daughter.
mysql> select fname,ssn from employee_45,dependent_45 where essn=ssn and
relationship='daughter';
+----------+-----------+
| fname | ssn |
+----------+-----------+
| john | 123456789 |
| franklin | 333445555 |
+----------+-----------+

4. For every project located in 'Stafford', list the project number, the controlling
department number, and the department manager's last name, address, and
birthdate.
mysql> select p.pnumber,p.dnum,e.lname,e.address,e.bdate from employee_45 e,
department_45 d,project_45 p where plocation='stafford'and dnumber=dnum and
d.mgr_ssn=e.ssn;
+---------+------+---------+-----------------------+------------+
| pnumber | dnum | lname | address | bdate |
+---------+------+---------+-----------------------+------------+
| 10 | 4 | wallace | 291 berry,bellaire,tx | 1941-06-20 |
| 30 | 4 | wallace | 291 berry,bellaire,tx | 1941-06-20 |
+---------+------+---------+-----------------------+------------+

5. For each employee, retrieve the employee's name, and the name of his or her
immediate supervisor.
mysql> select e.fname as emp,s.fname as supervisor from employee_45 as e,
employee_45 as s where (e.super_ssn=s.ssn);
+----------+------------+
| emp | supervisor |
+----------+------------+
| john | franklin |
| franklin | james |
| joyce | franklin |
| ramesh | franklin |
| jennifer | james |
| ahmad | jennifer |
| alicia | jennifer |
+----------+------------+

6. Increase the salary of all employees working on the 'ProductX' project for more than
30 hours by 15% .
mysql> update employee_45 as e set salary=1.15*salary where ssn in (select ssn from
works_on_45 as w,project_45 as p where w.essn=e.ssn and w.pno=p.pnumber
and pname='productX' and hours>30);
Query OK , 1 row affected (0.29 sec)
Rows matched: 1 Changed: 1 Warnings: 0

mysql>SELECT * FROM employee_45 WHERE ssn IN (SELECT w.essn FROM works_on_45 AS w


JOIN project_45 AS p ON w.pno = p.pnumber WHERE p.pname = 'ProductX' AND w.hours >
30);
+-------+-------+-------+-----------+------------+--------------------------+------+----------+-----------+-----+
| FNAME | MINIT | LNAME | SSN | BDATE | address | SEX | SALARY | SUPER_SSN | DNO |
+-------+-------+-------+-----------+------------+--------------------------+------+----------+-----------+-----+
| John | B | Smith | 123456789 | 1965-01-09 | 731 Fondren, Houston, TX | M | 39675.00 | 333445555 | 5 |
+-------+-------+-------+-----------+------------+--------------------------+------+----------+-----------+-----+
1 row in set (0.00 sec)

ROLL NO:2127220801045 PAGE NO:2


7. Retrieve a list of employees and the project name each works in, ordered by the
employee's department, and within each department ordered alphabetically by
employee first name.
mysql> select fname,pname,dno from employee_45 as e,works_on_45 as w,project_45 as
p where e.ssn=w.essn and w.pno=p.pnumber order by dno,fname;
+----------+-----------------+-----+
| fname | pname | dno |
+----------+-----------------+-----+
| ahmad | computerization | 4 |
| ahmad | newbenefits | 4 |
| alicia | computerization | 4 |
| alicia | newbenefits | 4 |
| jennifer | reorganization | 4 |
| jennifer | newbenefits | 4 |
| franklin | computerization | 5 |
| franklin | reorganization | 5 |
| franklin | productY | 5 |
| franklin | productZ | 5 |
| john | productX | 5 |
| john | productY | 5 |
| joyce | productX | 5 |
| joyce | productY | 5 |
| ramesh | productZ | 5 |
+----------+-----------------+-----+

8. Retrieve the names of department which has project carried out in multiple
locations.
mysql> select dname from department_45 as d,project_45 where dnumber=dnum group by
dnum having count(plocation)>1;
+----------------+
| dname |
+----------------+
| administration |
| research |
+----------------+

9. Retrieve the name and ssn of employees who has dependent with same name and
gender.
mysql> SELECT distinct E.Fname, E.Lname, E.Ssn FROM EMPLOYEE_45 e JOIN
DEPENDENT_45 D1 ON E.Ssn = D1.Essn JOIN DEPENDENT_45 D2 ON E.Ssn = D2.Essn WHERE
D1.Dependent_name = D2.Dependent_name AND D1.Sex = D2.Sex;
+----------+---------+-----------+
| Fname | Lname | Ssn |
+----------+---------+-----------+
| John | Smith | 123456789 |
| Franklin | Wong | 333445555 |
| jennifer | Wallace | 987654321 |
+----------+---------+-----------+
3 rows in set (0.00 sec)
10.Find the names of employees joined prior to their supervisor.
mysql> ALTER TABLE employee_45 ADD COLUMN Join_Date DATE;

mysql> UPDATE employee_45 set join_date='1989-09-23' where dno=4;


Query OK, 0 rows affected (0.02 sec)
Rows matched: 3 Changed: 0 Warnings: 0

ROLL NO:2127220801045 PAGE NO:3


mysql> UPDATE employee_45 set join_date='1999-10-13' where dno=5;
Query OK, 4 rows affected (0.01 sec)
Rows matched: 4 Changed: 4 Warnings: 0
mysql> UPDATE employee_45 set join_date='2001-01-30' where dno=1;
Query OK, 1 row affected (0.02 sec)
Rows matched: 1 Changed: 1 Warnings:0
mysql> SELECT e.Fname, e.Lname, e.Join_Date AS Employee_Join_Date, d.Mgr_start_date
AS Supervisor_Join_Date FROM employee_45 e JOIN department_45 d ON e.Dno =
d.Dnumber WHERE e.Join_Date < d.Mgr_start_date;

+----------+---------+--------------------+----------------------+
| Fname | Lname | Employee_Join_Date | Supervisor_Join_Date |
+----------+---------+--------------------+----------------------+
| jennifer | Wallace | 1989-09-23 | 1995-01-01 |
| ahmed | jabber | 1989-09-23 | 1995-01-01 |
| alicia | zelya | 1989-09-23 | 1995-01-01 |
+----------+---------+--------------------+----------------------+

11.Find the name of the oldest and youngest manager of the company.
mysql> select max(bdate) as oldest,min(bdate) as youngest from employee_45 where
ssn in (select mgr_ssn from department_45);

+------------+------------+
| oldest | youngest |
+------------+------------+
| 1955-12-08 | 1937-11-10 |
+------------+------------+

12.Find name of employee whose experience is greater than all employee of the
company.

13.Find name of employee whose experience is less than some employee of the
department 2.
ROLL NO:2127220801045 PAGE NO:4
In employee_45 table dno contains 1,4 and 5. dno=2 is invalid so ,it is
considered as empty
Empty set(0.0 sec)
14.Find the name of employee who has a dependent.
mysql> select distinct(fname) from employee_45 as e,dependent_45 as d where
e.ssn=d.essn;
+----------+
| fname |
+----------+
| John |
| Franklin |
| jennifer |
+----------+
15.Find the name of employee who has no dependent.
mysql> select fname from employee_45 where ssn not in (select essn from
dependent_45);
+--------+
| fname |
+--------+
| joyce |
| ramesh |
| james |
| ahmad |
| alicia |
+--------+

16.Find the number of dependents for each employee.


mysql> select fname,count(*) from employee_45 as e,dependent_45 as d where
e.ssn=d.essn group by essn;
+----------+----------+
| fname | count(*) |
+----------+----------+
| John | 3 |
| Franklin | 3 |
| jennifer | 1 |
+----------+----------+
17.Write a query to display the name and salary of all employees whose salary is not in
the range of 40000 and 50000.
mysql> select fname,salary from employee_45 where salary not between 40000 and
50000;
+----------+----------+
| fname | salary |
+----------+----------+
| john | 36000.00 |
| joyce | 30000.00 |
| james | 67320.00 |
| jennifer | 51600.00 |
| ahmad | 30000.00 |
| alicia | 30000.00 |
+----------+----------+

18.Write a query to display the name and salary of employees who earned more than
30000 and are in department number 1 or 3.
ROLL NO:2127220801045 PAGE NO:5
mysql> select fname,salary from employee_45 where salary>30000 and dno in (1,3);
+-------+----------+
| fname | salary |
+-------+----------+
| james | 67320.00 |
+-------+----------+

19.Find the total salary paid for employee of each department.


mysql> select dno,sum(salary) from employee_45 group by dno;
+-----+-------------+
| dno | sum(salary) |
+-----+-------------+
| 1 | 67320.00 |
| 4 | 111600.00 |
| 5 | 159600.00 |
+-----+-------------+

20. Select the name of the employee who is working under manager of department 4.
mysql> select fname from employee_45 where super_ssn in (select ssn from
employee_45 where dno=4);
+--------+
| fname |
+--------+
| ahmad |
| alicia |
+--------+

21.Find the employee who is neither a supervisor nor a manager.


mysql> select fname from employee_45 where not exists(select mgr_ssn,mgr_ssn from
employee_45,department_45 where dno=dnumber);
Empty set (0.08 sec)

22.Find the employees who are not working in any project.


mysql> select fname from employee_45 where ssn not in (select essn from works_on_45
where ssn=essn);
+-------+
| fname |
+-------+
| james |
+-------+

23.Print the project wise count of employees.


mysql> select pname,count(*) from employee_45,project_45,works_on_45 where essn=ssn
and pnumber=pno group by pname;
+------------------+----------+
| pname | count(*) |
+------------------+----------+
| pcomputerization | 3 |
| pnewbenefits | 3 |
| preorganization | 2 |
| productX | 2 |
| productY | 3 |
| productZ | 2 |

ROLL NO:2127220801045 PAGE NO:6


+------------------+----------+

24.Find the employee who is not working in the same projects as that of Narayan.
mysql> select fname from employee_45,works_on_45 where essn=ssn and pno in (select
pno from employee_45,works_on_45 where lname='narayan' and ssn=essn);
+----------+
| fname |
+----------+
| franklin |
| ramesh |
+----------+

25.Retrieve the employee numbers of all employees who work on project located in
Bellaire, Houston, or Stafford.
mysql> select plocation,count(*) from employee_45,project_45,works_on_45 where
essn=ssn and pnumber=pno and plocation in ('bellaire','houston','stafford') group
by plocation;
+-----------+----------+
| plocation | count(*) |
+-----------+----------+
| bellaire | 2 |
| houston | 4 |
| stafford | 6 |
+-----------+----------+

26.List the names of employees along with their dependents’ names and relationships.
mysql> select fname,dependent_name,relationship from employee_45,dependent_45 where
essn=ssn;
+----------+----------------+--------------+
| fname | dependent_name | relationship |
+----------+----------------+--------------+
| John | Alice | Daughter |
| John | Elizabeth | Spouse |
| John | Michael | Son |
| Franklin | Alice | Daughter |
| Franklin | Joy | Spouse |
| Franklin | Theodore | Son |
| jennifer | Abner | Spouse |
+----------+----------------+--------------+

27.List the name and SSN of employees who have daughters born after 1980
mysql> select fname as name,ssn from employee_45 as e,dependent_45 as d where
relationship='daughter' and d.bdate>'1980/12/31' and ssn=essn;
+----------+-------+-----------+
| FNAME | LNAME | SSN |
+----------+-------+-----------+
| John | Smith | 123456789 |
| Franklin | Wong | 333445555 |
+----------+-------+-----------+

28.List the names and SSN of employees who manage a department.


mysql> select fname,ssn from employee_45,department_45 where mgr_ssn=ssn;
+----------+-----------+
| fname | ssn |
+----------+-----------+
| franklin | 333445555 |
| james | 888665555 |

ROLL NO:2127220801045 PAGE NO:7


| jennifer | 987654321 |
+----------+-----------+

29.For each employee, retrieve the employee's name, and the name of his or her
immediate supervisor.
mysql> select a.fname as emp,b.fname as supervisor from employee_45 as
a,employee_45 as b where a.super_ssn=b.ssn;
+----------+------------+
| emp | supervisor |
+----------+------------+
| john | franklin |
| franklin | james |
| joyce | franklin |
| ramesh | franklin |
| jennifer | james |
| ahmad | jennifer |
| alicia | jennifer |
+----------+------------+

30.Retrieve the details of all employees who reside in Houston.


mysql> select * from employee_45 where address like '%houston%';
+----------+-------+---------+-----------+------------+------------------------+------+----------+-----------+-----+
| fname | minit | lname | ssn | bdate | address | sex | salary | super_ssn | dno |
+----------+-------+---------+-----------+------------+------------------------+------+----------+-----------+-----+
| john | B | smith | 123456789 | 1965-01-09 | 731 fondren,houston,tx | M | 36000.00 | 333445555 | 5 |
| franklin | T | wong | 333445555 | 1955-12-08 | 638 voss,houston,tx | M | 48000.00 | 888665555 | 5 |
| joyce | A | english | 453453453 | 1972-07-31 | 5631 rice,houston,tx | F | 30000.00 | 333445555 | 5 |
| james | E | borg | 888665555 | 1937-11-10 | 450 stone,houston,tx | M | 67320.00 | NULL | 1 |
| ahmad | V | jabbar | 987987987 | 1969-03-29 | 980 dallas,houston,tx | M | 30000.00 | 987654321 | 4 |
+----------+-------+---------+-----------+------------+------------------------+------+----------+-----------+-----+

31.Retrieve the details of all employees who reside in the same location as their project.

ROLL NO:2127220801045 PAGE NO:8


31.Retrieve the list of employees and the projects they work on ordered by department;
and within each department ordered by their last name and first name.
mysql> select fname,lname,pname from employee_45 as e,project_45 as p,works_on_45
as w where w.essn=e.ssn and w.pno=p.pnumber order by dno,lname,fname;
+----------+---------+------------------+
| fname | lname | pname |
+----------+---------+------------------+
| ahmad | jabbar | pnewbenefits |
| ahmad | jabbar | pcomputerization |
| jennifer | wallace | pnewbenefits |
| jennifer | wallace | preorganization |
| alicia | zelaya | pnewbenefits |
| alicia | zelaya | pcomputerization |
| joyce | english | productY |
| joyce | english | productX |
| ramesh | narayan | productZ |
| john | smith | productY |
| john | smith | productX |
| franklin | wong | productZ |
| franklin | wong | preorganization |
| franklin | wong | pcomputerization |
| franklin | wong | productY |
+----------+---------+------------------+

32.Update the project location to ‘Bellaire’ for all the projects in department no 5 with
Project Number 10.

mysql> UPDATE project_45 SET Plocation='Bellaire' WHERE Dnum=5 and Pnumber=10;


Query OK, 0 rows affected (0.00 sec)
Rows matched: 0 Changed: 0 Warnings: 0

RESULT:

IT22312 – DATABASE CONCEPTS LABORATORY


ROLL NO:2127220801045 PAGE NO:9
EX.NO: 4
DATE:
Views and Sequences

AIM:

QUERIES:
1. Create a view SIMPLE_EMP with SSN,ENAME,SALARY, DNO from Employee.
mysql> CREATE VIEW SIMPLE_EMP_45 AS SELECT SSN, FNAME AS ENAME, SALARY, DNO
FROM EMPLOYEE_45;
Query OK, 0 rows affected (0.02 sec)

mysql> SELECT * FROM SIMPLE_EMP_45;


+-----------+----------+----------+-----+
| SSN | ENAME | SALARY | DNO |
+-----------+----------+----------+-----+
| 123456789 | John | 39675.00 | 5 |
| 333445555 | Franklin | 40000.00 | 5 |
| 453453453 | joyce | 25000.00 | 5 |
| 666884444 | ramesh | 38000.00 | 5 |
| 888665555 | james | 11000.00 | 1 |
| 987654321 | jennifer | 43000.00 | 4 |
| 987987987 | ahmed | 25000.00 | 4 |
| 999887777 | alicia | 25000.00 | 4 |
+-----------+----------+----------+-----+

2. Create a View SORTED_EMP listing the employees sorted by their First_names.


mysql> create view SORTED_EMP_45 as select fname,lname from employee_45 order by
fname;
Query OK, 0 rows affected (0.04 sec)

mysql> SELECT * FROM SORTED_EMP_45;


+----------+---------+
| fname | lname |
+----------+---------+
| ahmed | jabber |
| alicia | zelya |
| Franklin | Wong |
| james | borg |
| jennifer | Wallace |
| John | Smith |
| joyce | english |
| ramesh | narayan |
+----------+---------+
3. Create a View SUB_SMITH to store all subordinates whose name has ‘SMITH’ .
mysql> create view SUB_SMITH_45 as (select fname from employee_45 where
(fname='smith' or lname = 'smith') and ssn not in (select mgr_ssn from
department_45));
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT * FROM SUB_SMITH_45;

ROLL NO:2127220801045 PAGE NO:10


+-------+
| fname |
+-------+
| John |
+-------+
4. Create a View MGR_SMITH to store all Managers whose name has ‘SMITH’ .
mysql> CREATE VIEW MGR_SMITH_45 AS SELECT * FROM EMPLOYEE_45 WHERE SSN IN ( SELECT
DISTINCT SUPER_SSN FROM EMPLOYEE_45 WHERE LNAME LIKE '%SMITH%');
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT * FROM MGR_SMITH_45;


+----------+-------+-------+-----------+------------+---------------------+------+----------+-----------+-----+
| FNAME | MINIT | LNAME | SSN | BDATE | address | SEX | SALARY | SUPER_SSN | DNO |
+----------+-------+-------+-----------+------------+---------------------+------+----------+-----------+-----+
| Franklin | T | Wong | 333445555 | 1955-12-18 | 638 Voss,Houston,TX | M | 40000.00 | 888665555 | 5 |
+----------+-------+-------+-----------+------------+---------------------+------+----------+-----------+-----+

5. Display the tuples of SUB_SMITH and MGR_SMITH (Hint: union).


mysql> SELECT * FROM employee_45 WHERE Super_ssn = (SELECT Ssn FROM employee_45
WHERE Lname = 'Smith') AND Lname <> 'Smith' UNION SELECT * FROM employee_45 WHERE
Lname = 'Smith';
+-------+-------+-------+-----------+------------+--------------------------+------+----------+-----------+-----
+----------------------+------------+
| FNAME | MINIT | LNAME | SSN | BDATE | address | SEX | SALARY | SUPER_SSN | DNO |
Supervisor_Join_Date | Join_Date |
+-------+-------+-------+-----------+------------+--------------------------+------+----------+-----------+-----
+----------------------+------------+
| John | B | Smith | 123456789 | 1965-01-09 | 731 Fondren, Houston, TX | M | 39675.00 | 333445555 | 5 | 1988-05-22
| 1999-10-13 |
+-------+-------+-------+-----------+------------+--------------------------+------+----------+-----------+-----
+----------------------+------------+
1 row in set (0.00 sec)

6. Create a View to display the number of employees in each department as DNO,


NO_of_Employees.
mysql> create view COUNT_45 as (select dno,count(*) from employee_45,department_45
where dno=dnumber group by dno);
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT * FROM COUNT_45;


+-----+----------+
| dno | count(*) |
+-----+----------+
| 5 | 4 |
| 1 | 1 |
| 4 | 3 |
+-----+----------+
7. Create a view to display the names of employees starting with alphabet ‘C to S’.
mysql> CREATE VIEW Employees_C_to_S_45 AS SELECT Fname, Lname FROM employee_45
WHERE Fname BETWEEN 'C%' AND 'S%';
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT * FROM Employees_C_to_S_45;


+----------+---------+
| Fname | Lname |
+----------+---------+
| John | Smith |
| Franklin | Wong |
| joyce | english |
| ramesh | narayan |
| james | borg |
| jennifer | Wallace |

ROLL NO:2127220801045 PAGE NO:11


+----------+---------+
6 rows in set (0.00 sec)

8. Create a View to list the names of employees who work in a project located at
“Stafford”.
mysql> create view STAFFORD_LIST_45 as (select fname from
employee_45,project_45,works_on_45 where pno=pnumber and essn=ssn and
plocation='stafford');

mysql> SELECT * FROM STAFFORD_LIST_45;


+----------+
| fname |
+----------+
| Franklin |
| ahmed |
| alicia |
| ahmed |
| alicia |
+----------+
9. Create a View SINGLES as employees who do not have dependents.
mysql> create view SINGLES_45 as (select DISTINCT ssn,fname from
employee_45,dependent_45 where ssn not in (select essn from dependent_45));
Query OK, 0 rows affected (0.01 sec)

mysql> SELECT * FROM SINGLES_45;


+-----------+--------+
| ssn | fname |
+-----------+--------+
| 453453453 | joyce |
| 666884444 | ramesh |
| 888665555 | james |
| 987987987 | ahmed |
| 999887777 | alicia |
+-----------+--------+

10.Drop all views


mysql> create view DUMMY_VIEW as (select ssn,fname,dno from employee_45);
mysql> drop view DUMMY_VIEW;
Query OK, 0 rows affected (0.02 sec)

RESULT:

ROLL NO:2127220801045 PAGE NO:12

You might also like