Professional Documents
Culture Documents
DB3EX
DB3EX
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 |
+----------+------------------------+
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
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;
+----------+---------+--------------------+----------------------+
| 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 |
+--------+
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 |
+-------+----------+
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 |
+--------+
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 |
+----------+-------+-----------+
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 |
+----------+------------+
31.Retrieve the details of all employees who reside in the same location as their project.
32.Update the project location to ‘Bellaire’ for all the projects in department no 5 with
Project Number 10.
RESULT:
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)
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');
RESULT: