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'tt2basically the same table ast1? If so, how is it possible that itsidbecameNULL, but nott1sids? - Why is it necessary to check
WHERE t2.id IS NULLwhen allt2.idis 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 JOINed myt1andt2together based on theirids. Secondly, I also check that thet1.lastnamehas to be bigger (>) thant2.lastnamewhich 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.lastnamewitht2.lastnameone by one but to allt2.lastnameand then returns nothing if just one of thet2.lastnamerenders that condition invalid?
I think that I'm missing something about something here. Could someone please help me? Thank you in advance.