Please refer to the following answer first: this
So in that answer, it provided a way to take a data based on one distinct column with its corresponding columns. How it picks which one of that distinct column can be sorted by ascending or descending on the condition WHERE t1.lastname > t2.lastname
or WHERE t1.lastname < t2.lastname
respectively, which I understand.
I'm still practicing SQL and I have questions in regard to the method provided in the above link.
- When I tried to select
t2.id
, it is all null. I can't comprehend this, isn'tt2
basically the same table ast1
? If so, how is it possible that itsid
becameNULL
, but nott1
sid
s? - Why is it necessary to check
WHERE t2.id IS NULL
when allt2.id
is going to return NULL anyway? - This is a part about which I think I have a slight idea. However, please correct me if I'm wrong. The above method worked (let's talk about the descending order here) because: firstly, the I
LEFT JOIN
ed myt1
andt2
together based on theirid
s. Secondly, I also check that thet1.lastname
has to be bigger (>
) thant2.lastname
which I assume is using ASCII or UNICODE values, which returns me voila! Only one result; the one that has a higher value. Okay now more question: does it check thet1.lastname
witht2.lastname
one by one but to allt2.lastname
and then returns nothing if just one of thet2.lastname
renders that condition invalid?
I think that I'm missing something about something here. Could someone please help me? Thank you in advance.