To display students who were not born in 2009
Expt. No.30
Page No.60
Not born in 2009 (NOT BETWEEN)
Aim
To display the names and dates of birth of students who were not born in 2009, using NOT BETWEEN.
Concepts used
Queries
USE school;SELECT name, dob FROM student WHERE dob NOT BETWEEN '2009-01-01' AND '2009-12-31';
A marked line continues the line above it. In your file, write it on the same line.
Table used
Table: student
+-----+-------+---------+--------+--------+------------+-------+ | rno | class | section | gender | name | dob | marks | +-----+-------+---------+--------+--------+------------+-------+ | 1 | XI | A | F | Aarohi | 2009-03-14 | 78 | | 2 | XI | A | M | Bharat | 2008-11-02 | 45 | | 3 | XI | B | M | Chetan | 2009-07-21 | NULL | | 4 | XI | B | F | Divya | 2009-01-30 | 91 | | 5 | XII | A | M | Eshan | 2008-05-17 | 66 | | 6 | XII | A | F | Fatima | 2008-09-09 | 83 | | 7 | XII | B | M | Gagan | 2007-12-25 | 38 | | 8 | XII | B | F | Hina | 2008-02-11 | NULL | | 9 | XI | A | M | Ishan | 2009-10-05 | 57 | | 10 | XII | A | F | Jaya | 2008-06-28 | 74 | +-----+-------+---------+--------+--------+------------+-------+
Output
mysql> USE school;
Database changed
mysql> SELECT name, dob FROM student WHERE dob NOT BETWEEN '2009-01-01' AND '2009-12-31';
+--------+------------+
| name | dob |
+--------+------------+
| Bharat | 2008-11-02 |
| Eshan | 2008-05-17 |
| Fatima | 2008-09-09 |
| Gagan | 2007-12-25 |
| Hina | 2008-02-11 |
| Jaya | 2008-06-28 |
+--------+------------+
6 rows in set
✓ Printed by running this query set, not typed by hand.
Result
Six students were not born in 2009: Bharat, Eshan, Fatima, Gagan, Hina and Jaya.
Viva voce: what the examiner may ask
01What does NOT do?
It reverses a condition: true becomes false and false becomes true.
02Together, how many rows do BETWEEN and NOT BETWEEN return for this table?
All 10, because every student has a date of birth (none is NULL).
03How else can this be written?
dob < '2009-01-01' OR dob > '2009-12-31'.
Variations for the lab test
- Display students whose marks are not between 40 and 80.
- Display students not in section A using NOT.
- Rewrite the query with < and >.
! Common mistake Writing
NOT dob BETWEEN ... also works, but dob NOT BETWEEN ... is clearer and is the form used in the syllabus.