Oracle select query with inner select query error

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 02:47:55

The problem is placing ORDER BY in the WHERE clause subquery. SQL syntax does not allow you to order elements of the subquery in the WHERE clause, because it does not change the result of the query overall.

You should move it out to fix the syntax:

SELECT empid  FROM employees WHERE deptid IN
    (
        SELECT deptid FROM department WHERE description LIKE '%application%'
    )
ORDER BY createddate DESC

createddate is not a column in employees table. It exists only in department table

Then you need to join to the department table, and use ORDER BY on one of its columns:

SELECT e.empid
FROM employees e
JOIN department d ON e.deptid = d.deptid
WHERE d.description LIKE '%application%'
ORDER BY d.createddate DESC

Obviously, you don't need order by in your subquery and you can remove it and then parenthesis will be just right

SELECT empid  
FROM employees 
WHERE deptid IN (SELECT deptid 
                 FROM department 
                 WHERE description LIKE '%application%');

If you want to apply order by you would have to do it on your outer query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!