Invalid identifier when using left outer join in SQL











up vote
0
down vote

favorite












I get this error for the below code.




Invalid identifier




Any suggestions?



SELECT 
subjects.neptuncode AS "neptuncode",
subjects.subjectname AS "subjectname",
enrollments.examdate AS "examdate"
FROM
subjects, enrollments
LEFT OUTER JOIN
enrollments ON enrollments.subject = subjects.subject_ID
HAVING
COUNT(enrollments.enrollmentdate) = 1
ORDER BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate;









share|improve this question
























  • You should explain what you want to do, given that your query has multiple errors. A database tag is also helpful.
    – Gordon Linoff
    Nov 8 at 18:56












  • enrollments LEFT OUTER JOIN enrollments ...
    – jarlh
    Nov 8 at 20:28










  • Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was replaced with the proper ANSI JOIN syntax in the ANSI-92 SQL Standard (more than 25 years ago) and its use is discouraged
    – marc_s
    Nov 8 at 21:22















up vote
0
down vote

favorite












I get this error for the below code.




Invalid identifier




Any suggestions?



SELECT 
subjects.neptuncode AS "neptuncode",
subjects.subjectname AS "subjectname",
enrollments.examdate AS "examdate"
FROM
subjects, enrollments
LEFT OUTER JOIN
enrollments ON enrollments.subject = subjects.subject_ID
HAVING
COUNT(enrollments.enrollmentdate) = 1
ORDER BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate;









share|improve this question
























  • You should explain what you want to do, given that your query has multiple errors. A database tag is also helpful.
    – Gordon Linoff
    Nov 8 at 18:56












  • enrollments LEFT OUTER JOIN enrollments ...
    – jarlh
    Nov 8 at 20:28










  • Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was replaced with the proper ANSI JOIN syntax in the ANSI-92 SQL Standard (more than 25 years ago) and its use is discouraged
    – marc_s
    Nov 8 at 21:22













up vote
0
down vote

favorite









up vote
0
down vote

favorite











I get this error for the below code.




Invalid identifier




Any suggestions?



SELECT 
subjects.neptuncode AS "neptuncode",
subjects.subjectname AS "subjectname",
enrollments.examdate AS "examdate"
FROM
subjects, enrollments
LEFT OUTER JOIN
enrollments ON enrollments.subject = subjects.subject_ID
HAVING
COUNT(enrollments.enrollmentdate) = 1
ORDER BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate;









share|improve this question















I get this error for the below code.




Invalid identifier




Any suggestions?



SELECT 
subjects.neptuncode AS "neptuncode",
subjects.subjectname AS "subjectname",
enrollments.examdate AS "examdate"
FROM
subjects, enrollments
LEFT OUTER JOIN
enrollments ON enrollments.subject = subjects.subject_ID
HAVING
COUNT(enrollments.enrollmentdate) = 1
ORDER BY
subjects.neptuncode, subjects.subjectname, enrollments.examdate;






sql






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 8 at 21:22









marc_s

568k12810991249




568k12810991249










asked Nov 8 at 18:50









Emil Bədrəddinli

356




356












  • You should explain what you want to do, given that your query has multiple errors. A database tag is also helpful.
    – Gordon Linoff
    Nov 8 at 18:56












  • enrollments LEFT OUTER JOIN enrollments ...
    – jarlh
    Nov 8 at 20:28










  • Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was replaced with the proper ANSI JOIN syntax in the ANSI-92 SQL Standard (more than 25 years ago) and its use is discouraged
    – marc_s
    Nov 8 at 21:22


















  • You should explain what you want to do, given that your query has multiple errors. A database tag is also helpful.
    – Gordon Linoff
    Nov 8 at 18:56












  • enrollments LEFT OUTER JOIN enrollments ...
    – jarlh
    Nov 8 at 20:28










  • Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was replaced with the proper ANSI JOIN syntax in the ANSI-92 SQL Standard (more than 25 years ago) and its use is discouraged
    – marc_s
    Nov 8 at 21:22
















You should explain what you want to do, given that your query has multiple errors. A database tag is also helpful.
– Gordon Linoff
Nov 8 at 18:56






You should explain what you want to do, given that your query has multiple errors. A database tag is also helpful.
– Gordon Linoff
Nov 8 at 18:56














enrollments LEFT OUTER JOIN enrollments ...
– jarlh
Nov 8 at 20:28




enrollments LEFT OUTER JOIN enrollments ...
– jarlh
Nov 8 at 20:28












Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was replaced with the proper ANSI JOIN syntax in the ANSI-92 SQL Standard (more than 25 years ago) and its use is discouraged
– marc_s
Nov 8 at 21:22




Bad habits to kick : using old-style JOINs - that old-style comma-separated list of tables style was replaced with the proper ANSI JOIN syntax in the ANSI-92 SQL Standard (more than 25 years ago) and its use is discouraged
– marc_s
Nov 8 at 21:22












3 Answers
3






active

oldest

votes

















up vote
2
down vote













Change this line



FROM subjects, enrollments


to



FROM subjects


You shouldn't mix the old style of joins (comma delimited) and explicit joins. Really you shouldn't use comma delimited joins as they were essentially replaced many, many years ago.






share|improve this answer























  • You can, but not on the same table without proper aliases (and you shouldn't be using implicit joins anyway, since explicit joins are a part of ANSI-SQL for almost 30 years now...)
    – Zohar Peled
    Nov 8 at 18:53






  • 1




    IMHO one shouldn't mix old comma-syntax joins with any other query that contains characters. :-)
    – Shawn
    Nov 8 at 18:53










  • Just a pedantic comment: some databases allow you to mix old & new style joins. It's confusing to me, and my take is they shouldn't be allowed. But they do.
    – The Impaler
    Nov 8 at 18:53










  • Thanks for the feedback; answer updated.
    – UnhandledExcepSean
    Nov 8 at 18:54










  • @TheImpaler, the old style comma separated joins are still in the SQL standard, can't really be removed... But don't use them when writing new code!
    – jarlh
    Nov 8 at 20:29


















up vote
1
down vote














FROM subjects, enrollments




There's your problem.
Change that to FROM subjects - problem solved.






share|improve this answer




























    up vote
    1
    down vote













    I think the query you want is:



    SELECT s.neptuncode, s.subjectname, MAX(e.examdate) as "examdate"
    FROM subjects s JOIN
    enrollments e
    ON e.subject = s.subject_ID
    GROUP BY s.neptuncode, s.subjectname
    HAVING COUNT(*) = 1
    ORDER BY s.neptuncode, s.subjectname;


    This returns the exam date for subjects that have only one enrollee.



    Notes:





    • Never use commas in the FROM clause. Always use proper explicit JOIN syntax.

    • A LEFT JOIN is not needed. Your HAVING clause suggests that you want exactly one match.

    • You should be using GROUP BY; the HAVING presupposes that you want to aggregate.

    • There is no need to include the exam date in the ORDER BY, because there is only one row per neptun code and subject.






    share|improve this answer





















    • Thanks a lot I got the idea. But how can I list the codes(neptuncode) and names of all subjects(subjectname) in case of those subjects, for which any of the students registered for the first time, list exam date for this condition.
      – Emil Bədrəddinli
      Nov 8 at 19:14






    • 2




      @EmilBədrəddinli . . . You should ask a new question as a new question. Along the way, it is good practice to accept an answer for this question.
      – Gordon Linoff
      Nov 8 at 19:33











    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214302%2finvalid-identifier-when-using-left-outer-join-in-sql%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    3 Answers
    3






    active

    oldest

    votes








    3 Answers
    3






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    2
    down vote













    Change this line



    FROM subjects, enrollments


    to



    FROM subjects


    You shouldn't mix the old style of joins (comma delimited) and explicit joins. Really you shouldn't use comma delimited joins as they were essentially replaced many, many years ago.






    share|improve this answer























    • You can, but not on the same table without proper aliases (and you shouldn't be using implicit joins anyway, since explicit joins are a part of ANSI-SQL for almost 30 years now...)
      – Zohar Peled
      Nov 8 at 18:53






    • 1




      IMHO one shouldn't mix old comma-syntax joins with any other query that contains characters. :-)
      – Shawn
      Nov 8 at 18:53










    • Just a pedantic comment: some databases allow you to mix old & new style joins. It's confusing to me, and my take is they shouldn't be allowed. But they do.
      – The Impaler
      Nov 8 at 18:53










    • Thanks for the feedback; answer updated.
      – UnhandledExcepSean
      Nov 8 at 18:54










    • @TheImpaler, the old style comma separated joins are still in the SQL standard, can't really be removed... But don't use them when writing new code!
      – jarlh
      Nov 8 at 20:29















    up vote
    2
    down vote













    Change this line



    FROM subjects, enrollments


    to



    FROM subjects


    You shouldn't mix the old style of joins (comma delimited) and explicit joins. Really you shouldn't use comma delimited joins as they were essentially replaced many, many years ago.






    share|improve this answer























    • You can, but not on the same table without proper aliases (and you shouldn't be using implicit joins anyway, since explicit joins are a part of ANSI-SQL for almost 30 years now...)
      – Zohar Peled
      Nov 8 at 18:53






    • 1




      IMHO one shouldn't mix old comma-syntax joins with any other query that contains characters. :-)
      – Shawn
      Nov 8 at 18:53










    • Just a pedantic comment: some databases allow you to mix old & new style joins. It's confusing to me, and my take is they shouldn't be allowed. But they do.
      – The Impaler
      Nov 8 at 18:53










    • Thanks for the feedback; answer updated.
      – UnhandledExcepSean
      Nov 8 at 18:54










    • @TheImpaler, the old style comma separated joins are still in the SQL standard, can't really be removed... But don't use them when writing new code!
      – jarlh
      Nov 8 at 20:29













    up vote
    2
    down vote










    up vote
    2
    down vote









    Change this line



    FROM subjects, enrollments


    to



    FROM subjects


    You shouldn't mix the old style of joins (comma delimited) and explicit joins. Really you shouldn't use comma delimited joins as they were essentially replaced many, many years ago.






    share|improve this answer














    Change this line



    FROM subjects, enrollments


    to



    FROM subjects


    You shouldn't mix the old style of joins (comma delimited) and explicit joins. Really you shouldn't use comma delimited joins as they were essentially replaced many, many years ago.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 8 at 18:54

























    answered Nov 8 at 18:51









    UnhandledExcepSean

    9,60522040




    9,60522040












    • You can, but not on the same table without proper aliases (and you shouldn't be using implicit joins anyway, since explicit joins are a part of ANSI-SQL for almost 30 years now...)
      – Zohar Peled
      Nov 8 at 18:53






    • 1




      IMHO one shouldn't mix old comma-syntax joins with any other query that contains characters. :-)
      – Shawn
      Nov 8 at 18:53










    • Just a pedantic comment: some databases allow you to mix old & new style joins. It's confusing to me, and my take is they shouldn't be allowed. But they do.
      – The Impaler
      Nov 8 at 18:53










    • Thanks for the feedback; answer updated.
      – UnhandledExcepSean
      Nov 8 at 18:54










    • @TheImpaler, the old style comma separated joins are still in the SQL standard, can't really be removed... But don't use them when writing new code!
      – jarlh
      Nov 8 at 20:29


















    • You can, but not on the same table without proper aliases (and you shouldn't be using implicit joins anyway, since explicit joins are a part of ANSI-SQL for almost 30 years now...)
      – Zohar Peled
      Nov 8 at 18:53






    • 1




      IMHO one shouldn't mix old comma-syntax joins with any other query that contains characters. :-)
      – Shawn
      Nov 8 at 18:53










    • Just a pedantic comment: some databases allow you to mix old & new style joins. It's confusing to me, and my take is they shouldn't be allowed. But they do.
      – The Impaler
      Nov 8 at 18:53










    • Thanks for the feedback; answer updated.
      – UnhandledExcepSean
      Nov 8 at 18:54










    • @TheImpaler, the old style comma separated joins are still in the SQL standard, can't really be removed... But don't use them when writing new code!
      – jarlh
      Nov 8 at 20:29
















    You can, but not on the same table without proper aliases (and you shouldn't be using implicit joins anyway, since explicit joins are a part of ANSI-SQL for almost 30 years now...)
    – Zohar Peled
    Nov 8 at 18:53




    You can, but not on the same table without proper aliases (and you shouldn't be using implicit joins anyway, since explicit joins are a part of ANSI-SQL for almost 30 years now...)
    – Zohar Peled
    Nov 8 at 18:53




    1




    1




    IMHO one shouldn't mix old comma-syntax joins with any other query that contains characters. :-)
    – Shawn
    Nov 8 at 18:53




    IMHO one shouldn't mix old comma-syntax joins with any other query that contains characters. :-)
    – Shawn
    Nov 8 at 18:53












    Just a pedantic comment: some databases allow you to mix old & new style joins. It's confusing to me, and my take is they shouldn't be allowed. But they do.
    – The Impaler
    Nov 8 at 18:53




    Just a pedantic comment: some databases allow you to mix old & new style joins. It's confusing to me, and my take is they shouldn't be allowed. But they do.
    – The Impaler
    Nov 8 at 18:53












    Thanks for the feedback; answer updated.
    – UnhandledExcepSean
    Nov 8 at 18:54




    Thanks for the feedback; answer updated.
    – UnhandledExcepSean
    Nov 8 at 18:54












    @TheImpaler, the old style comma separated joins are still in the SQL standard, can't really be removed... But don't use them when writing new code!
    – jarlh
    Nov 8 at 20:29




    @TheImpaler, the old style comma separated joins are still in the SQL standard, can't really be removed... But don't use them when writing new code!
    – jarlh
    Nov 8 at 20:29












    up vote
    1
    down vote














    FROM subjects, enrollments




    There's your problem.
    Change that to FROM subjects - problem solved.






    share|improve this answer

























      up vote
      1
      down vote














      FROM subjects, enrollments




      There's your problem.
      Change that to FROM subjects - problem solved.






      share|improve this answer























        up vote
        1
        down vote










        up vote
        1
        down vote










        FROM subjects, enrollments




        There's your problem.
        Change that to FROM subjects - problem solved.






        share|improve this answer













        FROM subjects, enrollments




        There's your problem.
        Change that to FROM subjects - problem solved.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 8 at 18:51









        Zohar Peled

        51.8k73172




        51.8k73172






















            up vote
            1
            down vote













            I think the query you want is:



            SELECT s.neptuncode, s.subjectname, MAX(e.examdate) as "examdate"
            FROM subjects s JOIN
            enrollments e
            ON e.subject = s.subject_ID
            GROUP BY s.neptuncode, s.subjectname
            HAVING COUNT(*) = 1
            ORDER BY s.neptuncode, s.subjectname;


            This returns the exam date for subjects that have only one enrollee.



            Notes:





            • Never use commas in the FROM clause. Always use proper explicit JOIN syntax.

            • A LEFT JOIN is not needed. Your HAVING clause suggests that you want exactly one match.

            • You should be using GROUP BY; the HAVING presupposes that you want to aggregate.

            • There is no need to include the exam date in the ORDER BY, because there is only one row per neptun code and subject.






            share|improve this answer





















            • Thanks a lot I got the idea. But how can I list the codes(neptuncode) and names of all subjects(subjectname) in case of those subjects, for which any of the students registered for the first time, list exam date for this condition.
              – Emil Bədrəddinli
              Nov 8 at 19:14






            • 2




              @EmilBədrəddinli . . . You should ask a new question as a new question. Along the way, it is good practice to accept an answer for this question.
              – Gordon Linoff
              Nov 8 at 19:33















            up vote
            1
            down vote













            I think the query you want is:



            SELECT s.neptuncode, s.subjectname, MAX(e.examdate) as "examdate"
            FROM subjects s JOIN
            enrollments e
            ON e.subject = s.subject_ID
            GROUP BY s.neptuncode, s.subjectname
            HAVING COUNT(*) = 1
            ORDER BY s.neptuncode, s.subjectname;


            This returns the exam date for subjects that have only one enrollee.



            Notes:





            • Never use commas in the FROM clause. Always use proper explicit JOIN syntax.

            • A LEFT JOIN is not needed. Your HAVING clause suggests that you want exactly one match.

            • You should be using GROUP BY; the HAVING presupposes that you want to aggregate.

            • There is no need to include the exam date in the ORDER BY, because there is only one row per neptun code and subject.






            share|improve this answer





















            • Thanks a lot I got the idea. But how can I list the codes(neptuncode) and names of all subjects(subjectname) in case of those subjects, for which any of the students registered for the first time, list exam date for this condition.
              – Emil Bədrəddinli
              Nov 8 at 19:14






            • 2




              @EmilBədrəddinli . . . You should ask a new question as a new question. Along the way, it is good practice to accept an answer for this question.
              – Gordon Linoff
              Nov 8 at 19:33













            up vote
            1
            down vote










            up vote
            1
            down vote









            I think the query you want is:



            SELECT s.neptuncode, s.subjectname, MAX(e.examdate) as "examdate"
            FROM subjects s JOIN
            enrollments e
            ON e.subject = s.subject_ID
            GROUP BY s.neptuncode, s.subjectname
            HAVING COUNT(*) = 1
            ORDER BY s.neptuncode, s.subjectname;


            This returns the exam date for subjects that have only one enrollee.



            Notes:





            • Never use commas in the FROM clause. Always use proper explicit JOIN syntax.

            • A LEFT JOIN is not needed. Your HAVING clause suggests that you want exactly one match.

            • You should be using GROUP BY; the HAVING presupposes that you want to aggregate.

            • There is no need to include the exam date in the ORDER BY, because there is only one row per neptun code and subject.






            share|improve this answer












            I think the query you want is:



            SELECT s.neptuncode, s.subjectname, MAX(e.examdate) as "examdate"
            FROM subjects s JOIN
            enrollments e
            ON e.subject = s.subject_ID
            GROUP BY s.neptuncode, s.subjectname
            HAVING COUNT(*) = 1
            ORDER BY s.neptuncode, s.subjectname;


            This returns the exam date for subjects that have only one enrollee.



            Notes:





            • Never use commas in the FROM clause. Always use proper explicit JOIN syntax.

            • A LEFT JOIN is not needed. Your HAVING clause suggests that you want exactly one match.

            • You should be using GROUP BY; the HAVING presupposes that you want to aggregate.

            • There is no need to include the exam date in the ORDER BY, because there is only one row per neptun code and subject.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 8 at 18:54









            Gordon Linoff

            750k34286393




            750k34286393












            • Thanks a lot I got the idea. But how can I list the codes(neptuncode) and names of all subjects(subjectname) in case of those subjects, for which any of the students registered for the first time, list exam date for this condition.
              – Emil Bədrəddinli
              Nov 8 at 19:14






            • 2




              @EmilBədrəddinli . . . You should ask a new question as a new question. Along the way, it is good practice to accept an answer for this question.
              – Gordon Linoff
              Nov 8 at 19:33


















            • Thanks a lot I got the idea. But how can I list the codes(neptuncode) and names of all subjects(subjectname) in case of those subjects, for which any of the students registered for the first time, list exam date for this condition.
              – Emil Bədrəddinli
              Nov 8 at 19:14






            • 2




              @EmilBədrəddinli . . . You should ask a new question as a new question. Along the way, it is good practice to accept an answer for this question.
              – Gordon Linoff
              Nov 8 at 19:33
















            Thanks a lot I got the idea. But how can I list the codes(neptuncode) and names of all subjects(subjectname) in case of those subjects, for which any of the students registered for the first time, list exam date for this condition.
            – Emil Bədrəddinli
            Nov 8 at 19:14




            Thanks a lot I got the idea. But how can I list the codes(neptuncode) and names of all subjects(subjectname) in case of those subjects, for which any of the students registered for the first time, list exam date for this condition.
            – Emil Bədrəddinli
            Nov 8 at 19:14




            2




            2




            @EmilBədrəddinli . . . You should ask a new question as a new question. Along the way, it is good practice to accept an answer for this question.
            – Gordon Linoff
            Nov 8 at 19:33




            @EmilBədrəddinli . . . You should ask a new question as a new question. Along the way, it is good practice to accept an answer for this question.
            – Gordon Linoff
            Nov 8 at 19:33


















            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53214302%2finvalid-identifier-when-using-left-outer-join-in-sql%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            這個網誌中的熱門文章

            Tangent Lines Diagram Along Smooth Curve

            Yusuf al-Mu'taman ibn Hud

            Zucchini