Skip to content
Advertisement

SQL query causes error “Incorrect syntax near the keyword ‘order'”

This is my SQL query, and I get an error:

Incorrect syntax near the keyword ‘order’

I have checked everything, but I am unable to correct this. How can I fix it?

SELECT TOP (1)
    CONVERT(VARCHAR(19), DayCheckOut, 120)
FROM
    (SELECT TOP (2)
         A1.DayCheckOut AS DayCheckOut
     FROM
         EmployeeAttendace A1
     INNER JOIN
         EmployeeMaster B1 ON A1.EmployeeId = B1.Id
     WHERE
         B1.EmailId = 'raja.xyz@gmail.com'
     ORDER BY
         A1.Id DESC)
ORDER BY
    DayCheckOut DESC

Advertisement

Answer

Use an alias name:

 SELECT top 1
 convert(varchar(19),DayCheckOut,120)
 FROM
   (SELECT top 2 A1.DayCheckOut as DayCheckOut FROM EmployeeAttendace A1 INNER JOIN
    EmployeeMaster B1 ON A1.EmployeeId = B1.Id WHERE B1.EmailId =
    'pooja.yadav@computronics.in' order by A1.Id desc) as AliasName
 order by DayCheckOut desc
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement