On executing a T-SQL UNION statement in SQL Server, I get the following error:
Msg 156, Level 15, State 1, Server WIN-ILO9GLLB9J0, Line 7
Incorrect syntax near the keyword 'UNION'
I am a student practicing SQL queries. I have to find the name of a city with maximum characters and the name of a city with the minimum characters from a table. In case there are cities with the same character length I have to print the first city in alphabetical order. I have written the following code:
SELECT TOP 1 CITY, LEN(CITY)
FROM STATION
ORDER BY LEN(CITY), CITY
UNION
SELECT TOP 1 CITY, LEN(CITY)
FROM STATION
ORDER BY LEN(CITY) DESC, CITY
The two queries work fine on their own.