Advanced query using Join
I have created the following tables:
CREATE TABLE Employees
(
EmployeeID int IDENTITY(1,1) PRIMARY KEY
,EmployeeNumber int UNIQUE
,DateOfBirth datetime NOT NULL
,FirstName nvarchar(14) NOT NULL
,MiddleName nvarchar(14) NOT NULL
,LastName nvarchar(16) NOT NULL
,DateHired datetime NOT NULL
)
CREATE TABLE Customers
(
CustomerID int IDENTITY(1,1) PRIMARY KEY
,FirstName nvarchar(14) NOT NULL
,MiddleName nvarchar(14) NOT NULL
,LastName nvarchar(16) NOT NULL
,DateLastVisited datetime NOT NULL
,EmailAddress nvarchar(52) NOT NULL
)
CREATE TABLE Departments
(
DepartmentID int IDENTITY(1,1) PRIMARY KEY
,Code nchar(4) UNIQUE
,Name nvarchar(40) NOT NULL
)
CREATE TABLE DepartmentEmployees
(
DepartmentEmployeeID int IDENTITY(1,1) PRIMARY KEY
,EmployeeID int NOT NULL
CONSTRAINT Department_Employee REFERENCES Employees(EmployeeID)
,DepartmentID int NOT NULL
CONSTRAINT Employee_Department REFERENCES Departments(DepartmentID)
,DateStarted datetime NOT NULL
,DateEnded datetime NOT NULL
)
CREATE TABLE Salaries
(
SalaryID int IDENTITY(1,1) PRIMARY KEY
,EmployeeID int NOT NULL
CONSTRAINT Salaried_Employee REFERENCES Employees(EmployeeID)
,Amount money NOT NULL
,DateStarts datetime NOT NULL
,DateEnds datetime NOT NULL
)
I have inserted a data according to the table requirement.
I am working on this question: Get all employees first and last name, with a department code and a salary
I have written this query for the question but it is not working.
SELECT
E.FirstName, E.LastName, D.Code, S.Amount
FROM
Employees E
INNER JOIN
Salaries S ON E.EmployeeID = S.EmployeeID
INNER JOIN
Departments D ON D.DepartmentID = E.EmployeeID
Please help me out
sql
add a comment |
I have created the following tables:
CREATE TABLE Employees
(
EmployeeID int IDENTITY(1,1) PRIMARY KEY
,EmployeeNumber int UNIQUE
,DateOfBirth datetime NOT NULL
,FirstName nvarchar(14) NOT NULL
,MiddleName nvarchar(14) NOT NULL
,LastName nvarchar(16) NOT NULL
,DateHired datetime NOT NULL
)
CREATE TABLE Customers
(
CustomerID int IDENTITY(1,1) PRIMARY KEY
,FirstName nvarchar(14) NOT NULL
,MiddleName nvarchar(14) NOT NULL
,LastName nvarchar(16) NOT NULL
,DateLastVisited datetime NOT NULL
,EmailAddress nvarchar(52) NOT NULL
)
CREATE TABLE Departments
(
DepartmentID int IDENTITY(1,1) PRIMARY KEY
,Code nchar(4) UNIQUE
,Name nvarchar(40) NOT NULL
)
CREATE TABLE DepartmentEmployees
(
DepartmentEmployeeID int IDENTITY(1,1) PRIMARY KEY
,EmployeeID int NOT NULL
CONSTRAINT Department_Employee REFERENCES Employees(EmployeeID)
,DepartmentID int NOT NULL
CONSTRAINT Employee_Department REFERENCES Departments(DepartmentID)
,DateStarted datetime NOT NULL
,DateEnded datetime NOT NULL
)
CREATE TABLE Salaries
(
SalaryID int IDENTITY(1,1) PRIMARY KEY
,EmployeeID int NOT NULL
CONSTRAINT Salaried_Employee REFERENCES Employees(EmployeeID)
,Amount money NOT NULL
,DateStarts datetime NOT NULL
,DateEnds datetime NOT NULL
)
I have inserted a data according to the table requirement.
I am working on this question: Get all employees first and last name, with a department code and a salary
I have written this query for the question but it is not working.
SELECT
E.FirstName, E.LastName, D.Code, S.Amount
FROM
Employees E
INNER JOIN
Salaries S ON E.EmployeeID = S.EmployeeID
INNER JOIN
Departments D ON D.DepartmentID = E.EmployeeID
Please help me out
sql
1
There's no direct connection between employees and departments, you need to join Employees - DepartmentEmployees - Departments
– dnoeth
Nov 16 '18 at 20:54
1
When you say it's not working it's helpful to explain why. Not working could mean a number of things; an error, unexpected results, no results, causes your pc to catch fire (the last one is a jest, but you get the idea). Help us help you by explaining what's happening and what you expect to happen.
– Larnu
Nov 16 '18 at 20:59
@jenna your join is incorrect. CheckDepartmentEmployeestable, maybe it will help you figure out the answer.
– iSR5
Nov 16 '18 at 21:13
add a comment |
I have created the following tables:
CREATE TABLE Employees
(
EmployeeID int IDENTITY(1,1) PRIMARY KEY
,EmployeeNumber int UNIQUE
,DateOfBirth datetime NOT NULL
,FirstName nvarchar(14) NOT NULL
,MiddleName nvarchar(14) NOT NULL
,LastName nvarchar(16) NOT NULL
,DateHired datetime NOT NULL
)
CREATE TABLE Customers
(
CustomerID int IDENTITY(1,1) PRIMARY KEY
,FirstName nvarchar(14) NOT NULL
,MiddleName nvarchar(14) NOT NULL
,LastName nvarchar(16) NOT NULL
,DateLastVisited datetime NOT NULL
,EmailAddress nvarchar(52) NOT NULL
)
CREATE TABLE Departments
(
DepartmentID int IDENTITY(1,1) PRIMARY KEY
,Code nchar(4) UNIQUE
,Name nvarchar(40) NOT NULL
)
CREATE TABLE DepartmentEmployees
(
DepartmentEmployeeID int IDENTITY(1,1) PRIMARY KEY
,EmployeeID int NOT NULL
CONSTRAINT Department_Employee REFERENCES Employees(EmployeeID)
,DepartmentID int NOT NULL
CONSTRAINT Employee_Department REFERENCES Departments(DepartmentID)
,DateStarted datetime NOT NULL
,DateEnded datetime NOT NULL
)
CREATE TABLE Salaries
(
SalaryID int IDENTITY(1,1) PRIMARY KEY
,EmployeeID int NOT NULL
CONSTRAINT Salaried_Employee REFERENCES Employees(EmployeeID)
,Amount money NOT NULL
,DateStarts datetime NOT NULL
,DateEnds datetime NOT NULL
)
I have inserted a data according to the table requirement.
I am working on this question: Get all employees first and last name, with a department code and a salary
I have written this query for the question but it is not working.
SELECT
E.FirstName, E.LastName, D.Code, S.Amount
FROM
Employees E
INNER JOIN
Salaries S ON E.EmployeeID = S.EmployeeID
INNER JOIN
Departments D ON D.DepartmentID = E.EmployeeID
Please help me out
sql
I have created the following tables:
CREATE TABLE Employees
(
EmployeeID int IDENTITY(1,1) PRIMARY KEY
,EmployeeNumber int UNIQUE
,DateOfBirth datetime NOT NULL
,FirstName nvarchar(14) NOT NULL
,MiddleName nvarchar(14) NOT NULL
,LastName nvarchar(16) NOT NULL
,DateHired datetime NOT NULL
)
CREATE TABLE Customers
(
CustomerID int IDENTITY(1,1) PRIMARY KEY
,FirstName nvarchar(14) NOT NULL
,MiddleName nvarchar(14) NOT NULL
,LastName nvarchar(16) NOT NULL
,DateLastVisited datetime NOT NULL
,EmailAddress nvarchar(52) NOT NULL
)
CREATE TABLE Departments
(
DepartmentID int IDENTITY(1,1) PRIMARY KEY
,Code nchar(4) UNIQUE
,Name nvarchar(40) NOT NULL
)
CREATE TABLE DepartmentEmployees
(
DepartmentEmployeeID int IDENTITY(1,1) PRIMARY KEY
,EmployeeID int NOT NULL
CONSTRAINT Department_Employee REFERENCES Employees(EmployeeID)
,DepartmentID int NOT NULL
CONSTRAINT Employee_Department REFERENCES Departments(DepartmentID)
,DateStarted datetime NOT NULL
,DateEnded datetime NOT NULL
)
CREATE TABLE Salaries
(
SalaryID int IDENTITY(1,1) PRIMARY KEY
,EmployeeID int NOT NULL
CONSTRAINT Salaried_Employee REFERENCES Employees(EmployeeID)
,Amount money NOT NULL
,DateStarts datetime NOT NULL
,DateEnds datetime NOT NULL
)
I have inserted a data according to the table requirement.
I am working on this question: Get all employees first and last name, with a department code and a salary
I have written this query for the question but it is not working.
SELECT
E.FirstName, E.LastName, D.Code, S.Amount
FROM
Employees E
INNER JOIN
Salaries S ON E.EmployeeID = S.EmployeeID
INNER JOIN
Departments D ON D.DepartmentID = E.EmployeeID
Please help me out
sql
sql
edited Nov 16 '18 at 22:00
marc_s
570k12811031251
570k12811031251
asked Nov 16 '18 at 20:43
jenna
124
124
1
There's no direct connection between employees and departments, you need to join Employees - DepartmentEmployees - Departments
– dnoeth
Nov 16 '18 at 20:54
1
When you say it's not working it's helpful to explain why. Not working could mean a number of things; an error, unexpected results, no results, causes your pc to catch fire (the last one is a jest, but you get the idea). Help us help you by explaining what's happening and what you expect to happen.
– Larnu
Nov 16 '18 at 20:59
@jenna your join is incorrect. CheckDepartmentEmployeestable, maybe it will help you figure out the answer.
– iSR5
Nov 16 '18 at 21:13
add a comment |
1
There's no direct connection between employees and departments, you need to join Employees - DepartmentEmployees - Departments
– dnoeth
Nov 16 '18 at 20:54
1
When you say it's not working it's helpful to explain why. Not working could mean a number of things; an error, unexpected results, no results, causes your pc to catch fire (the last one is a jest, but you get the idea). Help us help you by explaining what's happening and what you expect to happen.
– Larnu
Nov 16 '18 at 20:59
@jenna your join is incorrect. CheckDepartmentEmployeestable, maybe it will help you figure out the answer.
– iSR5
Nov 16 '18 at 21:13
1
1
There's no direct connection between employees and departments, you need to join Employees - DepartmentEmployees - Departments
– dnoeth
Nov 16 '18 at 20:54
There's no direct connection between employees and departments, you need to join Employees - DepartmentEmployees - Departments
– dnoeth
Nov 16 '18 at 20:54
1
1
When you say it's not working it's helpful to explain why. Not working could mean a number of things; an error, unexpected results, no results, causes your pc to catch fire (the last one is a jest, but you get the idea). Help us help you by explaining what's happening and what you expect to happen.
– Larnu
Nov 16 '18 at 20:59
When you say it's not working it's helpful to explain why. Not working could mean a number of things; an error, unexpected results, no results, causes your pc to catch fire (the last one is a jest, but you get the idea). Help us help you by explaining what's happening and what you expect to happen.
– Larnu
Nov 16 '18 at 20:59
@jenna your join is incorrect. Check
DepartmentEmployees table, maybe it will help you figure out the answer.– iSR5
Nov 16 '18 at 21:13
@jenna your join is incorrect. Check
DepartmentEmployees table, maybe it will help you figure out the answer.– iSR5
Nov 16 '18 at 21:13
add a comment |
2 Answers
2
active
oldest
votes
SELECT E.FirstName,E.LastName, D.Code,S.Amount From Employees E
Inner Join Salaries S
On E.EmployeeID= S.EmployeeID
Inner join Departments D
**on D.DepartmentID= E.EmployeeID**
Are you sure you want to join with departmentID to EmployeeID?
1
You should ask questions in the question comments.
– Chris Albert
Nov 16 '18 at 21:04
Yep.. It looks like I need to earn more reputation to do that.
– Praveen Valavan
Nov 16 '18 at 21:06
add a comment |
This query will give you all you need
select firstname, lastname ,Departments.Code as DepartmentCode,Salaries.Amount as Salary
from
Employees
inner join DepartmentEmployees on Employees.EmployeeID = DepartmentEmployees.EmployeeID
inner join Departments on DepartmentEmployees.DepartmentID = Departments.DepartmentID
inner join Salaries on Employees.EmployeeID = Salaries.EmployeeID
Every Employee belongs to a department( DepartmentEmployees). Because of that you must join Employee with DepartmentEmployees. In next join you can find the code of Department and in 3rd join you find the salary amount from Salaries table.
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%2f53345117%2fadvanced-query-using-join%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
SELECT E.FirstName,E.LastName, D.Code,S.Amount From Employees E
Inner Join Salaries S
On E.EmployeeID= S.EmployeeID
Inner join Departments D
**on D.DepartmentID= E.EmployeeID**
Are you sure you want to join with departmentID to EmployeeID?
1
You should ask questions in the question comments.
– Chris Albert
Nov 16 '18 at 21:04
Yep.. It looks like I need to earn more reputation to do that.
– Praveen Valavan
Nov 16 '18 at 21:06
add a comment |
SELECT E.FirstName,E.LastName, D.Code,S.Amount From Employees E
Inner Join Salaries S
On E.EmployeeID= S.EmployeeID
Inner join Departments D
**on D.DepartmentID= E.EmployeeID**
Are you sure you want to join with departmentID to EmployeeID?
1
You should ask questions in the question comments.
– Chris Albert
Nov 16 '18 at 21:04
Yep.. It looks like I need to earn more reputation to do that.
– Praveen Valavan
Nov 16 '18 at 21:06
add a comment |
SELECT E.FirstName,E.LastName, D.Code,S.Amount From Employees E
Inner Join Salaries S
On E.EmployeeID= S.EmployeeID
Inner join Departments D
**on D.DepartmentID= E.EmployeeID**
Are you sure you want to join with departmentID to EmployeeID?
SELECT E.FirstName,E.LastName, D.Code,S.Amount From Employees E
Inner Join Salaries S
On E.EmployeeID= S.EmployeeID
Inner join Departments D
**on D.DepartmentID= E.EmployeeID**
Are you sure you want to join with departmentID to EmployeeID?
edited Nov 16 '18 at 21:30
tiagoperes
2,20521734
2,20521734
answered Nov 16 '18 at 21:01
Praveen Valavan
389
389
1
You should ask questions in the question comments.
– Chris Albert
Nov 16 '18 at 21:04
Yep.. It looks like I need to earn more reputation to do that.
– Praveen Valavan
Nov 16 '18 at 21:06
add a comment |
1
You should ask questions in the question comments.
– Chris Albert
Nov 16 '18 at 21:04
Yep.. It looks like I need to earn more reputation to do that.
– Praveen Valavan
Nov 16 '18 at 21:06
1
1
You should ask questions in the question comments.
– Chris Albert
Nov 16 '18 at 21:04
You should ask questions in the question comments.
– Chris Albert
Nov 16 '18 at 21:04
Yep.. It looks like I need to earn more reputation to do that.
– Praveen Valavan
Nov 16 '18 at 21:06
Yep.. It looks like I need to earn more reputation to do that.
– Praveen Valavan
Nov 16 '18 at 21:06
add a comment |
This query will give you all you need
select firstname, lastname ,Departments.Code as DepartmentCode,Salaries.Amount as Salary
from
Employees
inner join DepartmentEmployees on Employees.EmployeeID = DepartmentEmployees.EmployeeID
inner join Departments on DepartmentEmployees.DepartmentID = Departments.DepartmentID
inner join Salaries on Employees.EmployeeID = Salaries.EmployeeID
Every Employee belongs to a department( DepartmentEmployees). Because of that you must join Employee with DepartmentEmployees. In next join you can find the code of Department and in 3rd join you find the salary amount from Salaries table.
add a comment |
This query will give you all you need
select firstname, lastname ,Departments.Code as DepartmentCode,Salaries.Amount as Salary
from
Employees
inner join DepartmentEmployees on Employees.EmployeeID = DepartmentEmployees.EmployeeID
inner join Departments on DepartmentEmployees.DepartmentID = Departments.DepartmentID
inner join Salaries on Employees.EmployeeID = Salaries.EmployeeID
Every Employee belongs to a department( DepartmentEmployees). Because of that you must join Employee with DepartmentEmployees. In next join you can find the code of Department and in 3rd join you find the salary amount from Salaries table.
add a comment |
This query will give you all you need
select firstname, lastname ,Departments.Code as DepartmentCode,Salaries.Amount as Salary
from
Employees
inner join DepartmentEmployees on Employees.EmployeeID = DepartmentEmployees.EmployeeID
inner join Departments on DepartmentEmployees.DepartmentID = Departments.DepartmentID
inner join Salaries on Employees.EmployeeID = Salaries.EmployeeID
Every Employee belongs to a department( DepartmentEmployees). Because of that you must join Employee with DepartmentEmployees. In next join you can find the code of Department and in 3rd join you find the salary amount from Salaries table.
This query will give you all you need
select firstname, lastname ,Departments.Code as DepartmentCode,Salaries.Amount as Salary
from
Employees
inner join DepartmentEmployees on Employees.EmployeeID = DepartmentEmployees.EmployeeID
inner join Departments on DepartmentEmployees.DepartmentID = Departments.DepartmentID
inner join Salaries on Employees.EmployeeID = Salaries.EmployeeID
Every Employee belongs to a department( DepartmentEmployees). Because of that you must join Employee with DepartmentEmployees. In next join you can find the code of Department and in 3rd join you find the salary amount from Salaries table.
answered Nov 16 '18 at 23:44
Mehdi akbari
544
544
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%2f53345117%2fadvanced-query-using-join%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
1
There's no direct connection between employees and departments, you need to join Employees - DepartmentEmployees - Departments
– dnoeth
Nov 16 '18 at 20:54
1
When you say it's not working it's helpful to explain why. Not working could mean a number of things; an error, unexpected results, no results, causes your pc to catch fire (the last one is a jest, but you get the idea). Help us help you by explaining what's happening and what you expect to happen.
– Larnu
Nov 16 '18 at 20:59
@jenna your join is incorrect. Check
DepartmentEmployeestable, maybe it will help you figure out the answer.– iSR5
Nov 16 '18 at 21:13