USing Join Information to get the information from the table












0















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;









share|improve this question

























  • 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





    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
















0















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;









share|improve this question

























  • 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





    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














0












0








0








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;









share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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 as Join instead of Right 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 of optional 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






  • 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

















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












1 Answer
1






active

oldest

votes


















0














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;





share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    0














    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;





    share|improve this answer




























      0














      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;





      share|improve this answer


























        0












        0








        0







        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;





        share|improve this answer













        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;






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 14 '18 at 6:36









        Ajan BalakumaranAjan Balakumaran

        671310




        671310






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Bressuire

            Vorschmack

            Quarantine