A query with an ORDER BY clause cannot be used as a table expression. That is, a view, inline table-valued function,sub0query, derived table
or common table expression. This produces the error message "The ORDER BY clause is invalid in views, inline functions, derived tables, and subqueries, unless TOP is also specified."
SELECT UserID, Name
FROM (SELECT UserID, Name FROM TSubscribers ORDER BY Name) AS D
The way to get around this is to use the TOP option.
SELECT UserID, Name
FROM (SELECT TOP 100 Percent UserID, Name, Company FROM TSubscribers ORDER BY Name) AS D
SELECT CASE
SELECT CustomerID, City,
CASE
WHEN COUNT(orderID) = 0 Then 'No_Orders'
WHEN COUNT(orderID) <=2 Then 'Up_to_2_orders'
WHEN COUNT(orderID) > 2 Then 'More_than_2_orders'
END AS Category
FROM Customers
GROUP BY CustomerID, City