1

I'm trying to get a count result using a SET operator in SQL Developer.

I have to find how many of "attribute1" are in "table_name1" but are not in "table_name2"

Essentially I want the result that I would get from the following query, but using a SET operator.

SELECT count(distinct <attribute1>)
FROM <table_name1>
WHERE <attribute1> IS NOT (SELECT <attribute1>
                           FROM <table_name2>);

Can anyone please help me?

user4824195
  • 113
  • 3
  • 11

3 Answers3

0

Please try below solution:

SELECT count(distinct <attribute1>)
FROM <table_name1>
WHERE <attribute1> NOT IN (SELECT <attribute1>
                           FROM <table_name2>);

I hope this will helpful to you.

chirag satapara
  • 1,947
  • 1
  • 15
  • 26
0

If you must use a set operator, then you can solve this using MINUS:

SELECT COUNT(*)                      -- use COUNT(DISTINCT attribute1) to avoid
FROM                                 -- duplicates
(
    SELECT attribute1
    FROM table_name1
    MINUS
    SELECT attribute1
    FROM table_name2
) t

However, I would probably use a LEFT JOIN here because it's conceptually simple:

SELECT COUNT(DISTINCT t1.attribute1) -- replace with COUNT(*) to count duplicates
FROM table_name1 t1
LEFT JOIN table_name2 t2
    ON t1.attribute1 = t2.attribute1
WHERE t2.attribute1 IS NULL          -- indicates that attribute does NOT appear in
                                     -- the second table
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0
SELECT COUNT(<attribute1>)
FROM <table_name1>
WHERE <attribute1> MINUS (SELECT <attribute1>
                           FROM <table_name2>);

https://docs.oracle.com/cd/B19306_01/server.102/b14200/operators005.htm

Updated answer

SELECT COUNT(X.id_num)
(SELECT id_num
FROM Tree 
WHERE id_num)
MINUS 
(SELECT id_num 
FROM Bird) AS X
Tharsan Sivakumar
  • 6,351
  • 3
  • 19
  • 28