SQL查询显示员工姓名及对应部门
SQL Query Display the names of the employees and their corresponding department
以下是我的 CREATE TABLE
脚本:
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)
);
以下是我的 INSERT
脚本:
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)
以下是我的select脚本:
SELECT
Person.LastName, Department.DepartmentName
FROM
Person, Department
FULL OUTER JOIN
Employee ON Employee.PersonId = Employee.DepartmentId
这是不正确的结果。我不知道什么是正确的。这是我第一次做 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
我在计算机科学系停了下来,因为它太长了。我该如何做对?请帮忙。谢谢! :)
你在找这个吗:-
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
SELECT Person.LastName, Department.DepartmentName
FROM Employee INNER JOIN Person
ON Employee.PersonId = Person.ID
INNER JOIN Department
ON Employee.DepartmentId = Department.ID
以下是我的 CREATE TABLE
脚本:
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)
);
以下是我的 INSERT
脚本:
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)
以下是我的select脚本:
SELECT
Person.LastName, Department.DepartmentName
FROM
Person, Department
FULL OUTER JOIN
Employee ON Employee.PersonId = Employee.DepartmentId
这是不正确的结果。我不知道什么是正确的。这是我第一次做 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
我在计算机科学系停了下来,因为它太长了。我该如何做对?请帮忙。谢谢! :)
你在找这个吗:-
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
SELECT Person.LastName, Department.DepartmentName
FROM Employee INNER JOIN Person
ON Employee.PersonId = Person.ID
INNER JOIN Department
ON Employee.DepartmentId = Department.ID