简体   繁体   中英

Joining two tables in SQL Server

I have two tables. One called Employee and the other called Departments

These are the two tables:

CREATE TABLE Departmenr (
    department_code NCHAR(4),
    department_name NVARCHAR(15),
    city NVARCHAR(20),
    budget MONEY
)

CREATE TABLE Employee (
    employee_id NCHAR(6), 
    name NVARCHAR(20), 
    position NVARCHAR(20),
    salary MONEY, 
    dcode NCHAR(3),
)

I have to write a statement that lists the name of each employee and name of the department they work in, for all employees with who have a salary over £20,000. This means I have to join the Employee and Department tables to get an output.

I thought it might be something like this:

SELECT Emplyee.name, Department.department_name
FROM Employee
FULL OUTER JOIN Department 
ON Employee.salary > 20000;

but it has errors. How do I do this?

Assuming dcode is a foreign key for table department you can do:

SELECT e.NAME,d.department_name
FROM Employee e
INNER JOIN Department d ON e.dcode = d.department_code
WHERE e.salary > 20000;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM