Josh Pulsipher
Josh Pulsipher

Reputation: 3

Using subqueries to obtain sorted data

So for a school assignment, I need to output the student last name and first name, and just the first 8 that have enrolled, sorted as such. I'm not going to worry about the limiting it to 8 (that's easy when everything else works), but I need to figure out what I'm doing wrong and need to do here. Here's what I have:

SELECT student.first_name, student.last_name
FROM student 
WHERE student.student_id IN (SELECT  enrollment.student_id
FROM enrollment
ORDER BY enrollment.enroll_date);

Upvotes: 0

Views: 31

Answers (1)

T. Peter
T. Peter

Reputation: 887

Well because oracle actually see your code like this:

SELECT student.first_name, student.last_name
FROM student 
WHERE student.student_id IN (
SELECT enrollment.student_id FROM 
enrollment ) --where the ) should be!!!
ORDER BY enrollment.enroll_date  --and this line just mess up the code delete this. 

the reason why this happen is in the subquery you try to order by. Just get rid of that order by or use it outside the subquery and the error should not occur anymore.

Beside when you use IN there is no meaning to use order by in that subquery.

Upvotes: 1

Related Questions