Worked Examples · Example 9.8
Q.The following query selects details of all the employees except those working in department number D01 or D02.
Yanam CbseNCERTSubjective· 2mImportance★★★★★
16% · 15/95 Questions
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →NOT IN excludes rows whose column value is in the given list. Run against the chapter's EMPLOYEE table (Table 9.8), WHERE DeptId NOT IN ('D01','D02') returns the 5 employees who are in neither department.
The query
SELECT *
FROM EMPLOYEE
WHERE DeptId NOT IN ('D01', 'D02');
This is equivalent to WHERE DeptId != 'D01' AND DeptId != 'D02' — De Morgan's law turns "not (A or B)" into "(not A) and (not B)".
Checking every row of Table 9.8
| EmpNo | Ename | DeptId | Excluded (D01/D02)? | Selected? |
|---|---|---|---|---|
| 101 | Aaliya | D02 | yes | no |
| 102 | Kritika | D01 | yes | no |
| 103 | Shabbir | D01 | yes | no |
| 104 | Gurpreet | D04 | no | yes |
| 105 | Joseph | D03 | no | yes |
| 106 | Sanya | D02 | yes | no |
| 107 | Vergese | D01 | yes | no |
| 108 | Nachaobi | D05 | no | yes |
| 109 | Daribha | D04 | no | yes |
| 110 | Tanya | D05 | no | yes |
Output (5 rows):
+-------+----------+--------+-------+--------+
| EmpNo | Ename | Salary | Bonus | DeptId |
+-------+----------+--------+-------+--------+
| 104 | Gurpreet | 19000 | 565 | D04 |
| 105 | Joseph | 34000 | 875 | D03 |
| 108 | Nachaobi | 29000 | NULL | D05 |
| 109 | Daribha | 42000 | NULL | D04 | …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.