How to exclude records with certain values in sql select How to exclude records with certain values in sql select sql sql

How to exclude records with certain values in sql select


One way:

SELECT DISTINCT sc.StoreIdFROM StoreClients scWHERE NOT EXISTS(    SELECT * FROM StoreClients sc2     WHERE sc2.StoreId = sc.StoreId AND sc2.ClientId = 5)


SELECT SC.StoreId FROM StoreClients SCWHERE SC.StoreId NOT IN (SELECT StoreId FROM StoreClients WHERE ClientId = 5)

In this way neither JOIN nor GROUP BY is necessary.


SELECT  DISTINCT a.StoreIDFROM    tableName a        LEFT JOIN tableName b           ON a.StoreID = b.StoreID AND b.ClientID = 5WHERE   b.StoreID IS NULL

OUTPUT

╔═════════╗║ STOREID ║╠═════════╣║       3 ║╚═════════╝