USing Join Information to get the information from the table
I am new to the SQL. I am learning joins in query so that i eterive the information from the table using information. I am working in this question .
Get the EmployeeNumber for the employees and the date they started any given department.
Below is my table definition :
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 added the information by inserting the data accordingly to the table.
For the question .
I have written this answer but i am not sure wether it is right or not.
SELECT e.EmployeeNumber,
d.DateStarted
FROM Employees e
Right
Join DepartmentEmployees d
on e.EmployeeID= d.DepartmentEmployeeID;
sql
add a comment |
I am new to the SQL. I am learning joins in query so that i eterive the information from the table using information. I am working in this question .
Get the EmployeeNumber for the employees and the date they started any given department.
Below is my table definition :
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 added the information by inserting the data accordingly to the table.
For the question .
I have written this answer but i am not sure wether it is right or not.
SELECT e.EmployeeNumber,
d.DateStarted
FROM Employees e
Right
Join DepartmentEmployees d
on e.EmployeeID= d.DepartmentEmployeeID;
sql
So, does your query ends up with the desired result? And also, why don't you just use an inner join asJoin
instead ofRight Join
?
– vahdet
Nov 14 '18 at 6:31
2
Don't even consider RIGHT JOIN until you fully understand LEFT JOIN! It's much easier to understandmain table left join optional data
, instead ofoptional data right join main table
.
– jarlh
Nov 14 '18 at 7:15
add a comment |
I am new to the SQL. I am learning joins in query so that i eterive the information from the table using information. I am working in this question .
Get the EmployeeNumber for the employees and the date they started any given department.
Below is my table definition :
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 added the information by inserting the data accordingly to the table.
For the question .
I have written this answer but i am not sure wether it is right or not.
SELECT e.EmployeeNumber,
d.DateStarted
FROM Employees e
Right
Join DepartmentEmployees d
on e.EmployeeID= d.DepartmentEmployeeID;
sql
I am new to the SQL. I am learning joins in query so that i eterive the information from the table using information. I am working in this question .
Get the EmployeeNumber for the employees and the date they started any given department.
Below is my table definition :
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 added the information by inserting the data accordingly to the table.
For the question .
I have written this answer but i am not sure wether it is right or not.
SELECT e.EmployeeNumber,
d.DateStarted
FROM Employees e
Right
Join DepartmentEmployees d
on e.EmployeeID= d.DepartmentEmployeeID;
sql
sql
edited Nov 14 '18 at 8:57
Shaili
618826
618826
asked Nov 14 '18 at 6:27
jennajenna
124
124
So, does your query ends up with the desired result? And also, why don't you just use an inner join asJoin
instead ofRight Join
?
– vahdet
Nov 14 '18 at 6:31
2
Don't even consider RIGHT JOIN until you fully understand LEFT JOIN! It's much easier to understandmain table left join optional data
, instead ofoptional data right join main table
.
– jarlh
Nov 14 '18 at 7:15
add a comment |
So, does your query ends up with the desired result? And also, why don't you just use an inner join asJoin
instead ofRight Join
?
– vahdet
Nov 14 '18 at 6:31
2
Don't even consider RIGHT JOIN until you fully understand LEFT JOIN! It's much easier to understandmain table left join optional data
, instead ofoptional data right join main table
.
– jarlh
Nov 14 '18 at 7:15
So, does your query ends up with the desired result? And also, why don't you just use an inner join as
Join
instead of Right Join
?– vahdet
Nov 14 '18 at 6:31
So, does your query ends up with the desired result? And also, why don't you just use an inner join as
Join
instead of Right Join
?– vahdet
Nov 14 '18 at 6:31
2
2
Don't even consider RIGHT JOIN until you fully understand LEFT JOIN! It's much easier to understand
main table left join optional data
, instead of optional data right join main table
.– jarlh
Nov 14 '18 at 7:15
Don't even consider RIGHT JOIN until you fully understand LEFT JOIN! It's much easier to understand
main table left join optional data
, instead of optional data right join main table
.– jarlh
Nov 14 '18 at 7:15
add a comment |
1 Answer
1
active
oldest
votes
DepartmentEmployeeID is the primary key for the table department, you need to join the employee id's in both tables.
SELECT E.EmployeeNumber, D.DateStarted
FROM DepartmentEmployees D
LEFT JOIN Employees E
ON E.EmployeeID= D.EmployeeID;
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%2f53294275%2fusing-join-information-to-get-the-information-from-the-table%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
DepartmentEmployeeID is the primary key for the table department, you need to join the employee id's in both tables.
SELECT E.EmployeeNumber, D.DateStarted
FROM DepartmentEmployees D
LEFT JOIN Employees E
ON E.EmployeeID= D.EmployeeID;
add a comment |
DepartmentEmployeeID is the primary key for the table department, you need to join the employee id's in both tables.
SELECT E.EmployeeNumber, D.DateStarted
FROM DepartmentEmployees D
LEFT JOIN Employees E
ON E.EmployeeID= D.EmployeeID;
add a comment |
DepartmentEmployeeID is the primary key for the table department, you need to join the employee id's in both tables.
SELECT E.EmployeeNumber, D.DateStarted
FROM DepartmentEmployees D
LEFT JOIN Employees E
ON E.EmployeeID= D.EmployeeID;
DepartmentEmployeeID is the primary key for the table department, you need to join the employee id's in both tables.
SELECT E.EmployeeNumber, D.DateStarted
FROM DepartmentEmployees D
LEFT JOIN Employees E
ON E.EmployeeID= D.EmployeeID;
answered Nov 14 '18 at 6:36
Ajan BalakumaranAjan Balakumaran
671310
671310
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%2f53294275%2fusing-join-information-to-get-the-information-from-the-table%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
So, does your query ends up with the desired result? And also, why don't you just use an inner join as
Join
instead ofRight Join
?– vahdet
Nov 14 '18 at 6:31
2
Don't even consider RIGHT JOIN until you fully understand LEFT JOIN! It's much easier to understand
main table left join optional data
, instead ofoptional data right join main table
.– jarlh
Nov 14 '18 at 7:15