What is the purpose of the MINUS command in oracle?

5

I've never seen this command before and I also do not find it around here, I do not know if it has a different name and because of the Oracle documentation, I can not understand exactly how it works. I just want an explanation and a simple example of the command. Many thanks.

I would also like to know why the command below does not work (apparently it's because of b.NOM_CARRO).

SELECT b.COD_CARRO, b.NOM_CARRO FROM CARROS b
MINUS
SELECT a.COD_CARRO FROM VEICULOS a;
    
asked by anonymous 12.06.2015 / 20:12

3 answers

5

The Oracle MINUS command has the meaning of "exception". It is typically used to exclude data that is returned in a query. Do not confuse deleting data like DELETE. It's something quite different.

Taking your code as an example,

SELECT b.COD_CARRO, b.NOM_CARRO FROM CARROS b
MINUS
SELECT a.COD_CARRO FROM VEICULOS a;

Suppose that in the table "b" there is a record whose COD_CARRO is 1.

If there is COD_CARRO = 1 in table "a", this record is excluded from the SELECT result.

In case of selecting different columns, it is recommended to use the NOT EXISTS function.

In your case it would look like this:

SELECT b.COD_CARRO, b.NOM_CARRO FROM CARROS b
WHERE
NOT EXISTS
(
SELECT 0 FROM a 
WHERE 
a.COD_CARRO = b.COD_CARRO
);
    
12.06.2015 / 20:25
3

A succinct form is a "minus", the result of one query less than the other.

Example: Sellers who did not sell anything this month

Select codigo 
from vendedores
Minus
Select codigo_vendedor
From vendas
Where to_char(data_venda,'yyyymm') = to_char(sysdate,'yyyymm')
    
14.06.2015 / 00:11
1

According to documentation , the minus return all non-duplicate rows from the first query that is not in the second query, is similar to 'calculation' of difference in the theory of the set. In some cases it may be equivalent to select with WHERE NOT IN() .

    
12.06.2015 / 20:24