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.

Sources:

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

Last modified: October 14, 2021

Author

Comments

Write a Reply or Comment