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.
Here, we have columns- name, occupation and rank (generated for each occupation using ROW_NUMBER() function)
Since 1st it is partitioned by occupation, it gives values sorted in asc order according to occupation and then sorts
the names as asc order.
MAX on a string datatype column gives the maximum order name - in doctor occupation we have names = Jenny and samantha,
so SAMANTHA is selected here, similarly for PROFESSOR we have KETTY, for SINGER we have PRIYA, for Actor we have JULIA
and then there is a GROUP BY RNK,
SO, the table formed here will be :
Occupations
You are viewing a single comment's thread. Return to all comments →
MS SQL SERVER SOLUTION :
explanation :
this query provides a table(table_with_ranks) that looks like :
Here, we have columns- name, occupation and rank (generated for each occupation using ROW_NUMBER() function) Since 1st it is partitioned by occupation, it gives values sorted in asc order according to occupation and then sorts the names as asc order.
Now, this table is used to query :
MAX on a string datatype column gives the maximum order name - in doctor occupation we have names = Jenny and samantha, so SAMANTHA is selected here, similarly for PROFESSOR we have KETTY, for SINGER we have PRIYA, for Actor we have JULIA
and then there is a GROUP BY RNK, SO, the table formed here will be :
and this is the desired solution !
`