Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
LEFT JOIN vs FULL JOIN
Message
 
General information
Forum:
Visual FoxPro
Category:
Databases,Tables, Views, Indexing and SQL syntax
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01514564
Message ID:
01514694
Views:
31
>>>>>>Hi,
>>>>>>
>>>>>>I am working on joining two tables into a query and it looks like Selecting From Table1 and FULL JOIN Table2 is the same as Select from Table2 and LEFT JOINT Table1. Is there advantage using one or the other? The tables/queries Table1 and Table2 are very small (less than 100 records) so I am not sure there would be difference in speed. But I am wondering if one is more practical then the other. TIA.
>>>>>
>>>>>The results returtned by LEFT JOIN vs. FULL JOIN are different when you have non-matching records in both tables. If you only have, say, records in Table1 and some of them don't have matching records in Table2, but the opposite is not true, then LEFT JOIN and FULL JOIN will return the exactly same result and if you can anticipate this situation, it's better to use LEFT JOIN then.
>>>>
>>>>Thank you again for your input. I was too tired last night to really understand what you are saying. Today I have played with some sample data, trying difference scenarios, and I choose to go with FULL JOIN. The reason is that if Table2 has record(s) not matching to the records in Table1, the LEFT JOIN selects this record(s) but the value in the column on which the LEFT JOIN is built gets NULL value. The other way, with FULL JOIN, the column on which the JOIN is built always gets the value (correct value). This is important to me.
>>>
>>>I think you're still missing the point.
>>>
>>>If table2 has some records that don't have corresponding records in table1, then
>>>
>>>select Table2.* from Table2 LEFT JOIN Table1 ...
>>>
>>>Here you need to start with the table that may not have corresponding records in another table.
>>>
>>>FULL JOIN is only used when the situation is symmetrical, e.g. you may have non-matching records in either of the tables. Then and only then it make sense to use FULL JOIN.
>>
>>Let me show you an example:
>>
>>
>>CREATE CURSOR Table1 (fk_fld i, task_no i)
>>INSERT INTO Table1 VALUES (1,1)
>>INSERT INTO Table1 VALUES (1,2)
>>INSERT INTO Table1 VALUES (1,2)
>>INSERT INTO Table1 VALUES (1,3)
>>
>>*!!!! This is the record that makes the difference.
>>*-- Record of TASK_NO that does not exist.  
>>INSERT INTO Table1 VALUES (1,7)
>>
>>*-- Note that in Table2 each record has a unique value in TASK_NO.  This is must.
>>CREATE CURSOR Table2 (task_no i, flda c(1), fldb c(1))
>>INSERT INTO Table2 VALUES (1,"A","B")
>>INSERT INTO Table2 VALUES (2,"C","D")
>>INSERT INTO Table2 VALUES (3,"C","F")
>>INSERT INTO Table2 VALUES (4,"K","I")
>>INSERT INTO Table2 VALUES (6,"M","N")
>>
>>*-- Sample LEFT JOIN
>>
>>Select Table1.FK_FLD,;
>>       Table2.TASK_NO,;
>>       Table2.FLDA,;
>>       Table2.FLDB;
>>from table2 ;
>>LEFT JOIN Table1 ON table2.TASK_NO = Table1.TASK_NO;
>>
>>*-- Herer is the FULL JOIN
>>
>>Select Table1.FK_FLD,;
>>       NVL(Table1.TASK_NO,Table2.TASK_NO) as TASK_NO,;
>>       Table2.FLDA,;
>>       Table2.FLDB;
>>from Table1 ;
>>FULL JOIN Table2 ON Table2.TASK_NO = Table1.TASK_NO
>>
>>
>>Note that if not for the record in Table1 where TASK_NO is 7 the LEFT JOIN and FULL JOIN would be the same. But when I add this record to the Table1 the FULL JOIN query includes this record. But the LEFT JOIN does not.
>>
>>Do you see what I mean?
>
>As I said, you need to start from Table1. Change your query to use RIGHT JOIN or start from the Table1 since Table1 has more records than Table2 in your example.
>
>Though in your case you have non-matching records in both tables (you don't have 6 and 4 in your first table). In this case you do need to use FULL JOIN.

Where do you see that Table1 has more records than Table2 in my example? And now you are adding RIGHT JOIN to the mix (before we only talked about LEFT JOIN and FULL JOIN). Also, keep in mind that the number of records in Table1 and Table2 can vary. It is not always that one has more records than the other. But what I am wondering is, what is the hypothetical example where FULL JOIN will not give me the right results?

UPDATE. I am sorry. I misread. I see that you agree that I need FULL JOIN. Thank you.
"The creative process is nothing but a series of crises." Isaac Bashevis Singer
"My experience is that as soon as people are old enough to know better, they don't know anything at all." Oscar Wilde
"If a nation values anything more than freedom, it will lose its freedom; and the irony of it is that if it is comfort or money that it values more, it will lose that too." W.Somerset Maugham
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform