简体   繁体   中英

How to display {null} all companies with no employees

You currently are maintaining a contact relation management system for an organization where they keep track of Organizations and Employees belonging to these organizations. You are asked to clean up their data and identify all organizations that don't have any employees attached to it.

You have two tables, Organizations and Employees. There is a one-to-many relationship from Organizations to Employees. Ie. An Organization can have many employees and an employee belongs to an organization.

Organizations: id Integer name String

Employees: id Integer name String organization_id Integer

Assignment: Write an SQL query that will produce a list of Companies that don't have any Employees attached to them.

I have tried:

Select Organisation,Organisation.ID, Organisation.Name 
From Organisation 
Left Join Employees On organisationID = Employees Employees.ID 
order by Organisation,Organisation.name 

Probably the point of this homework question is to get you using Joins, which are one of the key mechanisms for relating data in one table to the data in another.

Generally SQL has inner and outer joins, the difference being that an inner join requires records in each table to exist for the query to produce a result record. An outer join only requires that one table have an existant record.

A left join (left outer join) requires that the table to the left of the keyword join has records whereas a right join (right outer join) requires that the table to the right of the keyword join has records. A full outer join permits either left or right to have records.

If one side of a join doesn't have a record then the resulting record will have nulls for its column values, so if you have a not-null (such as primary key) column in your table you can always tell whether there was a record by filtering out any not-null values.

For example (assuming your Id columns are defined "not null", which is usually valid and if it isn't valid it's a problem with the design):

select
    *
  from Organizations
    left join Employees on
      Organizations.id = Employees.organization_id
  where Employees.organization_id is null
;

It fits the Join query where the organization_id will be exist in the Organization table but not exist (null in other word) in the Employee table.

Query: Write an SQL query that will produce a list of Companies that don't have any Employees attached to them.

SELECT O.id, O.Name
FROM Organization AS O
LEFT JOIN Employees AS E
  ON O.id=E.organization_id
WHERE E.organization_id IS NULL

Organizations: id Integer name String

Employees: id Integer name String organization_id Integer

You can use following query to get Organization without employees.

SELECT Org.id,Org.name from Organizations Org 
LEFT OUTER JOIN 
Employees Emp on Org.id=Emp.orgnization_id where Emp.id is null;

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