Search code examples
sqlsql-servert-sqlcountwindow-functions

Difference between "exists", and "in" in SQL


This is my query

select nom, prenom, mail ,trig 
from dbo.reseau
where  nom, prenom, mail, trig in  
(
    select mail, nom, prenom, mail, trig
    from dbo.reseau
    group by mail, nom, prenom, mail, trig
    having count(1) > 1
)

This query don't work they tell me that I have to use exists. I used id, and it doesn't work.


Solution

  • EXISTS: Specifies a subquery to test for the existence of rows.

    In other words... Does this subquery return any rows?

    IN: Determines whether a specified value matches any value in a subquery or a list.

    In other words... Hey, get me the the first and last name of employees whose Job Title is 'Design Engineer', 'Tool Designer', 'Marketing Assistant' code snippet from link below.

    SELECT p.FirstName, p.LastName, e.JobTitle  
    FROM Person.Person AS p  
    JOIN HumanResources.Employee AS e  
        ON p.BusinessEntityID = e.BusinessEntityID  
    WHERE e.JobTitle IN ('Design Engineer', 'Tool Designer', 'Marketing Assistant');  
    GO  
    

    Further reading

    MS DOCS EXISTS

    MS DOCS IN