To display Rno, Name and DOB of students born between two dates
Expt. No.26
Page No.52
Born in 2009 (BETWEEN)
Aim
To display the roll number, name and date of birth of students born between '2009-01-01' and '2009-12-31'. (CBSE's list uses the year 2005; the year here is changed to match today's Class XI students.)
Concepts used
Queries
USE school;SELECT rno, name, dob FROM student WHERE dob 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 rno, name, dob FROM student WHERE dob BETWEEN '2009-01-01' AND '2009-12-31';
+-----+--------+------------+
| rno | name | dob |
+-----+--------+------------+
| 1 | Aarohi | 2009-03-14 |
| 3 | Chetan | 2009-07-21 |
| 4 | Divya | 2009-01-30 |
| 9 | Ishan | 2009-10-05 |
+-----+--------+------------+
4 rows in set
✓ Printed by running this query set, not typed by hand.
Result
Four students were born in 2009: Aarohi, Chetan, Divya and Ishan. BETWEEN includes both the first and the last date.
Viva voce: what the examiner may ask
01Is BETWEEN inclusive?
Yes. Both end values are included.
02How else can this condition be written?
dob >= '2009-01-01' AND dob <= '2009-12-31'.
03Why must the smaller value come first in BETWEEN?
BETWEEN '2009-12-31' AND '2009-01-01' matches nothing.
Variations for the lab test
- Display students born in 2008.
- Display students with marks between 60 and 80.
- Rewrite the query with >= and <=.
! Common mistake Writing the dates without quotes (
BETWEEN 2009-01-01 AND ...) makes MySQL subtract the numbers instead of reading dates.