Correlated Query with more than one table SQL












0















I need to list some staff details who are registered for 2 or more courses using a sub query.
The staff_id in the staff table is linked to the course table, so s.staff_id = c.staff_id. But I'm really confused and don't know how to correct.



SELECT STAFF.STAFF_ID, STAFF.FIRST_NAME, STAFF.LAST_NAME, STAFF.TITLE, COURSE.ID 
FROM STAFF, COURSE C
WHERE STAFF.STAFF_ID = COURSE.STAFF_ID
AND (SELECT COURSE.COURSE_ID FROM COURSE
GROUP BY STAFF.STAFF_ID
HAVING COUNT(COURSE.COURSE_ID) >=2);









share|improve this question





























    0















    I need to list some staff details who are registered for 2 or more courses using a sub query.
    The staff_id in the staff table is linked to the course table, so s.staff_id = c.staff_id. But I'm really confused and don't know how to correct.



    SELECT STAFF.STAFF_ID, STAFF.FIRST_NAME, STAFF.LAST_NAME, STAFF.TITLE, COURSE.ID 
    FROM STAFF, COURSE C
    WHERE STAFF.STAFF_ID = COURSE.STAFF_ID
    AND (SELECT COURSE.COURSE_ID FROM COURSE
    GROUP BY STAFF.STAFF_ID
    HAVING COUNT(COURSE.COURSE_ID) >=2);









    share|improve this question



























      0












      0








      0








      I need to list some staff details who are registered for 2 or more courses using a sub query.
      The staff_id in the staff table is linked to the course table, so s.staff_id = c.staff_id. But I'm really confused and don't know how to correct.



      SELECT STAFF.STAFF_ID, STAFF.FIRST_NAME, STAFF.LAST_NAME, STAFF.TITLE, COURSE.ID 
      FROM STAFF, COURSE C
      WHERE STAFF.STAFF_ID = COURSE.STAFF_ID
      AND (SELECT COURSE.COURSE_ID FROM COURSE
      GROUP BY STAFF.STAFF_ID
      HAVING COUNT(COURSE.COURSE_ID) >=2);









      share|improve this question
















      I need to list some staff details who are registered for 2 or more courses using a sub query.
      The staff_id in the staff table is linked to the course table, so s.staff_id = c.staff_id. But I'm really confused and don't know how to correct.



      SELECT STAFF.STAFF_ID, STAFF.FIRST_NAME, STAFF.LAST_NAME, STAFF.TITLE, COURSE.ID 
      FROM STAFF, COURSE C
      WHERE STAFF.STAFF_ID = COURSE.STAFF_ID
      AND (SELECT COURSE.COURSE_ID FROM COURSE
      GROUP BY STAFF.STAFF_ID
      HAVING COUNT(COURSE.COURSE_ID) >=2);






      sql oracle correlated-subquery






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 1 at 22:33







      user10776032

















      asked Jan 1 at 21:42









      user10776032user10776032

      83




      83
























          4 Answers
          4






          active

          oldest

          votes


















          0














          If you want staff that have more than one course, a join is not necessary -- implicit or explicit. Here is a more direct approach:



          select s.*
          from staff s
          where s.staff_id in (select c.staff_id
          from course c
          group by c.staff_id
          having count(*) >= 2
          );





          share|improve this answer































            1














            I think that you don’t need a correlated subquery. An aggregate JOIN query with a HAVING clause should do it, like :



            SELECT
            S.STAFF_ID,
            S.FIRST_NAME,
            S.LAST_NAME,
            S.TITLE
            FROM
            STAFF AS S
            INNER JOIN COURSE AS C
            ON S.STAFF_ID = C.STAFF_ID
            GROUP BY
            S.STAFF_ID,
            S.FIRST_NAME,
            S.LAST_NAME,
            S.TITLE
            HAVING
            COUNT(*) >=2
            ;


            This will retrieve all staff persons that have at least two courses.






            share|improve this answer

































              0














              You will need to link your sub query back to your main query. At the moment you are just looking for ANY staff member that has more than two courses in your sub query. Try the below (untested).



              For clarity especially when you are using sub queries it is a good idea to alias all of your tables.



              SELECT S.STAFF_ID, S.FIRST_NAME, S.LAST_NAME, S.TITLE 
              FROM STAFF S, COURSE C
              WHERE S.STAFF_ID = C.STAFF_ID
              AND (SELECT C1.COURSE_ID FROM COURSE c1
              where s.staff_id = c1.staff_id
              HAVING COUNT(C1.COURSE_ID) >=2)


              The above assumes that you need to get some columns out of course as well, however at the moment you have none in your first select so the query could be further simplified to the below if you do not need anything from course.



              SELECT S.STAFF_ID, S.FIRST_NAME, S.LAST_NAME, S.TITLE 
              FROM STAFF S
              where (SELECT C.COURSE_ID FROM COURSE c
              where s.staff_id = c.staff_id
              HAVING COUNT(C.COURSE_ID) >=2)





              share|improve this answer
























              • Ah I forgot I needed to select the course_id in the first part

                – user10776032
                Jan 1 at 22:01



















              0














              If you want only staff details or else can join if needed course details as well



                    SELECT Staff.* from Staff s where   
              s.staff_id in
              (Select
              staff_id from
              (SELECT COURSE_ID,STAFF_ID
              FROM COURSE
              GROUP BY STAFF_ID
              HAVING COUNT(COURSE.COURSE_ID) >=2)
              );


              or via join infact even in join you require a subquery reason being group by when grouping by s.staff_id the c.course_id values will become inconsistent and unsync with s.staff_id



                         Select s.* ,c.COURSE_ID from staff s
              join course c
              on s.staff_id IN (Select c.staff_id group by
              c.staff_id
              HAVING COUNT(*) >=2)





              share|improve this answer

























                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',
                autoActivateHeartbeat: false,
                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%2f53999171%2fcorrelated-query-with-more-than-one-table-sql%23new-answer', 'question_page');
                }
                );

                Post as a guest















                Required, but never shown

























                4 Answers
                4






                active

                oldest

                votes








                4 Answers
                4






                active

                oldest

                votes









                active

                oldest

                votes






                active

                oldest

                votes









                0














                If you want staff that have more than one course, a join is not necessary -- implicit or explicit. Here is a more direct approach:



                select s.*
                from staff s
                where s.staff_id in (select c.staff_id
                from course c
                group by c.staff_id
                having count(*) >= 2
                );





                share|improve this answer




























                  0














                  If you want staff that have more than one course, a join is not necessary -- implicit or explicit. Here is a more direct approach:



                  select s.*
                  from staff s
                  where s.staff_id in (select c.staff_id
                  from course c
                  group by c.staff_id
                  having count(*) >= 2
                  );





                  share|improve this answer


























                    0












                    0








                    0







                    If you want staff that have more than one course, a join is not necessary -- implicit or explicit. Here is a more direct approach:



                    select s.*
                    from staff s
                    where s.staff_id in (select c.staff_id
                    from course c
                    group by c.staff_id
                    having count(*) >= 2
                    );





                    share|improve this answer













                    If you want staff that have more than one course, a join is not necessary -- implicit or explicit. Here is a more direct approach:



                    select s.*
                    from staff s
                    where s.staff_id in (select c.staff_id
                    from course c
                    group by c.staff_id
                    having count(*) >= 2
                    );






                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jan 1 at 22:25









                    Gordon LinoffGordon Linoff

                    780k35310412




                    780k35310412

























                        1














                        I think that you don’t need a correlated subquery. An aggregate JOIN query with a HAVING clause should do it, like :



                        SELECT
                        S.STAFF_ID,
                        S.FIRST_NAME,
                        S.LAST_NAME,
                        S.TITLE
                        FROM
                        STAFF AS S
                        INNER JOIN COURSE AS C
                        ON S.STAFF_ID = C.STAFF_ID
                        GROUP BY
                        S.STAFF_ID,
                        S.FIRST_NAME,
                        S.LAST_NAME,
                        S.TITLE
                        HAVING
                        COUNT(*) >=2
                        ;


                        This will retrieve all staff persons that have at least two courses.






                        share|improve this answer






























                          1














                          I think that you don’t need a correlated subquery. An aggregate JOIN query with a HAVING clause should do it, like :



                          SELECT
                          S.STAFF_ID,
                          S.FIRST_NAME,
                          S.LAST_NAME,
                          S.TITLE
                          FROM
                          STAFF AS S
                          INNER JOIN COURSE AS C
                          ON S.STAFF_ID = C.STAFF_ID
                          GROUP BY
                          S.STAFF_ID,
                          S.FIRST_NAME,
                          S.LAST_NAME,
                          S.TITLE
                          HAVING
                          COUNT(*) >=2
                          ;


                          This will retrieve all staff persons that have at least two courses.






                          share|improve this answer




























                            1












                            1








                            1







                            I think that you don’t need a correlated subquery. An aggregate JOIN query with a HAVING clause should do it, like :



                            SELECT
                            S.STAFF_ID,
                            S.FIRST_NAME,
                            S.LAST_NAME,
                            S.TITLE
                            FROM
                            STAFF AS S
                            INNER JOIN COURSE AS C
                            ON S.STAFF_ID = C.STAFF_ID
                            GROUP BY
                            S.STAFF_ID,
                            S.FIRST_NAME,
                            S.LAST_NAME,
                            S.TITLE
                            HAVING
                            COUNT(*) >=2
                            ;


                            This will retrieve all staff persons that have at least two courses.






                            share|improve this answer















                            I think that you don’t need a correlated subquery. An aggregate JOIN query with a HAVING clause should do it, like :



                            SELECT
                            S.STAFF_ID,
                            S.FIRST_NAME,
                            S.LAST_NAME,
                            S.TITLE
                            FROM
                            STAFF AS S
                            INNER JOIN COURSE AS C
                            ON S.STAFF_ID = C.STAFF_ID
                            GROUP BY
                            S.STAFF_ID,
                            S.FIRST_NAME,
                            S.LAST_NAME,
                            S.TITLE
                            HAVING
                            COUNT(*) >=2
                            ;


                            This will retrieve all staff persons that have at least two courses.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited Jan 1 at 21:54

























                            answered Jan 1 at 21:48









                            GMBGMB

                            15.6k3824




                            15.6k3824























                                0














                                You will need to link your sub query back to your main query. At the moment you are just looking for ANY staff member that has more than two courses in your sub query. Try the below (untested).



                                For clarity especially when you are using sub queries it is a good idea to alias all of your tables.



                                SELECT S.STAFF_ID, S.FIRST_NAME, S.LAST_NAME, S.TITLE 
                                FROM STAFF S, COURSE C
                                WHERE S.STAFF_ID = C.STAFF_ID
                                AND (SELECT C1.COURSE_ID FROM COURSE c1
                                where s.staff_id = c1.staff_id
                                HAVING COUNT(C1.COURSE_ID) >=2)


                                The above assumes that you need to get some columns out of course as well, however at the moment you have none in your first select so the query could be further simplified to the below if you do not need anything from course.



                                SELECT S.STAFF_ID, S.FIRST_NAME, S.LAST_NAME, S.TITLE 
                                FROM STAFF S
                                where (SELECT C.COURSE_ID FROM COURSE c
                                where s.staff_id = c.staff_id
                                HAVING COUNT(C.COURSE_ID) >=2)





                                share|improve this answer
























                                • Ah I forgot I needed to select the course_id in the first part

                                  – user10776032
                                  Jan 1 at 22:01
















                                0














                                You will need to link your sub query back to your main query. At the moment you are just looking for ANY staff member that has more than two courses in your sub query. Try the below (untested).



                                For clarity especially when you are using sub queries it is a good idea to alias all of your tables.



                                SELECT S.STAFF_ID, S.FIRST_NAME, S.LAST_NAME, S.TITLE 
                                FROM STAFF S, COURSE C
                                WHERE S.STAFF_ID = C.STAFF_ID
                                AND (SELECT C1.COURSE_ID FROM COURSE c1
                                where s.staff_id = c1.staff_id
                                HAVING COUNT(C1.COURSE_ID) >=2)


                                The above assumes that you need to get some columns out of course as well, however at the moment you have none in your first select so the query could be further simplified to the below if you do not need anything from course.



                                SELECT S.STAFF_ID, S.FIRST_NAME, S.LAST_NAME, S.TITLE 
                                FROM STAFF S
                                where (SELECT C.COURSE_ID FROM COURSE c
                                where s.staff_id = c.staff_id
                                HAVING COUNT(C.COURSE_ID) >=2)





                                share|improve this answer
























                                • Ah I forgot I needed to select the course_id in the first part

                                  – user10776032
                                  Jan 1 at 22:01














                                0












                                0








                                0







                                You will need to link your sub query back to your main query. At the moment you are just looking for ANY staff member that has more than two courses in your sub query. Try the below (untested).



                                For clarity especially when you are using sub queries it is a good idea to alias all of your tables.



                                SELECT S.STAFF_ID, S.FIRST_NAME, S.LAST_NAME, S.TITLE 
                                FROM STAFF S, COURSE C
                                WHERE S.STAFF_ID = C.STAFF_ID
                                AND (SELECT C1.COURSE_ID FROM COURSE c1
                                where s.staff_id = c1.staff_id
                                HAVING COUNT(C1.COURSE_ID) >=2)


                                The above assumes that you need to get some columns out of course as well, however at the moment you have none in your first select so the query could be further simplified to the below if you do not need anything from course.



                                SELECT S.STAFF_ID, S.FIRST_NAME, S.LAST_NAME, S.TITLE 
                                FROM STAFF S
                                where (SELECT C.COURSE_ID FROM COURSE c
                                where s.staff_id = c.staff_id
                                HAVING COUNT(C.COURSE_ID) >=2)





                                share|improve this answer













                                You will need to link your sub query back to your main query. At the moment you are just looking for ANY staff member that has more than two courses in your sub query. Try the below (untested).



                                For clarity especially when you are using sub queries it is a good idea to alias all of your tables.



                                SELECT S.STAFF_ID, S.FIRST_NAME, S.LAST_NAME, S.TITLE 
                                FROM STAFF S, COURSE C
                                WHERE S.STAFF_ID = C.STAFF_ID
                                AND (SELECT C1.COURSE_ID FROM COURSE c1
                                where s.staff_id = c1.staff_id
                                HAVING COUNT(C1.COURSE_ID) >=2)


                                The above assumes that you need to get some columns out of course as well, however at the moment you have none in your first select so the query could be further simplified to the below if you do not need anything from course.



                                SELECT S.STAFF_ID, S.FIRST_NAME, S.LAST_NAME, S.TITLE 
                                FROM STAFF S
                                where (SELECT C.COURSE_ID FROM COURSE c
                                where s.staff_id = c.staff_id
                                HAVING COUNT(C.COURSE_ID) >=2)






                                share|improve this answer












                                share|improve this answer



                                share|improve this answer










                                answered Jan 1 at 21:51









                                Shaun PetersonShaun Peterson

                                1,0981017




                                1,0981017













                                • Ah I forgot I needed to select the course_id in the first part

                                  – user10776032
                                  Jan 1 at 22:01



















                                • Ah I forgot I needed to select the course_id in the first part

                                  – user10776032
                                  Jan 1 at 22:01

















                                Ah I forgot I needed to select the course_id in the first part

                                – user10776032
                                Jan 1 at 22:01





                                Ah I forgot I needed to select the course_id in the first part

                                – user10776032
                                Jan 1 at 22:01











                                0














                                If you want only staff details or else can join if needed course details as well



                                      SELECT Staff.* from Staff s where   
                                s.staff_id in
                                (Select
                                staff_id from
                                (SELECT COURSE_ID,STAFF_ID
                                FROM COURSE
                                GROUP BY STAFF_ID
                                HAVING COUNT(COURSE.COURSE_ID) >=2)
                                );


                                or via join infact even in join you require a subquery reason being group by when grouping by s.staff_id the c.course_id values will become inconsistent and unsync with s.staff_id



                                           Select s.* ,c.COURSE_ID from staff s
                                join course c
                                on s.staff_id IN (Select c.staff_id group by
                                c.staff_id
                                HAVING COUNT(*) >=2)





                                share|improve this answer






























                                  0














                                  If you want only staff details or else can join if needed course details as well



                                        SELECT Staff.* from Staff s where   
                                  s.staff_id in
                                  (Select
                                  staff_id from
                                  (SELECT COURSE_ID,STAFF_ID
                                  FROM COURSE
                                  GROUP BY STAFF_ID
                                  HAVING COUNT(COURSE.COURSE_ID) >=2)
                                  );


                                  or via join infact even in join you require a subquery reason being group by when grouping by s.staff_id the c.course_id values will become inconsistent and unsync with s.staff_id



                                             Select s.* ,c.COURSE_ID from staff s
                                  join course c
                                  on s.staff_id IN (Select c.staff_id group by
                                  c.staff_id
                                  HAVING COUNT(*) >=2)





                                  share|improve this answer




























                                    0












                                    0








                                    0







                                    If you want only staff details or else can join if needed course details as well



                                          SELECT Staff.* from Staff s where   
                                    s.staff_id in
                                    (Select
                                    staff_id from
                                    (SELECT COURSE_ID,STAFF_ID
                                    FROM COURSE
                                    GROUP BY STAFF_ID
                                    HAVING COUNT(COURSE.COURSE_ID) >=2)
                                    );


                                    or via join infact even in join you require a subquery reason being group by when grouping by s.staff_id the c.course_id values will become inconsistent and unsync with s.staff_id



                                               Select s.* ,c.COURSE_ID from staff s
                                    join course c
                                    on s.staff_id IN (Select c.staff_id group by
                                    c.staff_id
                                    HAVING COUNT(*) >=2)





                                    share|improve this answer















                                    If you want only staff details or else can join if needed course details as well



                                          SELECT Staff.* from Staff s where   
                                    s.staff_id in
                                    (Select
                                    staff_id from
                                    (SELECT COURSE_ID,STAFF_ID
                                    FROM COURSE
                                    GROUP BY STAFF_ID
                                    HAVING COUNT(COURSE.COURSE_ID) >=2)
                                    );


                                    or via join infact even in join you require a subquery reason being group by when grouping by s.staff_id the c.course_id values will become inconsistent and unsync with s.staff_id



                                               Select s.* ,c.COURSE_ID from staff s
                                    join course c
                                    on s.staff_id IN (Select c.staff_id group by
                                    c.staff_id
                                    HAVING COUNT(*) >=2)






                                    share|improve this answer














                                    share|improve this answer



                                    share|improve this answer








                                    edited Jan 1 at 22:51

























                                    answered Jan 1 at 22:34









                                    Himanshu AhujaHimanshu Ahuja

                                    8492217




                                    8492217






























                                        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.




                                        draft saved


                                        draft discarded














                                        StackExchange.ready(
                                        function () {
                                        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53999171%2fcorrelated-query-with-more-than-one-table-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







                                        Popular posts from this blog

                                        Angular Downloading a file using contenturl with Basic Authentication

                                        Olmecas

                                        Can't read property showImagePicker of undefined in react native iOS