Download presentation
Presentation is loading. Please wait.
Published byIlene Harvey Modified over 9 years ago
1
Displaying Data from Multiple Tables
2
EMPNO DEPTNO LOC ----- ------- -------- 7839 10 NEW YORK 7698 30 CHICAGO 7782 10 NEW YORK 7566 20 DALLAS 7654 30 CHICAGO 7499 30 CHICAGO... 14 rows selected. EMPNO DEPTNO LOC ----- ------- -------- 7839 10 NEW YORK 7698 30 CHICAGO 7782 10 NEW YORK 7566 20 DALLAS 7654 30 CHICAGO 7499 30 CHICAGO... 14 rows selected. EMPDEPT EMPNOENAME...DEPTNO -----------...------ 7839KING... 10 7698BLAKE... 30... 7934MILLER... 10 DEPTNO DNAME LOC ------ ------------------ 10ACCOUNTINGNEW YORK 20RESEARCHDALLAS 30SALESCHICAGO 40OPERATIONSBOSTON Obtaining Data from Multiple Tables
3
Displaying Data from Multiple Tables What is a join? When data from more than one table in the database is required. A join condition is used. Rows in one table can be joined to rows in another table according to common values existing in corresponding columns, usually primary and foreign key columns.
4
Guidelines: When writing a SELECT statement that joins tables. Precede the column name with the table name for clarity and to enhance database access. Write the join condition in the where clause. If the same column name appears in more than one table, the column name must be prefixed with the table name. To join n tables together, you need minimum of (n-1) join conditions. Therefore, to join four tables, a minimum of three joins are required. This rule may not apply if your table has a concatenated primary key, in which case more than one column is required to uniquely identify each row.
5
Query 1 Retrieve the name and address of all employees who work for the 'Research' department SELECT FNAME, LNAME, ADDRESS FROM EMPLOYEE, DEPARTMENT WHERE DNAME='Research' AND DNUMBER=DNO;
6
Query 2 For every project located in 'Stafford', list the project number, the controlling department number, and the department manager's last name, address, and birthdate. SELECT PNUMBER, DNUM, LNAME, BDATE, ADDRESS FROMPROJECT, DEPARTMENT, EMPLOYEE WHERE DNUM=DNUMBER AND MGRSSN=SSN AND PLOCATION='Stafford'
7
Query 3 The name of the females employees and the name of their male dependents. SELECT fname, lname,d.dependent_name FROM employee e, dependent d WHERE ssn=d.ESSN and d.sex='M' and e.sex='F'
8
Query 4 All managers first and last names who begun managing the department during 1981 and 1991 select fname, lname from employee, department where mgrssn=ssn and (mgrstartdate between '01-jan-1981' and '31-dec-1991')
9
Query 5 Retrieve Employee name and the project name of all employees who worked on a project 10 to 30 hours. SELECT fname, lname,pname FROM employee, works_on, project WHERE pnumber=pno and essn=ssn and (hours between 10 and 30 )
10
Query 6 List the project names and the total hours the employees have spent on each. SELECT pname, sum(hours) FROM works_on, project WHERE pno=pnumber GROUP BY pname
11
Query 7 For each department that has more that 2 employees, retrieve the department name and the average salary of all employees working in that department arranged in descend order by the average salary. SELECT dname, count(*), avg(salary) FROM employee, department WHERE dno=dnumber GROUP BY dname HAVING count(*)>=2 ORDER BY avg(salary) desc
12
Query 8 Retrieve the names of all employees who have more than two dependents SELECT fname, lname, count(*) "Number of dependents" FROM employee, dependent WHERE ssn=essn GROUP BY fname, lname HAVING count(*) >2;
13
Self joins Joining table to itself Consider the situation, where we want to display the name of Smith’s supervisor, you need to: Find Smith in the employee table by looking at the LNAME column. Find the supervisor number for Smith by looking at the SUPERSSN column. Smith’s supervisor SSN is 333445555. Find the name of the supervisor with SSN 333445555 by looking at LNAME column. Franklin SSN is 333445555, so Franklin is Smith’s manager.
14
Query 9 For each employee, retrieve the employee's name, and the name of his or her immediate supervisor SELECTE.FNAME, E.LNAME, S.FNAME, S.LNAME FROM EMPLOYEE E, EMPLOYEE S WHEREE.SUPERSSN=S.SSN
15
Example of table aliases Retrieve the name and address of all employees who work for the 'Research' department SELECT E.FNAME, E.LNAME, E.ADDRESS FROM EMPLOYEE E, DEPARTMENT D WHERE D.DNAME='Research' AND D.DNUMBER=E.DNO;
16
The DELETE Command Removes rows from a relation Includes a WHERE-clause to select the rows to be deleted rows are deleted from only one table at a time A missing WHERE-clause specifies that all rows in the relation are to be deleted; the table then becomes an empty table The number of rows deleted depends on the number of rows in the relation that satisfy the WHERE-clause Referential integrity should be enforced
17
The DELETE Command DELETE FROM EMPLOYEE WHERELNAME='Brown’ DELETE FROM EMPLOYEE WHERE SSN='123456789’ DELETE FROM EMPLOYEE
18
The UPDATE Command Used to modify attribute values of one or more selected rows A WHERE-clause selects the rows to be modified An additional SET-clause specifies the attributes to be modified and their new values Each command modifies rows in the same relation Referential integrity should be enforced
19
The UPDATE Command Change the location and controlling department number of project number 10 to 'Bellaire' and 5, respectively. UPDATE PROJECT SETPLOCATION = 'Bellaire', DNUM = 5 WHEREPNUMBER=10
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.