Search code examples
sqlsql-serversql-server-2008sql-server-2014sql-server-2014-express

SQL Query Display the names of the employees and their corresponding department


The following are my CREATE TABLE scripts:

CREATE TABLE Person 
( 
   ID INT Primary Key Identity (1,1), 
   LastName nVarchar (20) not NULL, 
   FirstName nVarchar (20) not NULL, 
   MiddleName nVarchar (20), 
   BirthDate DateTime not NULL, 
   Age INT not NULL, 
   Check (Age18) 
); 

CREATE TABLE Department 
( 
   ID INT Primary Key Identity (1,1), 
   DepartmentName nVarchar (50) Unique, 
   DepartmentCode nVarchar (20) Unique, 
   IsActive Bit Default (1) 
); 

CREATE TABLE Employee 
( 
   ID INT Primary Key Identity (1,1), 
   PersonId INT Foreign Key REFERENCES Person, 
   DepartmentId INT Foreign Key REFERENCES Department, 
   Salary Decimal (18,2), 
   Check (Salary10000), 
   IsActive Bit Default (1) 
); 

The following are my INSERT scripts:

INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Dela Cruz','Juan',NULL,01/01/199,22) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Dela Cerna','Pedro','Juan',11/01/1993,21) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Villaflores','Rachel','Diacoma',10/7/1990,24) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Abendan','Marnell',NULL,03/15/1989,25) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Oplado','Aiza','Tapayan',11/18/1993,21) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Loreto','Desire','Talingting',06/10/1993,21) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Magbanua','Prince Laurence','Rallos',05/25/1992,22) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Locsin','Franz Cyril',NULL,02/14/1993,21) 
INSERT INTO Person (LastName, FirstName, MiddleName,BirthDate,Age) 
VALUES ('Dela Pena','Precious',NULL,01/01/199,21) 

 INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('ITDepartment','ItDept',1) 
INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('EngineeringDepartment','EDept',1) 
INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('ComputerScienceDepartment','CSDept',1) 
INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('InformationSystemDepartment','ISDept',1) 
INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('BusinessAdministrationDepartment','BADept',1) 
INSERT INTO Department (DepartmentName, DepartmentCode, IsActive) 
VALUES ('ElementaryDepartment','ElemDept',1) 

 INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (1,2,12000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (2,4,10001,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (3,6,13000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (4,1,25000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (5,3,15000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (6,5,10002,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (7,1,56000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (8,4,14000,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (9,6,15900,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (2,4,12300,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (4,1,13500,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (6,3,14300,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (8,5,12500,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (1,2,11460,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (3,4,10910,1) 
INSERT INTO Employee (PersonId,DepartmentId,Salary,IsActive) 
VALUES (5,6,10001,1) 

The following is my select script:

SELECT 
    Person.LastName, Department.DepartmentName 
FROM 
    Person, Department 
FULL OUTER JOIN 
    Employee ON Employee.PersonId = Employee.DepartmentId 

This is the result which is not right. I don't know what is the right thing for this. This is my first time doing JOINS.

Dela Cruz   BusinessAdministrationDepartment 
Dela Cerna  BusinessAdministrationDepartment 
Villaflores BusinessAdministrationDepartment 
Abendan BusinessAdministrationDepartment 
Oplado  BusinessAdministrationDepartment 
Loreto  BusinessAdministrationDepartment 
Magbanua    BusinessAdministrationDepartment 
Locsin  BusinessAdministrationDepartment 
Dela Pena   BusinessAdministrationDepartment 
Dela Cruz   ComputerScienceDepartment 
Dela Cerna  ComputerScienceDepartment 
Villaflores ComputerScienceDepartment 
Abendan ComputerScienceDepartment 
Oplado  ComputerScienceDepartment 
Loreto  ComputerScienceDepartment 
Magbanua    ComputerScienceDepartment 
Locsin  ComputerScienceDepartment 
Dela Pena   ComputerScienceDepartment 

I stopped at computer science department because it will be too long. How do I get this right? Please help. Thanks! :)


Solution

  • are you looking for this:-

    Select  p.LastName, d.DepartmentName
    From    Employee As e
            Join Person As p On e.PersonId = p.Id
            JOin Department As d On e.DepartmentId = d.ID