Struggling with SQL query
I'm fairly new to SQL and I'm working on an assignement for my Uni database course.
The request is to find the name of the employees that earn the minimum wage for each of the departments (jobs) in my database.
The EMPLOYEES table contains name, code, job and wage for every employee.
This is the query I've written so far, and while it gives me all the right names, it throws in some more that shouldn't be there. My idea was to catch the minimum wage for each job (with the subquery, which actually seems to work fine), and then join that with the full EMPLOYEES table, to grab the names aswell. What am I doing wrong?
SELECT E.EMP_NAME
FROM EMPLOYEES AS E
INNER JOIN (SELECT MIN(WAGE)AS W
FROM EMPLOYEES
GROUP BY JOB)AS EMP
ON E.WAGE=EMP.W
ORDER BY E.JOB;
mysql sql join select min
add a comment |
I'm fairly new to SQL and I'm working on an assignement for my Uni database course.
The request is to find the name of the employees that earn the minimum wage for each of the departments (jobs) in my database.
The EMPLOYEES table contains name, code, job and wage for every employee.
This is the query I've written so far, and while it gives me all the right names, it throws in some more that shouldn't be there. My idea was to catch the minimum wage for each job (with the subquery, which actually seems to work fine), and then join that with the full EMPLOYEES table, to grab the names aswell. What am I doing wrong?
SELECT E.EMP_NAME
FROM EMPLOYEES AS E
INNER JOIN (SELECT MIN(WAGE)AS W
FROM EMPLOYEES
GROUP BY JOB)AS EMP
ON E.WAGE=EMP.W
ORDER BY E.JOB;
mysql sql join select min
Hi. For the future for code questions please act on Minimal, Complete, and Verifiable example.
– philipxy
Nov 17 '18 at 18:32
add a comment |
I'm fairly new to SQL and I'm working on an assignement for my Uni database course.
The request is to find the name of the employees that earn the minimum wage for each of the departments (jobs) in my database.
The EMPLOYEES table contains name, code, job and wage for every employee.
This is the query I've written so far, and while it gives me all the right names, it throws in some more that shouldn't be there. My idea was to catch the minimum wage for each job (with the subquery, which actually seems to work fine), and then join that with the full EMPLOYEES table, to grab the names aswell. What am I doing wrong?
SELECT E.EMP_NAME
FROM EMPLOYEES AS E
INNER JOIN (SELECT MIN(WAGE)AS W
FROM EMPLOYEES
GROUP BY JOB)AS EMP
ON E.WAGE=EMP.W
ORDER BY E.JOB;
mysql sql join select min
I'm fairly new to SQL and I'm working on an assignement for my Uni database course.
The request is to find the name of the employees that earn the minimum wage for each of the departments (jobs) in my database.
The EMPLOYEES table contains name, code, job and wage for every employee.
This is the query I've written so far, and while it gives me all the right names, it throws in some more that shouldn't be there. My idea was to catch the minimum wage for each job (with the subquery, which actually seems to work fine), and then join that with the full EMPLOYEES table, to grab the names aswell. What am I doing wrong?
SELECT E.EMP_NAME
FROM EMPLOYEES AS E
INNER JOIN (SELECT MIN(WAGE)AS W
FROM EMPLOYEES
GROUP BY JOB)AS EMP
ON E.WAGE=EMP.W
ORDER BY E.JOB;
mysql sql join select min
mysql sql join select min
asked Nov 17 '18 at 16:38
Adriano Cotta Ramusino
133
133
Hi. For the future for code questions please act on Minimal, Complete, and Verifiable example.
– philipxy
Nov 17 '18 at 18:32
add a comment |
Hi. For the future for code questions please act on Minimal, Complete, and Verifiable example.
– philipxy
Nov 17 '18 at 18:32
Hi. For the future for code questions please act on Minimal, Complete, and Verifiable example.
– philipxy
Nov 17 '18 at 18:32
Hi. For the future for code questions please act on Minimal, Complete, and Verifiable example.
– philipxy
Nov 17 '18 at 18:32
add a comment |
1 Answer
1
active
oldest
votes
You are only joining on the wage. You need to join on the job as well:
SELECT E.EMP_NAME
FROM EMPLOYEES E JOIN
(SELECT E2.JOB, MIN(E2.WAGE) AS MIN_WAGE
FROM EMPLOYEES E2
GROUP BY E2.JOB
) w
ON E.WAGE = W.MIN_WAGE AND E.JOB = W.JOB
ORDER BY E.JOB;
Otherwise, you will get employees that match the minimum for another job -- not what you want.
Some notes:
- You are learning SQL so you should give every table an alias and use it for all column references.
- You should give meaning aliases to columns.
Wis not very meaningful.
Personally, I would write this using a correlated subquery:
select e.*
from employees e
where e.wage = (select min(e2.wage) from employees e2 where e2.job = e.job);
I feel so dumb for not noticing that. Also, thank you a lot for the suggestion, I'm definitely trying to learn to write more efficient queries. I will try to go back on the ones I've already done and try to make them more similar to the one you wrote!
– Adriano Cotta Ramusino
Nov 17 '18 at 16:51
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%2f53353255%2fstruggling-with-sql-query%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You are only joining on the wage. You need to join on the job as well:
SELECT E.EMP_NAME
FROM EMPLOYEES E JOIN
(SELECT E2.JOB, MIN(E2.WAGE) AS MIN_WAGE
FROM EMPLOYEES E2
GROUP BY E2.JOB
) w
ON E.WAGE = W.MIN_WAGE AND E.JOB = W.JOB
ORDER BY E.JOB;
Otherwise, you will get employees that match the minimum for another job -- not what you want.
Some notes:
- You are learning SQL so you should give every table an alias and use it for all column references.
- You should give meaning aliases to columns.
Wis not very meaningful.
Personally, I would write this using a correlated subquery:
select e.*
from employees e
where e.wage = (select min(e2.wage) from employees e2 where e2.job = e.job);
I feel so dumb for not noticing that. Also, thank you a lot for the suggestion, I'm definitely trying to learn to write more efficient queries. I will try to go back on the ones I've already done and try to make them more similar to the one you wrote!
– Adriano Cotta Ramusino
Nov 17 '18 at 16:51
add a comment |
You are only joining on the wage. You need to join on the job as well:
SELECT E.EMP_NAME
FROM EMPLOYEES E JOIN
(SELECT E2.JOB, MIN(E2.WAGE) AS MIN_WAGE
FROM EMPLOYEES E2
GROUP BY E2.JOB
) w
ON E.WAGE = W.MIN_WAGE AND E.JOB = W.JOB
ORDER BY E.JOB;
Otherwise, you will get employees that match the minimum for another job -- not what you want.
Some notes:
- You are learning SQL so you should give every table an alias and use it for all column references.
- You should give meaning aliases to columns.
Wis not very meaningful.
Personally, I would write this using a correlated subquery:
select e.*
from employees e
where e.wage = (select min(e2.wage) from employees e2 where e2.job = e.job);
I feel so dumb for not noticing that. Also, thank you a lot for the suggestion, I'm definitely trying to learn to write more efficient queries. I will try to go back on the ones I've already done and try to make them more similar to the one you wrote!
– Adriano Cotta Ramusino
Nov 17 '18 at 16:51
add a comment |
You are only joining on the wage. You need to join on the job as well:
SELECT E.EMP_NAME
FROM EMPLOYEES E JOIN
(SELECT E2.JOB, MIN(E2.WAGE) AS MIN_WAGE
FROM EMPLOYEES E2
GROUP BY E2.JOB
) w
ON E.WAGE = W.MIN_WAGE AND E.JOB = W.JOB
ORDER BY E.JOB;
Otherwise, you will get employees that match the minimum for another job -- not what you want.
Some notes:
- You are learning SQL so you should give every table an alias and use it for all column references.
- You should give meaning aliases to columns.
Wis not very meaningful.
Personally, I would write this using a correlated subquery:
select e.*
from employees e
where e.wage = (select min(e2.wage) from employees e2 where e2.job = e.job);
You are only joining on the wage. You need to join on the job as well:
SELECT E.EMP_NAME
FROM EMPLOYEES E JOIN
(SELECT E2.JOB, MIN(E2.WAGE) AS MIN_WAGE
FROM EMPLOYEES E2
GROUP BY E2.JOB
) w
ON E.WAGE = W.MIN_WAGE AND E.JOB = W.JOB
ORDER BY E.JOB;
Otherwise, you will get employees that match the minimum for another job -- not what you want.
Some notes:
- You are learning SQL so you should give every table an alias and use it for all column references.
- You should give meaning aliases to columns.
Wis not very meaningful.
Personally, I would write this using a correlated subquery:
select e.*
from employees e
where e.wage = (select min(e2.wage) from employees e2 where e2.job = e.job);
answered Nov 17 '18 at 16:40
Gordon Linoff
760k35294399
760k35294399
I feel so dumb for not noticing that. Also, thank you a lot for the suggestion, I'm definitely trying to learn to write more efficient queries. I will try to go back on the ones I've already done and try to make them more similar to the one you wrote!
– Adriano Cotta Ramusino
Nov 17 '18 at 16:51
add a comment |
I feel so dumb for not noticing that. Also, thank you a lot for the suggestion, I'm definitely trying to learn to write more efficient queries. I will try to go back on the ones I've already done and try to make them more similar to the one you wrote!
– Adriano Cotta Ramusino
Nov 17 '18 at 16:51
I feel so dumb for not noticing that. Also, thank you a lot for the suggestion, I'm definitely trying to learn to write more efficient queries. I will try to go back on the ones I've already done and try to make them more similar to the one you wrote!
– Adriano Cotta Ramusino
Nov 17 '18 at 16:51
I feel so dumb for not noticing that. Also, thank you a lot for the suggestion, I'm definitely trying to learn to write more efficient queries. I will try to go back on the ones I've already done and try to make them more similar to the one you wrote!
– Adriano Cotta Ramusino
Nov 17 '18 at 16:51
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%2f53353255%2fstruggling-with-sql-query%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
Hi. For the future for code questions please act on Minimal, Complete, and Verifiable example.
– philipxy
Nov 17 '18 at 18:32