SQLLanjut (Data base).pdf
-
Upload
aldo-rialdo -
Category
Documents
-
view
17 -
download
0
Transcript of SQLLanjut (Data base).pdf
OutlineMultiple Relation SQL QueriesMultiple Relation SQL QueriesNested Queries in SQL
2
Union, Intersection, DifferenceSQLnya :SQLnya :
U UNION∩ INTERSECT- MINUS- MINUS
Banyak SQL dialect tidak supportINTERSECT & DIFFERENCEQueries diformulasikan dengan Queries diformulasikan dengan menggunakan IN dan NOT IN
3
C hContoh…(union)
List the ESSN’s of employees who have dependents or work on projectsList the ESSN s of employees who have dependents or work on projects
WORKS_ON [ESSN, PNo, Hours]DEPENDENT [ESSN Dep Name Sex DOB Relationship]DEPENDENT [ESSN, Dep_Name, Sex, DOB, Relationship]
SELECT ESSN FROM WORKS_ONUNION ALLUNION SELECT ESSN FROM DEPENDENT
ALL duplikasi baris akan ditampilkan semua
ALL
ALL duplikasi baris akan ditampilkan semua
4
C hContoh…(intersect)
List the ESSN’s of employees who have dependents and work on projectsList the ESSN s of employees who have dependents and work on projects
WORKS_ON [ESSN, PNo, Hours]DEPENDENT [ESSN Dep Name Sex DOB Relationship]DEPENDENT [ESSN, Dep_Name, Sex, DOB, Relationship]
SELECT ESSN FROM WORKS_ONINTERSECT WHERE ESSN ININTERSECT SELECT ESSN FROM DEPENDENT(
WHERE ESSN IN)
5
C hContoh…(difference)
List the ESSN’s of employees who have dependents but do not work onList the ESSN s of employees who have dependents but do not work on projects
WORKS ON [ESSN, PNo, Hours]WORKS_ON [ESSN, PNo, Hours]DEPENDENT [ESSN, Dep_Name, Sex, DOB, Relationship]
SELECT ESSN FROM DEPENDENTSELECT ESSN FROM DEPENDENTWHERE ESSN NOT IN(SELECT ESSN FROM WORKS_ON)
6
Join di SQL
× =
SQL :SQL :SELECT * FROM S1, R1 related tableWHERE S1 sid R1 sid join condition
7
WHERE S1.sid = R1.sid join condition
Contoh…(join)
List the names of the managers of each departmentList the names of the managers of each department.EMPLOYEE[Ename, SSN, DOB, Address, Sex, Salary, SuperSSN, Dno]DEPARTMENT[DName, DNumber, MgrSSN, MgrStart]
DEPT MGR DEPARTMENT EMPLOYEEDEPT_MGR DEPARTMENT MGRSSN = SSN EMPLOYEE
RESULT Π Ename (DEPT_MGR)
SELECT Ename DnameSELECT Ename, DnameFROM DEPARTMENT, EMPLOYEEWHERE MgrSSN = SSN
SELECT E.Ename, D.DnameFROM DEPARTMENT AS D, EMPLOYEE AS EWHERE D.MgrSSN = E.SSN
8
Outer JoinOuter JoinOuter Join
Baris yang tidak memenuhi join condition tetap dipertahankanFull outer join
Baris tabel kiri & kanan dimasukkan pada tabel hasilBaris tabel kiri & kanan dimasukkan pada tabel hasilKolom relasi hasil dari kedua tabel diisi NULL
Left outer joinBaris tabel kiri dimasukkan pada tabel hasilBaris tabel kiri dimasukkan pada tabel hasilKolom relasi hasil dari tabel kanan diisi NULL
Right outer joinBaris tabel kanan dimasukkan pada tabel hasilBaris tabel kanan dimasukkan pada tabel hasilKolom relasi hasil dari tabel kiri diisi NULL
9
10
Contoh… (left outer join)
SELECT g girls t toySELECT g.girls, t.toyFROM toys tLEFT OUTER JOIN girls gON g toy id=t toy idON g.toy_id=t.toy_id
Hasil LEFT OUTER JOIN ?Hasil LEFT OUTER JOIN ?
11
Contoh… (left outer join)
SELECT g girls t toySELECT g.girls, t.toyFROM toys tLEFT OUTER JOIN girls gON g toy id=t toy idON g.toy_id=t.toy_id
Hasil LEFT OUTER JOIN ?
12
Contoh… (right outer join)
SELECT g girls t toySELECT g.girls, t.toyFROM toys tRIGHT OUTER JOIN girls gON g toy id=t toy idON g.toy_id=t.toy_id
Hasil RIGHT OUTER JOIN ?
13
Inner JoinJoin dengan dirinya sendiriJoin dengan dirinya sendiri
Bagaimana jika ingin mengetahui nama boss dari tiap pegawai?
14
Contoh… (inner join)
SELECT c1 name c2 name AS bossSELECT c1.name, c2.name AS bossFROM clow_info c1INNER JOIN clow_info c2ON c1.boss_id=c2.id
15
Nested QueriesQuery di dalam query (query bersarang)Query di dalam query (query bersarang)Sering disebut subquery atau inner queryQuery yang mengandung inner query disebut outer querySQL :SQL :
SELECT ... FROM … WHERE outer queryexpression {NOT IN|IN}-expression- comparison-operator {ANY|ALL}-{EXIST|NOT EXIST}(SELECT … FROM … WHERE ...); inner query( ); q y
16
Contoh… (nested query =)
List all employees with a salary equal to the lowest salary paidList all employees with a salary equal to the lowest salary paidEMPLOYEE[Ename, SSN, DOB, Address, Sex, Salary, SuperSSN, Dno]
SELECT Ename salary FROM EMPLOYEESELECT Ename, salary FROM EMPLOYEEWHERE salary =(SELECT min(salary) FROM EMPLOYEE);
17
Contoh… (nested query IN)
List names and birth dates for employees who live at the same address asList names and birth dates for employees who live at the same address as Smith”EMPLOYEE[Ename, SSN, DOB, Address, Sex, Salary, SuperSSN, Dno]
SELECT EName, DOB FROM EMPLOYEEWHERE Address IN(SELECT Address FROM EMPLOYEE(SELECT Address FROM EMPLOYEEWHERE EName = “Smith”);
SELECT E1 EName E1 DOBSELECT E1.EName, E1.DOBFROM EMPLOYEE E1, EMPLOYEE E2WHERE E1.Address = E2.Address and E2.Ename = “Smith”
18
Contoh… (nested query IN)
List department names for departments that have a manager with last nameList department names for departments that have a manager with last name “Smith”EMPLOYEE[Ename, LName, SSN, DOB, Address, Sex, Salary, Dno]DEPARTMENT[DName, DNumber, MgrSSN, MgrStart]DEPARTMENT[DName, DNumber, MgrSSN, MgrStart]
SELECT Dname FROM DEPARTMENTWHERE MgrSSN INWHERE MgrSSN IN(SELECT SSN FROM EMPLOYEEWHERE LName = “Smith”);
19
Contoh… (nested query >ALL)
List employees whose salary is greater than the salary of all List employees whose salary is greater than the salary of all employees in department 5EMPLOYEE[Ename, LName, SSN, DOB, Address, Sex, Salary, Dno]
SELECT * FROM EMPLOYEEWHERE salary > ALL(SELECT salary FROM EMPLOYEE(SELECT salary FROM EMPLOYEEWHERE Dno=5);
20
Contoh… (equivalence of IN and =ANY)
List department names for departments that have a manager with last nameList department names for departments that have a manager with last name “Smith”EMPLOYEE[Ename, LName, SSN, DOB, Address, Sex, Salary, Dno]DEPARTMENT[DName, DNumber, MgrSSN, MgrStart]DEPARTMENT[DName, DNumber, MgrSSN, MgrStart]
SELECT DISTINCT DnameFROM DEPARTMENTFROM DEPARTMENTWHERE MgrSSN = ANY(SELECT SSN
FROM EMPLOYEEFROM EMPLOYEEWHERE Lname = “Smith”)
21
Contoh… (nested query =ANY)
List the departments that have a project (s) in “Brisbane”List the departments that have a project (s) in BrisbaneDEPARTMENT[Dname, Dnumber, MgrSSN, MgrStart]PROJECT[PName, Pnumber, Plocation, Dnum]
SELECT *FROM DEPARTMENTWHERE Brisbane = ANYWHERE Brisbane = ANY(SELECT Plocation
FROM PROJECTWHERE DNum = DNumber);WHERE DNum = DNumber);
22
Contoh… (equivalence of NOT IN and <>ALL)
Find the names of departments that are not responsible for any projectFind the names of departments that are not responsible for any project located in TaipeiDEPARTMENT[DName, DNumber, MgrSSN, MgrStart]PROJECT[PName, Pnumber, Plocation, Dnum]PROJECT[PName, Pnumber, Plocation, Dnum]
SELECT Dname FROM DEPARTMENTWHERE Taipei NOT INWHERE Taipei NOT IN(SELECT Plocation FROM PROJECT
WHERE DNum = DNumber)
23
Contoh… (equivalence of NOT IN and <>ALL)
Find the names of departments that are not responsible for any projectFind the names of departments that are not responsible for any project located in TaipeiDEPARTMENT[DName, DNumber, MgrSSN, MgrStart]PROJECT[PName, Pnumber, Plocation, Dnum]PROJECT[PName, Pnumber, Plocation, Dnum]
SELECT Dname FROM DEPARTMENTWHERE Taipei <> ALLWHERE Taipei <> ALL(SELECT Plocation FROM PROJECT
WHERE DNum = DNumber)
24
<>ANY,<>ALL, NOT INIf a sub-query returns:{$30K $32K $37K}If a sub query returns:{$30K, $32K, $37K}
<> ANY means– NOT=$30K OR NOT=$32K OR NOT=$37K<> ALL means– NOT=$30K AND NOT=$32K AND NOT=$37KNOT IN means– NOT=$30K AND NOT=$32K AND NOT=$37K
25
C h Contoh… (nested query EXISTS)
Display the names of employees who have dependentsDisplay the names of employees who have dependentsEMPLOYEE[Ename, SSN, DOB, Address, Sex, Salary, SuperSSN, Dno]DEPENDENT [ESSN, Dep_Name, Sex, DOB, Relationship]
SELECT DISTINCT ENSELECT DISTINCT ENameFROM EMPLOYEEWHERE EXISTS
(SELECT *(FROM DEPENDENTWHERE ESSN = SSN )
26
C h Contoh… (nested query NOT EXISTS)
Display the names of employees who have no dependentsDisplay the names of employees who have no dependentsEMPLOYEE[Ename, SSN, DOB, Address, Sex, Salary, SuperSSN, Dno]DEPENDENT [ESSN, Dep_Name, Sex, DOB, Relationship]
SELECT DISTINCT ENameFROM EMPLOYEEWHERE NOT EXISTSWHERE NOT EXISTS
(SELECT *FROM DEPENDENTWHERE ESSN = SSN )WHERE ESSN = SSN )
27
C h Contoh… (nested query EXISTS)
Display the names of managers who have at least one dependentDisplay the names of managers who have at least one dependentEMPLOYEE[Ename, SSN, DOB, Address, Sex, Salary, SuperSSN, Dno]DEPARTMENT[Dname, Dnumber, MgrSSN, MgrStart]DEPENDENT [ESSN, Dep_Name, Sex, DOB, Relationship]
SELECT ENameFROM EMPLOYEEWHERE EXISTS
(SELECT * FROM DEPENDENTWHERE ESSN = SSN ) AND EXISTS(SELECT * FROM DEPARTMENTWHERE SSN = MgrSSN)WHERE SSN = MgrSSN)
28
C h Contoh… (nested query)
Display the details of employees who work on ALL projectsDisplay the details of employees who work on ALL projectsEMPLOYEE[Ename, SSN, DOB, Address, Sex, Salary, SuperSSN, Dno]PROJECT[PName, Pnumber, Plocation, Dnum]WORKS_ON [ESSN, Pno, Hours]_ [ ]
SELECT *FROM EMPLOYEEWHERE NOT EXISTS
(SELECT * FROM PROJECTWHERE NOT EXISTS
(SELECT * FROM WORKS ON(SELECT * FROM WORKS_ONWHERE ESSN = EMPLOYEE.SSNAND PNO = PROJECT.PNO)
29
NextView Function Stored Prosedure TriggerView, Function, Stored Prosedure, Trigger
30