Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Joining Continued: Dept. of Computer Science Faculty of Science and Technology

Download as ppt, pdf, or txt
Download as ppt, pdf, or txt
You are on page 1of 10

Joining Continued

Course Code: CSC 2108 Course Title: Introduction to Database

Dept. of Computer Science


Faculty of Science and Technology

Lecturer No: 15 Week No: 09 Semester: Fall


Lecturer: Kazi Sadia(kazi.sadia@aiub.edu)
Lecture Outline
 After completing this lesson, you should be able to do
the following:
 Write SELECT statements to access data from more
than one table using equality and nonequality joins
 View data that generally does not meet a join condition
by using outer joins
 Join a table to itself
Outer Joins
EMP DEPT
ENAME DEPTNO DEPTNO DNAME
----- ------ ------ ----------
KING 10 10 ACCOUNTING
BLAKE 30 30 SALES
CLARK 10 10 ACCOUNTING
JONES 20 20 RESEARCH
... ...
40 OPERATIONS

No employee in the
OPERATIONS department
Outer Joins
 You use an outer join to also see rows that do not
usually meet the join condition.
 Outer join operator is the plus sign (+).

SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column(+)
table1.column(+) == table2.column;
table2.column;

SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column
table1.column == table2.column(+);
table2.column(+);
Using Outer Joins
SQL> SELECT e.ename, d.deptno, d.dname
2 FROM emp e, dept d
3 WHERE e.deptno(+) = d.deptno
4 ORDER BY e.deptno;

ENAME DEPTNO DNAME


---------- --------- -------------
KING 10 ACCOUNTING
CLARK 10 ACCOUNTING
...
40 OPERATIONS
15 rows selected.
Self Joins
EMP (WORKER) EMP (MANAGER)
EMPNO ENAME MGR EMPNO ENAME
----- ------ ---- ----- --------
7839 KING
7698 BLAKE 7839 7839 KING
7782 CLARK 7839 7839 KING
7566 JONES 7839 7839 KING
7654 MARTIN 7698 7698 BLAKE
7499 ALLEN 7698 7698 BLAKE

“MGR in the WORKER table is equal to EMPNO in the


MANAGER table”
Joining a Table to Itself
SQL> SELECT worker.ename||' works for '||manager.ename
2 FROM emp worker, emp manager
3 WHERE worker.mgr = manager.empno;

WORKER.ENAME||'WORKSFOR'||MANAG
WORKER.ENAME||'WORKSFOR'||MANAG
-------------------------------
-------------------------------
BLAKE
BLAKE works
works for
for KING
KING
CLARK
CLARK works
works for
for KING
KING
JONES
JONES works
works for
for KING
KING
MARTIN
MARTIN works
works for
for BLAKE
BLAKE
...
...
13
13 rows
rows selected.
selected.
Summary
SELECT
SELECT table1.column,
table1.column, table2.column
table2.column
FROM
FROM table1,
table1, table2
table2
WHERE
WHERE table1.column1
table1.column1 == table2.column2;
table2.column2;

Equijoin
Equijoin Non-equijoin
Non-equijoin Outer
Outer join
join Self
Self join
join
Books

1. Modern Database Management (Sixth Edition) by Fred R. McFadden, Jeffrey A.


Hoffer, Mary B. Prescott
2. Database System Concepts (Fifth Edition) by Henry F. Korth, S. Sudarshan, A.
Silberschatz
3. Oracle-database-10g-sql-fundamentals-1-student-guide-volume-1
4. SQL and Relational Theory: How to Write Accurate SQL Code by C.J. Date
5. Database Systems: A Practical Approach to Design, Implementation and
Management (4th Edition) by Thomas M. Connolly, Carolyn E. Begg
6. Fundamentals of Database Systems, 5th Edition by RamezElmasri, Shamkant B.
Navathe
7. Database Design and Relational Theory: Normal Forms and All That Jazz by C. J. Date
8. An Introduction to Database Systems 8th Edition, by C.J. Date
References

1. https://www.db-book.com/db6/slide-dir/index.html
2. https://docs.oracle.com/en/database/oracle/oracle-database/20/sqlrf/SQL-
Standards.html#GUID-BCCCFF75-D2A4-43AD-8CAF-C3C97D92AC63
3. https://www.slideshare.net/HaaMeemMohiyuddin1/data-knowledge-and-
information
4. https://www.slideshare.net/tabinhasan/from-data-to-wisdom
5. https://www.slideshare.net/thinnaphat.bo/

You might also like