简体   繁体   中英

I am getting a “Identifier identifier” error when executing a SQL query

I am not sure how to fix the error that is referring to enrollment.student_id

SELECT std_name, course#
FROM student
INNER JOIN (SELECT min(grade) FROM enrollment GROUP BY grade)
ON enrollment.student_id = student.student_id;

getting the following error in Oracle SQL Developer:

ORA-00904: "ENROLLMENT"."STUDENT_ID": invalid identifier
00904. 00000 -  "%s: invalid identifier"
*Cause:    
*Action:
Error at Line: 22 Column: 4

The subquery does not return the student_id , so you can't reference it in the outer query. It seems like you want the minimum grade per student, so you also need to fix the group by clause of the subquery.

I would suggest:

SELECT s.std_name, e.min_grade
FROM student s
INNER JOIN (
    SELECT student_id, min(grade) min_grade 
    FROM enrollment 
    GROUP BY student_id
) e ON e.student_id = s.student_id;

Other fixes to your query:

  • you need to alias min(grade) in the subquery so you can refer to it in the outer query

  • the subquery itself needs an alias

Note that you can get the same result with a correlated subquery - this allows students that have no enrollment at all, while your original query does not, unless you turn the INNER JOIN to a LEFT JOIN :

SELECT 
    s.std_name, 
    (
        SELECT min(e.grade) 
        FROM enrollment e 
        WHERE e.student_id = s.student_id
    ) min_grade
FROM student s

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM