SQL query causes error “Incorrect syntax near the keyword 'order'”
Clash Royale CLAN TAG#URR8PPP
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 unable to correct this.please help
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
Some dbms want a table alias for the sub-query. Try
...) dt order by DayCheckOut desc
.– jarlh
Aug 8 at 11:09
...) dt order by DayCheckOut desc
Are you using MS SQL Server?
– jarlh
Aug 8 at 11:10
Thanks @Gordon, i give alias name and it works.
– user10176456
Aug 8 at 11:11
You need to add alias name for subquery like (---subquery---) as a
– Aakash Singh
Aug 8 at 11:30
5 Answers
5
Give 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
You need an alias, let's say q
q
for the subquery (SELECT top 2 A1.DayCheckOut as DayC ... order by A1.Id desc) q
(SELECT top 2 A1.DayCheckOut as DayC ... order by A1.Id desc) q
that's very common problem for MS SQL Server DB.
No need for that sub-query, modern SQL Server versions support OFFSET
/FETCH FIRST
.
OFFSET
FETCH FIRST
SELECT convert(varchar(19),A1.DayCheckOut,120)
FROM EmployeeAttendace A1 INNER JOIN EmployeeMaster B1 ON A1.EmployeeId = B1.Id
WHERE B1.EmailId = 'raja.xyz@gmail.com'
order by A1.Id desc
offset 1 fetch next 1 row only
I.e. use OFFSET
to skip the first row, use FETCH NEXT
to return only 1 row.
OFFSET
FETCH NEXT
It errors there, because you need an alias for a subquery.
However, you didn't need the redundant subquery:
SELECT top (1) convert(varchar(19),A1.DayCheckOut,120) as DayCheckOut
FROM EmployeeAttendace A1
INNER JOIN EmployeeMaster B1 ON A1.EmployeeId = B1.Id
WHERE B1.EmailId = 'raja.xyz@gmail.com'
order by A1.DayCheckOut desc;
While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.
– Nic3500
Aug 8 at 11:40
@Nic3500, OK. I will edit a bit.
– Cetin Basoz
Aug 8 at 12:09
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 in ('raja.xyz@gmail.com')
ORDER BY A1.Id desc
)
ORDER BY DayCheckOut DESC
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
You need a table alias for the subquery.
– Gordon Linoff
Aug 8 at 11:09