The following SQL Server Not Exists query will find the Employees whose Occupation is neither Skilled Manual nor Clerical.

SELECT 
	E1.EmpID
       ,E1.Name
       ,E1.Occupation
FROM 
	Employee E1
WHERE NOT EXISTS
	( 
    		SELECT 
			* 
		FROM 
			Employee E2 
		WHERE 
			E1.EmpID = E2.EmpID 
			AND 
			Occupation IN ('Skilled Manual', 'Clerical')
	)

Note: Using NOT EXISTS may have a performance hit. Try to use LEFT JOIN and WHERE column is NULL to test if there is a performance improvement. Sometimes, NOT EXISTS may be faster, and sometimes it may be slower.

	SELECT 
		TOP (1000) *
	FROM 
	dbo.tbTable01 T1  (NOLOCK)
	INNER JOIN dbo.tbTable02 T2  (NOLOCK) ON T2.Id  = T1.Id
	--LEFT JOIN tbTable03 T3  (NOLOCK) ON T3.PlateId = T1.PlateId
	--WHERE T3.BiImagePlateId IS NULL
	WHERE 
		NOT EXISTS(SELECT TOP 1 1 FROM tbTable03 WHERE PlateId = T1.PlateId)
	ORDER BY 
		T2.TrxnTime

Sources:

https://www.tutorialgateway.org/sql-not-exists-operator/

Last modified: October 29, 2021

Author

Comments

Write a Reply or Comment