Search code examples
sql-serverpymssql

Getting nth row on mssql - What that "sq" mean?


Need to get nth row from a table. i found this query, which works exactly matching the requirement.

SELECT TOP 1 name FROM (SELECT TOP 9 name FROM master..syslogins ORDER BY name ASC) sq ORDER BY name DESC

Not sure what is the "sq" in the query point to, so that i can customize for my requirement.

Can anyone let me know what is the "sq" in the above query points to??

Tried:

SELECT TOP 1 user FROM (SELECT TOP 3 user FROM customers ORDER BY user ASC)sq ORDER BY   user DESC

Issue error: No column name was specified for column 1 of 'sq'


Solution

  • use : [] for column names that match with keywords

    SELECT TOP 1 [user] FROM (SELECT TOP 3 [user] FROM customers ORDER BY user ASC)sq ORDER BY  [user] DESC