Data in my table looks like below :
Here is my code.
If @PId = 101 then the result will be
My question : is there a better way to achieve the same instead of using Sub query.
Thanks in advance
A lot depends on other information, not least of which is what do yuo mean by 'better'? Are we to assume this relates to a situation in your system where there is a similar circumstance but with much more data? Having a varchar variable and matching that to an INT column is a conversion that is unnecessary. Not having any indexes on the table is not good but I would hope the real tables you are working with would have one or two. You can get the same results with
but whether it is better depends on running it as a comparison and seeing the results. Until an index is added to the table then the estimated plan show my version as being more work than your code. Once an index of
Can you give us more information or is this all the sort of information you need?