Records without a corresponding key in another table

You want to find all the records in table A which don't have a corresponding record in table B.

Solution

Use NOT IN or a LEFT | RIGHT JOINref.

Discussion

If you've imported data and want to make sure the data matches between tables, you could use something like the following:

SELECT dy_id, dy_company from diary LEFT JOIN companies
ON dy_company=co_id WHERE co_id IS NULL;
   

This will select all diary entries whether or not there is a matching company entry and then filter out those that have matches.

Alternatively, you can use NOT IN

SELECT dy_id, dy_company from diary WHERE dy_company NOT IN
(SELECT co_id FROM companies);
   

Which one to use is a matter of taste and which you find most efficient (but bear in mind [not-in-nulls] with NULLs and NOT IN).