Joins and Nested Queries 112 Ex3
Joins and Nested Queries 112 Ex3
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 |
+ + +
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 |
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;
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)
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 |
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 |
+ +
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 |
+ +
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.
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 |
+ + + +
RESULT: