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 b includes a and b.
  • Use IS NULL and IS NOT NULL for empty values.
  • Next: UPDATE.

Congratulations! Chapter Finished. Learn more about the similar topics:
Exercises & Assignments
Write a query to get data of selected columns from a table
Write a query to select the student with the maximum marks
Write a query to select top 3 students by marks
Write a query to select the rows if a column has no value
Write a query to find the names of students that begin with ‘A’
Write a query to fetch the top 2 records
Interview Questions & Answers
How to select Top 10 records in MySQL?
Write a MySQL query to select students with less than 50% marks
Write a MySQL query to select students with marks between 70 and 80
Write a query to fetch top N records
Write a query to find the names of employees that begin with ‘S’
Write a query to select if column has no/empty value
What is NULL in MySQL? Is NULL the same as 0 or an empty string?