Home » SQL & PL/SQL » SQL & PL/SQL » Joining four tables..need help
Joining four tables..need help [message #650393] Sun, 24 April 2016 04:41 Go to next message
dbJuniorAdmin
Messages: 6
Registered: April 2016
Junior Member
Hi

How can I accomplish the following?

Q. Find the full names of applicants, titles of positions they applied for in year 1999 and the names of employers from Sydney who offer the positions.

Tables are: applicant, positions, applies, employer. Applies table has only primary keys of position and applicant

I tried natural join but it outputs "no rows selected"

Pls help.
Re: Joining four tables..need help [message #650394 is a reply to message #650393] Sun, 24 April 2016 04:44 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

What do you expect from us with no table description, no relational model, no relationship between these tables, in short nothing?

Show us the tables, show us what you tried.

Note that "natural join" is a good starting point to program bugs. Better explicitly express the join conditions.

[Updated on: Sun, 24 April 2016 04:45]

Report message to a moderator

Re: Joining four tables..need help [message #650395 is a reply to message #650394] Sun, 24 April 2016 04:45 Go to previous messageGo to next message
dbJuniorAdmin
Messages: 6
Registered: April 2016
Junior Member
Sorry Smile

desc position
Name Null? Type
----------------------------------------- -------- ----------------------------
PNUMBER NOT NULL NUMBER(Cool
TITLE NOT NULL VARCHAR2(20)
SALARY NOT NULL NUMBER(9,2)
EXTRAS VARCHAR2(20)
BONUS NUMBER(9,2)
SPECIFICATION NOT NULL VARCHAR2(30)
ENAME NOT NULL VARCHAR2(32)

SQL> desc employer
Name Null? Type
----------------------------------------- -------- ----------------------------
ENAME NOT NULL VARCHAR2(32)
CITY NOT NULL VARCHAR2(20)
STATE NOT NULL VARCHAR2(20)
PHONE NOT NULL NUMBER(10)
FAX NUMBER(10)
EMAIL VARCHAR2(22)
WEB VARCHAR2(20)

SQL> desc applicant
Name Null? Type
----------------------------------------- -------- ----------------------------
ANUMBER NOT NULL NUMBER(6)
FNAME NOT NULL VARCHAR2(15)
LNAME NOT NULL VARCHAR2(15)
DOB NOT NULL DATE
CITY NOT NULL VARCHAR2(20)
STATE NOT NULL VARCHAR2(20)
PHONE NOT NULL NUMBER(10)
FAX NUMBER(10)
EMAIL VARCHAR2(20)

SQL> desc applies
Name Null? Type
----------------------------------------- -------- ----------------------------
ANUMBER NOT NULL NUMBER(6)
PNUMBER NOT NULL NUMBER(Cool
APPDATE

I tried the following

select distinct title,ename,city,appdate from position natural join employer natural join applies natural join applicant where ((extract(year from appdate)='1999' and city='Sydney'));

no rows selected

[Updated on: Sun, 24 April 2016 04:48]

Report message to a moderator

Re: Joining four tables..need help [message #650396 is a reply to message #650395] Sun, 24 April 2016 04:48 Go to previous messageGo to next message
Michel Cadot
Messages: 68645
Registered: March 2007
Location: Nanterre, France, http://...
Senior Member
Account Moderator

Please read OraFAQ Forum Guide and How to use [code] tags and make your code easier to read.

What are the relationships between the tables? The primary keys? The foreign keys?
Show us the query you tried.

Re: Joining four tables..need help [message #650397 is a reply to message #650396] Sun, 24 April 2016 04:50 Go to previous messageGo to next message
dbJuniorAdmin
Messages: 6
Registered: April 2016
Junior Member
Applicant: Anumber pkey
Employer:Ename Pkey
Position: Pnumber pkey
Re: Joining four tables..need help [message #650398 is a reply to message #650396] Sun, 24 April 2016 04:52 Go to previous messageGo to next message
dbJuniorAdmin
Messages: 6
Registered: April 2016
Junior Member
select distinct title,ename,city,appdate from position natural join employer natural join applies natural join applicant where ((extract(year from appdate)='1999' and city='Sydney'));

no rows selected
Re: Joining four tables..need help [message #650399 is a reply to message #650395] Sun, 24 April 2016 04:54 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
Think about what natural join is doing: joining tables on columns with the same name. Then look at your tables. You have several columns with the same name, how many of them are meaningful join columns?
Re: Joining four tables..need help [message #650400 is a reply to message #650399] Sun, 24 April 2016 04:59 Go to previous messageGo to next message
dbJuniorAdmin
Messages: 6
Registered: April 2016
Junior Member
I only can get this

select distinct title,ename,city,appdate from position natural join employer natural join applies natural where ((extract(year from appdate)='1999' and city='Sydney'));

TITLE                ENAME                            CITY                 APPDATE
-------------------- -------------------------------- -------------------- ---------
Professor            University of New South Wales    Sydney               27-OCT-99
Professor            University of New South Wales    Sydney               26-OCT-99
Lecturer             University of New South Wales    Sydney               13-DEC-99
Professor            University of New South Wales    Sydney               28-OCT-99
Senior Lecturer      University of Technology, Sydney Sydney               17-JUN-99
Senior Lecturer      University of Technology, Sydney Sydney               18-JUN-99
Senior Lecturer      University of Technology, Sydney Sydney               09-MAY-99

7 rows selected.


but how can I get fname and lname?
Re: Joining four tables..need help [message #650403 is a reply to message #650400] Sun, 24 April 2016 06:02 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
One more hint (I'm not going to do your homework for you): use JOIN USING or JOIN ON, not NATURAL JOIN.
Re: Joining four tables..need help [message #650404 is a reply to message #650403] Sun, 24 April 2016 06:23 Go to previous messageGo to next message
dbJuniorAdmin
Messages: 6
Registered: April 2016
Junior Member
I think I did it, but I think it was worth posting here somewhat
as no one really gave me a solution
select fname,lname,title,ename from (select title,city,ename from position natural join employer) natural join applicant natural join applies where (extract(year from appdate)='1999' and city='Sydney');

FNAME           LNAME           TITLE                ENAME
--------------- --------------- -------------------- --------------------------------
John            Spiderman       Professor            University of New South Wales
John            Spiderman       Lecturer             University of New South Wales
Margaret        Finch           Professor            University of New South Wales
Margaret        Finch           Lecturer             University of New South Wales
John            Spiderman       Associate Professor  University of Technology, Sydney
John            Spiderman       Senior Lecturer      University of Technology, Sydney
Margaret        Finch           Associate Professor  University of Technology, Sydney
Margaret        Finch           Senior Lecturer      University of Technology, Sydney

8 rows selected.
Re: Joining four tables..need help [message #650405 is a reply to message #650404] Sun, 24 April 2016 06:32 Go to previous messageGo to next message
John Watson
Messages: 8931
Registered: January 2010
Location: Global Village
Senior Member
What you have done is devised a work around for the limitation of NATURAL JOIN, by projecting only a subset of the columns in EMPLOYER. It works in this case but what if you wanted to include the EMAIL columns in your output? You teacher will, I suppose, have to give you a positive mark, but it may be the lowest possible.
Re: Joining four tables..need help [message #650483 is a reply to message #650393] Tue, 26 April 2016 07:25 Go to previous messageGo to next message
martijn
Messages: 286
Registered: December 2006
Location: Netherlands
Senior Member
Maybe a bit bold question but: do you know what a join actually is?
I have the distinct feeling that you do not understand the idea of joins.

I suggest learning about joins, for example from here
Re: Joining four tables..need help [message #650487 is a reply to message #650483] Tue, 26 April 2016 12:11 Go to previous message
Bill B
Messages: 1971
Registered: December 2004
Senior Member
As a professional software designer/developer for 30 years I can tell you to NEVER use natural joins in any production code. It WILL break eventually. Specify your join conditions. In my mind it shouldn't even be allowed in the language.
Previous Topic: narration in one line
Next Topic: Partitioning Tables
Goto Forum:
  


Current Time: Thu Apr 25 03:26:58 CDT 2024