You are on page 1of 9

IT22312 – DATABASE CONCEPTS LABORATORY

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

AIM:

QUERIES:
1. Retrieve SSN and project location for all employees.
mysql> select ssn,plocation from employee_112,project_112 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 fname,address from employee_112,department_112 where dno=dnumber
anddname='research';
+ + +
| 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:2127220801112 PAGE NO:


3. Retrieve the name and ssn of all employees who have a daughter.
mysql> select fname,ssn from employee_112,dependent_112 where essn=ssn
andrelationship='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 pnumber,project_112.dnum,lname,address,bdate from
employee_112,department_112,project_112 where plocation='stafford'and
dnumber=dnum and mgr_ssn=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_112 as
e,employee_112 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_112 as e set salary=1.15*salary where ssn in (select ssn
fromworks_on_112 as w,project_112 as p where w.essn=e.ssn and w.pno=p.pnumber
and pname='productX' and hours>30);
Query OK

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_112 as e,works_on_112 as w,project_112
asp where e.ssn=w.essn and w.pno=p.pnumber order by dno,fname;
+ + + +
| fname | pname | dno |
+ + + +
| ahmad | computerization | 4 |

ROLL NO:2127220801112 PAGE NO:


| 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_112 as d,project_112 where dnumber=dnum group
bydnum 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 fname as name,ssn from employee_112 as e,dependent_112 as d
wherefname=dependent_name and e.sex=d.sex and essn=ssn;
Empty set (0.00 sec)

10. Find the names of the employees joined prior to their supervisors
mysql> ALTER TABLE employee_112 ADD COLUMN Join_Date DATE;

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


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

mysql> UPDATE employee_112 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_112 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_112 e JOIN department_112 d ON e.Dno =
d.Dnumber WHERE e.Join_Date < d.Mgr_start_date;

ROLL NO:2127220801112 PAGE NO:


+----------+---------+--------------------+----------------------+
| 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_112
wheressn in (select mgr_ssn from department_112);
+ + +
| oldest | youngest |
+ + +
| 1955-12-61 | 1937-11-10 |
+ + +

12.Find name of employee whose experience is greater than all employee of the
company.
mysql> SELECT fname FROM Employee_112 WHERE salary > (SELECT MAX(salary)
FROM Employee_112);
Empty set (0.05 sec)

13.Find name of employee whose experience is less than some employee of the
department 2.
In employee_112 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_112 as e,dependent_112 as d


wheree.ssn=d.essn;
+ +
| fname |
+ +
| franklin |
| jennifer |
+ +

ROLL NO:2127220801112 PAGE NO:


15. Find the name of employee who has no dependent.
mysql> select fname from employee_112 where ssn not in (select essn
fromdependent_112);
+ +
| fname |
+ +
| john |
| joyce |
| ramesh |
| james |
| ahmad |
| alicia |
+ +

16. Find the number of dependents for each employee.


mysql> select fname,count(*) from employee_112 as e,dependent_112 as d
wheree.ssn=d.essn group by essn;
+ + +
| fname | count(*) |
+ + +
| 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_112 where salary not between 40000
and50000;
+ + +
| fname | salary |
+ + +
| john | 36000.00 |
| joyce | 30000.00 |
| james | 67320.00 |
| jennifer | 61600.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.
mysql> select fname,salary from employee_112 where salary>30000 and dno in (1,3);
+ + +
| fname | salary |

ROLL NO:2127220801112 PAGE NO:


+ + +
| james | 67320.00 |
+ + +

19. Find the total salary paid for employee of each department.
mysql> select dno,sum(salary) from employee_112 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_112 where super_ssn in (select ssn
fromemployee_112 where dno=4);
+ +
| fname |
+ +
| ahmad |
| alicia |
+ +

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


mysql> select fname from employee_112 where not exists(select mgr_ssn,mgr_ssn
fromemployee_112,department_112 where dno=dnumber);
Empty set (0.112 sec)

22. Find the employees who are not working in any project.
mysql> select fname from employee_112 where ssn not in (select essn from
works_on_112where ssn=essn);
+ +
| fname |
+ +
| james |
+ +

23. Print the project wise count of employees.


mysql> select pname,count(*) from employee_112,project_112,works_on_112 where
essn=ssnand pnumber=pno group by pname;
+ + +
| pname | count(*) |
+ + +
| pcomputerization | 3 |
| pnewbenefits | 3 |
| preorganization | 2 |
| productX | 2 |
| productY | 3 |
| productZ | 2 |
+ + +

24. Find the employee who is not working in the same projects as that of Narayan.
ROLL NO:2127220801112 PAGE NO:
mysql> select fname from employee_112,works_on_112 where essn=ssn and pno in
(selectpno from employee_112,works_on_112 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_112,project_112,works_on_112 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_112,dependent_112
whereessn=ssn;
+ + + +
| fname | dependent_name | relationship |
+ + + +
| 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_112 as e,dependent_112 as d
whererelationship='daughter' and d.bdate>'1980/12/31' and ssn=essn;
+ + +
| name | ssn |
+ + +
| franklin | 333445555 |
+ + +

28. List the names and SSN of employees who manage a department.
mysql> select fname,ssn from employee_112,department_112 where mgr
_ssn=ssn;
+ + +
| fname | ssn |
+ + +
| franklin | 333445555 |
| james | 888665555 |
| jennifer | 987654321 |
+ + +

29.For each employee, retrieve the employee's name, and the name of his or her
immediate supervisor.

ROLL NO:2127220801112 PAGE NO:


mysql> select a.fname as emp,b.fname as supervisor from employee_112
asa,employee_112 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_112 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-61 | 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.
mysql> select * from employee_112, project_112where address like
'%plocation%';
Empty set (0.01 sec)

32.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_112 as e,project_112 as
p,works_on_112as 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 |
+ + + +

ROLL NO:2127220801112 PAGE NO:


33.Update the project location to ‘Bellaire’ for all the projects in department no 5 with
Project Number 10.
mysql> UPDATE project_112 SET Plocation='Bellaire' WHERE Dnum=5 and Pnumber=10;

mysql> select*from project_112;


+-----------------+---------+-----------+------+---------+
| Pname | Pnumber | Plocation | Dnum | MGR_SSN |
+-----------------+---------+-----------+------+---------+
| ProductX | 1 | Bellaire | 5 | NULL |
| ProductY | 2 | Bellaire | 5 | NULL |
| ProductZ | 3 | Houston | 5 | NULL |
| Computerization | 10 | Bellaire | 4 | NULL |
| Reorganization | 20 | Houston | 1 | NULL |
| Newbenefits | 30 | Stafford | 4 | NULL |
+-----------------+---------+-----------+------+---------+
6 rows in set (0.01 sec)

RESULT:

ROLL NO:2127220801112 PAGE NO:

You might also like