Removing Duplicates from Two SQL Columns

1
So the situation is as follows, I have a table and I have three fields in it, so I need to compare the a = b ) and find out if there is anyone else. What's the problem, I'm new in the middle of the sql and I'm not sure if I did it right, I tried the following ways.

SELECT DISTINCT [a]
               ,[b]
     FROM [Compara] -- 1 °




SELECT [a]
      ,[b]
     FROM [Compara]

WHERE [a] = [b] --2°

There was no return, so I do not know if I did it right or wrong, if someone can help me.

    
asked by anonymous 01.02.2018 / 13:43

1 answer

2

Follows:

SELECT CASE WHEN A = B THEN 'Coluna A igual a B' ELSE 'Coluna A diferente da B' END AS COMPARATIVO_01
  FROM TABELA
The SELECT above will return all the data in the table (since I did not use WHERE ), but in return, it will return if the records in column A are different from those in column B, compared to row.

Now if you just want to return the records where the A column is different from B , use the statement below:

SELECT *
  FROM TABELA
 WHERE A <> B;

Or to find out if they are the same:

SELECT *
  FROM TABELA
 WHERE A = B;

Edited

I put all three conditions to make clear to the user the possible possibilities of SQL.

Attention: As you said you are still learning SQL , be careful not to compare different columns of data. How to compare TRUE or FALSE with VARCHAR .

    
01.02.2018 / 13:55