We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Notice from the image, under professor column, the first Name is indexed as 1, the next name "Birtney" as 2. That is what I mean by index w.r.t occupation.
The below code will only give the "RowNumber" column, to get the result like in image proceed to step 3.
temp - Since I created a temporary table inside the query, I have to give it an alise. It is a good practise.
Why MIN in the select statement?
Since some of us here may not be fimilar with sql, I'll start with where I left so you get the whole picture.
Once you complete step 3, add "ORDER BY Name" (Refer above code on where to add Order by clause). The result will look like this https://imgur.com/aBHUqN6
What changed? the names in all four columns are sorted as per alphabetical order.
Now, we only want the names and not the NULL values from our virtual table. How can we do that? - There maybe be multiple ways, lets us consider the MIN/MAX (Yes, you can replace MIN with MAX and you will get the same result)
Without GROUP BY clause - When a MIN/MAX is used in a Select statement, it will return The "LOWEST" element from each column (which happened to be the first element because we used ORDER BY, if you use MAX, you will get the last element from each column). It will look like this https://imgur.com/XDZzc4Z
That means, you will always get a single row from a table.
Occupations
You are viewing a single comment's thread. Return to all comments →
Let me break it down in steps (answer in MySQL)
Step 1:
Create a virtual table in your head of the data given to us. It look like this https://imgur.com/u6DEcNQ
Step 2:
Create an index column with respect to occupation as "RowNumber".https://imgur.com/QzVCWFn
Notice from the image, under professor column, the first Name is indexed as 1, the next name "Birtney" as 2. That is what I mean by index w.r.t occupation.
The below code will only give the "RowNumber" column, to get the result like in image proceed to step 3.
Step 3:
Combine the result from step 1 and step 2:
Step 4:
Now, Order_by name then Group_By RowNumber.
Using Min/Max, if there is a name, it will return it, if not, return NULL.
**EDIT** I can see many asking why MIN or temp?
temp - Since I created a temporary table inside the query, I have to give it an alise. It is a good practise.
Why MIN in the select statement? Since some of us here may not be fimilar with sql, I'll start with where I left so you get the whole picture.
What changed? the names in all four columns are sorted as per alphabetical order.
Now, we only want the names and not the NULL values from our virtual table. How can we do that? - There maybe be multiple ways, lets us consider the MIN/MAX (Yes, you can replace MIN with MAX and you will get the same result)
Without GROUP BY clause - When a MIN/MAX is used in a Select statement, it will return The "LOWEST" element from each column (which happened to be the first element because we used ORDER BY, if you use MAX, you will get the last element from each column). It will look like this https://imgur.com/XDZzc4Z That means, you will always get a single row from a table.