We help IT Professionals succeed at work.

Query two SQl 2000 tables and find out which value is not in a particular table

I am trying to query two SQl 2000 tables and find out which value is not in a particular table.

The first table is called docsadm.people and contains columns named full_name and system_id

The second table is called docsadm.profile and contains columns named typist and author.

The docsadm.profile.typist and docsadm.profile.author columns have numeric values and reflect the values listed in the docsadm.people.system_id column.

I am trying to write a query that will tell me which docsadm.profile.typist and docsadm.profile.author value is not listed in the docsadm.people.system_id column.

Thanks for your assistance.  Frank -
Comment
Watch Question

Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Something like this should do it:

SELECT prof.typist, 'Typist not in People'
FROM docsadm.profile prof
LEFT OUTER JOIN docsadm.people peop ON peop.system_id = prof.typist
UNION ALL
SELECT auth.author, 'Author not in People'
FROM docsadm.profile auth
LEFT OUTER JOIN docsadm.people peop ON peop.system_id = auth.author
ORDER BY 1

Author

Commented:
It looks like it might of returned the correct results, how can I set the results to return distinct values. It returned over 1 million rows.

Thanks

Author

Commented:
There are only 330 authors and 406 typists...
Scott PletcherSenior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019

Commented:
Oops, sorry, use UNION instead of UNION ALL:

SELECT ...
UNION
SELECT ...
ORDER BY ...

Author

Commented:
I think we're getting a little closer. It only returned 736 rows but the data is not correct. I know the query should not return more than ten rows.

The query returns two columns, typist  and (No column name) which holds the 'Author not in People' and 'Typist not in People' values.

The typist column should be the system_id column and  should contain values where the docsadm.people.system_id column data is not in the docsadm.profile.author or docsadm.profile.typist column.

Hopefully I didn;'t confuse you.

Thanks...
Senior DBA
CERTIFIED EXPERT
Most Valuable Expert 2018
Distinguished Expert 2019
Commented:
D'OH, VERY SORRY, I left the WHERE conditions off my queries.


SELECT prof.typist, 'Typist not in People'
FROM docsadm.profile prof
LEFT OUTER JOIN docsadm.people peop ON peop.system_id = prof.typist
WHERE peop.system_id IS NULL
UNION ALL
SELECT auth.author, 'Author not in People'
FROM docsadm.profile auth
LEFT OUTER JOIN docsadm.people peop ON peop.system_id = auth.author
WHERE auth.system_id IS NULL
ORDER BY 1

Author

Commented:
No problem, it returned the correct results.

Thanks much for the speedy resolution, excellent job!

 - Frank-

Explore More ContentExplore courses, solutions, and other research materials related to this topic.