• Hmmm...I'm a little confused about the purpose of this code.

    First, the WHERE clause filters results to rows that have a particular ORDERID, and then you SELECT DISTINCT ORDERID, so you know ahead of time you're either going to get zero rows (when no rows exist with that ORDERID), or one row with the value you pass to the WHERE clause for ORDERID. So, ultimately, you're only really finding whether rows exist with that ORDERID or not.

    Second, since you're only going to return one value for ORDERID, why does it need to be sorted? The result set will be the same whether ordered or not, given the current query.

    I'm guessing that the query you posted has been altered for presentation on the forum, and some key details were modified in meaning-changing ways. Otherwise this is very strange 🙂

    Cheers!