Rewriting uncorrelated subquery to correlated subquery












0














I am working with a default oracle scott database with additional table PROJECT, where there are two columns: projectno and empno.



I want to select names of employees with the highest salaries for each project.



I know how to do it with uncorrelated subquery:



SELECT p.projno, 
e.sal,
e.ename
FROM emp e
INNER
JOIN proj_emp p
ON e.empno = p.empno
WHERE (e.sal, p.projno)
IN (SELECT MAX(e.sal),
p.projno
FROM emp e INNER JOIN proj_emp p
ON e.empno = p.empno
GROUP BY p.projno)


However, i was asked to do it with a correlated subquery written in a WHERE clause, but i am wondering if it is possible?










share|improve this question
























  • Were you also asked to do this without window functions?
    – Salman A
    Nov 12 at 11:42










  • In an exercise instruction all what was said was to names of employees with the highest salaries for each project with a correlated subquery in a WHERE caluse
    – MartinKitty
    Nov 12 at 11:46
















0














I am working with a default oracle scott database with additional table PROJECT, where there are two columns: projectno and empno.



I want to select names of employees with the highest salaries for each project.



I know how to do it with uncorrelated subquery:



SELECT p.projno, 
e.sal,
e.ename
FROM emp e
INNER
JOIN proj_emp p
ON e.empno = p.empno
WHERE (e.sal, p.projno)
IN (SELECT MAX(e.sal),
p.projno
FROM emp e INNER JOIN proj_emp p
ON e.empno = p.empno
GROUP BY p.projno)


However, i was asked to do it with a correlated subquery written in a WHERE clause, but i am wondering if it is possible?










share|improve this question
























  • Were you also asked to do this without window functions?
    – Salman A
    Nov 12 at 11:42










  • In an exercise instruction all what was said was to names of employees with the highest salaries for each project with a correlated subquery in a WHERE caluse
    – MartinKitty
    Nov 12 at 11:46














0












0








0







I am working with a default oracle scott database with additional table PROJECT, where there are two columns: projectno and empno.



I want to select names of employees with the highest salaries for each project.



I know how to do it with uncorrelated subquery:



SELECT p.projno, 
e.sal,
e.ename
FROM emp e
INNER
JOIN proj_emp p
ON e.empno = p.empno
WHERE (e.sal, p.projno)
IN (SELECT MAX(e.sal),
p.projno
FROM emp e INNER JOIN proj_emp p
ON e.empno = p.empno
GROUP BY p.projno)


However, i was asked to do it with a correlated subquery written in a WHERE clause, but i am wondering if it is possible?










share|improve this question















I am working with a default oracle scott database with additional table PROJECT, where there are two columns: projectno and empno.



I want to select names of employees with the highest salaries for each project.



I know how to do it with uncorrelated subquery:



SELECT p.projno, 
e.sal,
e.ename
FROM emp e
INNER
JOIN proj_emp p
ON e.empno = p.empno
WHERE (e.sal, p.projno)
IN (SELECT MAX(e.sal),
p.projno
FROM emp e INNER JOIN proj_emp p
ON e.empno = p.empno
GROUP BY p.projno)


However, i was asked to do it with a correlated subquery written in a WHERE clause, but i am wondering if it is possible?







sql oracle subquery correlated-subquery






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 at 11:43

























asked Nov 12 at 11:35









MartinKitty

257




257












  • Were you also asked to do this without window functions?
    – Salman A
    Nov 12 at 11:42










  • In an exercise instruction all what was said was to names of employees with the highest salaries for each project with a correlated subquery in a WHERE caluse
    – MartinKitty
    Nov 12 at 11:46


















  • Were you also asked to do this without window functions?
    – Salman A
    Nov 12 at 11:42










  • In an exercise instruction all what was said was to names of employees with the highest salaries for each project with a correlated subquery in a WHERE caluse
    – MartinKitty
    Nov 12 at 11:46
















Were you also asked to do this without window functions?
– Salman A
Nov 12 at 11:42




Were you also asked to do this without window functions?
– Salman A
Nov 12 at 11:42












In an exercise instruction all what was said was to names of employees with the highest salaries for each project with a correlated subquery in a WHERE caluse
– MartinKitty
Nov 12 at 11:46




In an exercise instruction all what was said was to names of employees with the highest salaries for each project with a correlated subquery in a WHERE caluse
– MartinKitty
Nov 12 at 11:46












2 Answers
2






active

oldest

votes


















2














I would do :



SELECT t.*
FROM (SELECT p.projno, e.sal, e.ename,
DENSE_RANK() OVER (PARTITION BY p.projno ORDER BY e.sal DESC) AS Seq
FROM emp e INNER JOIN
proj_emp p
ON e.empno = p.empno
) t
WHERE Seq = 1;


EDIT : If you want to do it with correlated subquery then i would rewrite your query to make correlated :



SELECT p.projno, e.sal, e.ename
FROM emp e INNER JOIN
proj_emp p
ON e.empno = p.empno
WHERE e.sal = (SELECT MAX(e1.sal)
FROM emp e1 INNER JOIN
proj_emp p1
ON e1.empno = p1.empno
WHERE p1.projno = p.projno
);





share|improve this answer























  • I forgot to mention, that in exercise instruction it was said to write correlated subquery in WHERE clause
    – MartinKitty
    Nov 12 at 11:43












  • @MartinKitty. . . You have a already done that task.
    – Yogesh Sharma
    Nov 12 at 11:46










  • I know, but a correlated subquery is required. I used uncorrelated subquery
    – MartinKitty
    Nov 12 at 11:48










  • Great! That is a solution, that i was looking for!
    – MartinKitty
    Nov 12 at 11:57



















1














Use window functions:



SELECT projno, sal, ename
FROM (SELECT p.projno, e.sal, e.ename,
MAX(e.sal) OVER (PARTITION BY p.projno) as max_sal
FROM emp e INNER JOIN
proj_emp p
ON e.empno = p.empno
) ps
WHERE sal = max_sal;





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%2f53261327%2frewriting-uncorrelated-subquery-to-correlated-subquery%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    2














    I would do :



    SELECT t.*
    FROM (SELECT p.projno, e.sal, e.ename,
    DENSE_RANK() OVER (PARTITION BY p.projno ORDER BY e.sal DESC) AS Seq
    FROM emp e INNER JOIN
    proj_emp p
    ON e.empno = p.empno
    ) t
    WHERE Seq = 1;


    EDIT : If you want to do it with correlated subquery then i would rewrite your query to make correlated :



    SELECT p.projno, e.sal, e.ename
    FROM emp e INNER JOIN
    proj_emp p
    ON e.empno = p.empno
    WHERE e.sal = (SELECT MAX(e1.sal)
    FROM emp e1 INNER JOIN
    proj_emp p1
    ON e1.empno = p1.empno
    WHERE p1.projno = p.projno
    );





    share|improve this answer























    • I forgot to mention, that in exercise instruction it was said to write correlated subquery in WHERE clause
      – MartinKitty
      Nov 12 at 11:43












    • @MartinKitty. . . You have a already done that task.
      – Yogesh Sharma
      Nov 12 at 11:46










    • I know, but a correlated subquery is required. I used uncorrelated subquery
      – MartinKitty
      Nov 12 at 11:48










    • Great! That is a solution, that i was looking for!
      – MartinKitty
      Nov 12 at 11:57
















    2














    I would do :



    SELECT t.*
    FROM (SELECT p.projno, e.sal, e.ename,
    DENSE_RANK() OVER (PARTITION BY p.projno ORDER BY e.sal DESC) AS Seq
    FROM emp e INNER JOIN
    proj_emp p
    ON e.empno = p.empno
    ) t
    WHERE Seq = 1;


    EDIT : If you want to do it with correlated subquery then i would rewrite your query to make correlated :



    SELECT p.projno, e.sal, e.ename
    FROM emp e INNER JOIN
    proj_emp p
    ON e.empno = p.empno
    WHERE e.sal = (SELECT MAX(e1.sal)
    FROM emp e1 INNER JOIN
    proj_emp p1
    ON e1.empno = p1.empno
    WHERE p1.projno = p.projno
    );





    share|improve this answer























    • I forgot to mention, that in exercise instruction it was said to write correlated subquery in WHERE clause
      – MartinKitty
      Nov 12 at 11:43












    • @MartinKitty. . . You have a already done that task.
      – Yogesh Sharma
      Nov 12 at 11:46










    • I know, but a correlated subquery is required. I used uncorrelated subquery
      – MartinKitty
      Nov 12 at 11:48










    • Great! That is a solution, that i was looking for!
      – MartinKitty
      Nov 12 at 11:57














    2












    2








    2






    I would do :



    SELECT t.*
    FROM (SELECT p.projno, e.sal, e.ename,
    DENSE_RANK() OVER (PARTITION BY p.projno ORDER BY e.sal DESC) AS Seq
    FROM emp e INNER JOIN
    proj_emp p
    ON e.empno = p.empno
    ) t
    WHERE Seq = 1;


    EDIT : If you want to do it with correlated subquery then i would rewrite your query to make correlated :



    SELECT p.projno, e.sal, e.ename
    FROM emp e INNER JOIN
    proj_emp p
    ON e.empno = p.empno
    WHERE e.sal = (SELECT MAX(e1.sal)
    FROM emp e1 INNER JOIN
    proj_emp p1
    ON e1.empno = p1.empno
    WHERE p1.projno = p.projno
    );





    share|improve this answer














    I would do :



    SELECT t.*
    FROM (SELECT p.projno, e.sal, e.ename,
    DENSE_RANK() OVER (PARTITION BY p.projno ORDER BY e.sal DESC) AS Seq
    FROM emp e INNER JOIN
    proj_emp p
    ON e.empno = p.empno
    ) t
    WHERE Seq = 1;


    EDIT : If you want to do it with correlated subquery then i would rewrite your query to make correlated :



    SELECT p.projno, e.sal, e.ename
    FROM emp e INNER JOIN
    proj_emp p
    ON e.empno = p.empno
    WHERE e.sal = (SELECT MAX(e1.sal)
    FROM emp e1 INNER JOIN
    proj_emp p1
    ON e1.empno = p1.empno
    WHERE p1.projno = p.projno
    );






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 12 at 11:50

























    answered Nov 12 at 11:40









    Yogesh Sharma

    28k51335




    28k51335












    • I forgot to mention, that in exercise instruction it was said to write correlated subquery in WHERE clause
      – MartinKitty
      Nov 12 at 11:43












    • @MartinKitty. . . You have a already done that task.
      – Yogesh Sharma
      Nov 12 at 11:46










    • I know, but a correlated subquery is required. I used uncorrelated subquery
      – MartinKitty
      Nov 12 at 11:48










    • Great! That is a solution, that i was looking for!
      – MartinKitty
      Nov 12 at 11:57


















    • I forgot to mention, that in exercise instruction it was said to write correlated subquery in WHERE clause
      – MartinKitty
      Nov 12 at 11:43












    • @MartinKitty. . . You have a already done that task.
      – Yogesh Sharma
      Nov 12 at 11:46










    • I know, but a correlated subquery is required. I used uncorrelated subquery
      – MartinKitty
      Nov 12 at 11:48










    • Great! That is a solution, that i was looking for!
      – MartinKitty
      Nov 12 at 11:57
















    I forgot to mention, that in exercise instruction it was said to write correlated subquery in WHERE clause
    – MartinKitty
    Nov 12 at 11:43






    I forgot to mention, that in exercise instruction it was said to write correlated subquery in WHERE clause
    – MartinKitty
    Nov 12 at 11:43














    @MartinKitty. . . You have a already done that task.
    – Yogesh Sharma
    Nov 12 at 11:46




    @MartinKitty. . . You have a already done that task.
    – Yogesh Sharma
    Nov 12 at 11:46












    I know, but a correlated subquery is required. I used uncorrelated subquery
    – MartinKitty
    Nov 12 at 11:48




    I know, but a correlated subquery is required. I used uncorrelated subquery
    – MartinKitty
    Nov 12 at 11:48












    Great! That is a solution, that i was looking for!
    – MartinKitty
    Nov 12 at 11:57




    Great! That is a solution, that i was looking for!
    – MartinKitty
    Nov 12 at 11:57













    1














    Use window functions:



    SELECT projno, sal, ename
    FROM (SELECT p.projno, e.sal, e.ename,
    MAX(e.sal) OVER (PARTITION BY p.projno) as max_sal
    FROM emp e INNER JOIN
    proj_emp p
    ON e.empno = p.empno
    ) ps
    WHERE sal = max_sal;





    share|improve this answer


























      1














      Use window functions:



      SELECT projno, sal, ename
      FROM (SELECT p.projno, e.sal, e.ename,
      MAX(e.sal) OVER (PARTITION BY p.projno) as max_sal
      FROM emp e INNER JOIN
      proj_emp p
      ON e.empno = p.empno
      ) ps
      WHERE sal = max_sal;





      share|improve this answer
























        1












        1








        1






        Use window functions:



        SELECT projno, sal, ename
        FROM (SELECT p.projno, e.sal, e.ename,
        MAX(e.sal) OVER (PARTITION BY p.projno) as max_sal
        FROM emp e INNER JOIN
        proj_emp p
        ON e.empno = p.empno
        ) ps
        WHERE sal = max_sal;





        share|improve this answer












        Use window functions:



        SELECT projno, sal, ename
        FROM (SELECT p.projno, e.sal, e.ename,
        MAX(e.sal) OVER (PARTITION BY p.projno) as max_sal
        FROM emp e INNER JOIN
        proj_emp p
        ON e.empno = p.empno
        ) ps
        WHERE sal = max_sal;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 12 at 11:37









        Gordon Linoff

        755k35290397




        755k35290397






























            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%2f53261327%2frewriting-uncorrelated-subquery-to-correlated-subquery%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

            List item for chat from Array inside array React Native

            Thiostrepton

            Caerphilly