Rewriting uncorrelated subquery to correlated subquery
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
add a comment |
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
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 aWHEREcaluse
– MartinKitty
Nov 12 at 11:46
add a comment |
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
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
sql oracle subquery correlated-subquery
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 aWHEREcaluse
– MartinKitty
Nov 12 at 11:46
add a comment |
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 aWHEREcaluse
– 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
add a comment |
2 Answers
2
active
oldest
votes
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
);
I forgot to mention, that in exercise instruction it was said to write correlated subquery inWHEREclause
– MartinKitty
Nov 12 at 11:43
@MartinKitty. . . You have a already done that task.
– Yogesh Sharma
Nov 12 at 11:46
I know, but acorrelated subqueryis required. I useduncorrelated subquery
– MartinKitty
Nov 12 at 11:48
Great! That is a solution, that i was looking for!
– MartinKitty
Nov 12 at 11:57
add a comment |
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;
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
);
I forgot to mention, that in exercise instruction it was said to write correlated subquery inWHEREclause
– MartinKitty
Nov 12 at 11:43
@MartinKitty. . . You have a already done that task.
– Yogesh Sharma
Nov 12 at 11:46
I know, but acorrelated subqueryis required. I useduncorrelated subquery
– MartinKitty
Nov 12 at 11:48
Great! That is a solution, that i was looking for!
– MartinKitty
Nov 12 at 11:57
add a comment |
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
);
I forgot to mention, that in exercise instruction it was said to write correlated subquery inWHEREclause
– MartinKitty
Nov 12 at 11:43
@MartinKitty. . . You have a already done that task.
– Yogesh Sharma
Nov 12 at 11:46
I know, but acorrelated subqueryis required. I useduncorrelated subquery
– MartinKitty
Nov 12 at 11:48
Great! That is a solution, that i was looking for!
– MartinKitty
Nov 12 at 11:57
add a comment |
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
);
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
);
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 inWHEREclause
– MartinKitty
Nov 12 at 11:43
@MartinKitty. . . You have a already done that task.
– Yogesh Sharma
Nov 12 at 11:46
I know, but acorrelated subqueryis required. I useduncorrelated subquery
– MartinKitty
Nov 12 at 11:48
Great! That is a solution, that i was looking for!
– MartinKitty
Nov 12 at 11:57
add a comment |
I forgot to mention, that in exercise instruction it was said to write correlated subquery inWHEREclause
– MartinKitty
Nov 12 at 11:43
@MartinKitty. . . You have a already done that task.
– Yogesh Sharma
Nov 12 at 11:46
I know, but acorrelated subqueryis required. I useduncorrelated 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
add a comment |
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;
add a comment |
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;
add a comment |
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;
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;
answered Nov 12 at 11:37
Gordon Linoff
755k35290397
755k35290397
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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
WHEREcaluse– MartinKitty
Nov 12 at 11:46