简体   繁体   中英

Joining three tables in Oracle11g SQL

Normally, this wouldn't be a problem for me, but 1. My instructor never taught us how to join three tables like this and 2. It wasn't even covered on this week's assignment. But because these databases aren't in Oracle interface database that we normally use, I have no way of even doing trial and error.

I have three tables. The contents aren't important to the question. The first table is STUDENTS, with columns LAST_NAME, SID, and MID (two of the MID cells have NULL values here). Second table is COURSES, with columns COURSES (don't know why whoever designed this question decided to name it the same as the table) and CID (I'm assuming that's a primary key) Third table is ENROLLED, with columns SID and CID.

I'm asked the following question "Use the three tables shown above to answer the following questions: a. What kind of join would you use to join all three tables? Write the syntax that would produce the desired result. b. Name two tables that could be used to retrieve data from a natural join. Write the syntax that would produce the desired result."

I'm stuck on this one. Internet hasn't been much help so far, not even Oracle's materials. We learned about joining, but we never joined more than two tables. Any help would be appreciated.

STUDENTS, with columns LAST_NAME, SID, and MID COURSES, with columns COURSES and CID ENROLLED, with columns SID and CID.

a table can be joined with another table if both the table have one column in common. so here STUDENTS TABLE can be joined with ENROLLED,

ENROLLED.SID =STUDENTS.SID

and COURSES can be joined with ENROLLED

ENROLLED.CID=COURSES.CID

What kind of join would you use to join all three tables? Write the syntax that would produce the desired result.

select last_name, courses
  from students 
    left join enrolled on students.sid = enrolled.sid
    left join courses on enrolled.cid = courses.cid

or

select last_name, courses from students
  left join (select * from enrolled join courses using (cid)) using (sid)

SQLFiddle

Here I used left join to show student even if he did not enroll anywhere. But you can use also inner join(s) to avoid such situation or full join to show everything - students who enrolled somewhere, these who did not enroll and courses that have no students. In this Stack Overflow topic you have very good explanation how different types of joins works.

Name two tables that could be used to retrieve data from a natural join. Write the syntax that would produce the desired result.

These pairs are: ENROLLED and STUDENTS (common key SID) and ENROLLED and COURSES (common key CID).

select * from enrolled natural join students;

select * from enrolled natural join courses;

You can use SQLFiddle to test your own ideas, like I used for first query. Choose database (Oracle) from listbox, next build schema (create tables, insert data) in left panel then write queries in right panel. If query runs succesfully results will be visible at bottom.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM