Correlated Query with more than one table SQL
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
add a comment |
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
add a comment |
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
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
sql oracle correlated-subquery
edited Jan 1 at 22:33
user10776032
asked Jan 1 at 21:42
user10776032user10776032
83
83
add a comment |
add a comment |
4 Answers
4
active
oldest
votes
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
);
add a comment |
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.
add a comment |
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)
Ah I forgot I needed to select the course_id in the first part
– user10776032
Jan 1 at 22:01
add a comment |
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)
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%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
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
);
add a comment |
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
);
add a comment |
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
);
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
);
answered Jan 1 at 22:25
Gordon LinoffGordon Linoff
780k35310412
780k35310412
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Jan 1 at 21:54
answered Jan 1 at 21:48
GMBGMB
15.6k3824
15.6k3824
add a comment |
add a comment |
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)
Ah I forgot I needed to select the course_id in the first part
– user10776032
Jan 1 at 22:01
add a comment |
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)
Ah I forgot I needed to select the course_id in the first part
– user10776032
Jan 1 at 22:01
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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)
add a comment |
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)
add a comment |
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)
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)
edited Jan 1 at 22:51
answered Jan 1 at 22:34
Himanshu AhujaHimanshu Ahuja
8492217
8492217
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.
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%2f53999171%2fcorrelated-query-with-more-than-one-table-sql%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