MySQL IN, BETWEEN and IS NULL
IN: match a list
IN is a short way to write many OR conditions.
SELECT name, city FROM students WHERE city IN ('Delhi', 'Pune');
Result
| name | city |
|---|---|
| Robin | Delhi |
| John | Pune |
| Vikram | Delhi |
BETWEEN: match a range
BETWEEN includes both end values.
SELECT name, marks FROM students WHERE marks BETWEEN 70 AND 90;
Result
| name | marks |
|---|---|
| Robin | 85 |
| John | 78 |
| Asha | 88 |
Read Write a query to select students with marks between 70 and 80.
IS NULL: find empty values
NULL means “no value”. You cannot test it with =. Use IS NULL and IS NOT NULL.
SELECT name, course_id FROM students WHERE course_id IS NULL;
Result
| name | course_id |
|---|---|
| Vikram | NULL |
SELECT COUNT(*) AS with_course FROM students WHERE course_id IS NOT NULL;
Result
| with_course |
|---|
| 5 |
Note:
WHERE course_id = NULL never returns a row, because NULL is not equal to anything, not even to NULL. Read how to select if a column has no value.
Summary
IN (a, b, c)matches any value of the list.BETWEEN a AND bincludes a and b.- Use
IS NULLandIS NOT NULLfor empty values. - Next: UPDATE.
Congratulations! Chapter Finished. Learn more about the similar topics:
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |