MySQL BETWEEN Operator

MySQL | BETWEEN Operator: Learn about the MySQL BETWEEN Operator, with its explanation, syntax, and query examples.
Submitted by Apurva Mathur, on September 13, 2022

BETWEEN Operator

BETWEEN Operator helps you to match some specific values of the column. It sounds confusing, but this operator aids us very much when it comes to write big queries, as we can even match some specific query inside a query, which sounds even more confusing. So in simple words, BETWEEN Operator helps us to match the values of the column.

BETWEEN Operator Syntax

SELECT column_name1, column_name2, ...
FROM table_name
WHERE column_name BETWEEN value1 AND value2;

Let us see some examples;

BETWEEN Operator Examples

Suppose you have tables named "student_details" and inside this table, you have the following columns;

BETWEEN Operator Example 1

Case 1: As you can see I have a column named "Marks", and I want the details of all those students who have scored "Marks" between 30- 80, in such case, we'll use.

SELECT * FROM student_details
WHERE Marks between 30 AND 80;
BETWEEN Operator Example 2

As you can see, we have got all the students who have a score from 30 - 80.

Case 2: Now, suppose we want to know the names of students from the "CSE" department and "ME" departments who have scores between 30-80, in such case, we will write;

SELECT * FROM student_details
WHERE Marks BETWEEN 30 AND 80 AND student_department IN('CSE','ME');
BETWEEN Operator Example 3

As you can see in the result, we have only got students' names from the "CSE" department, but the condition written was for both the departments "CSE" and "ME" then why is the result displayed only from the "CSE" department, this is because of the range we have applied, according to the range only these two students from "CSE" department fulfill the condition and other students of "ME" department does not fall in this range.

Case 3: Suppose we are asked to find out the list of names falls under 'Kiara' and 'Shantanu';

SELECT * FROM student_details 
WHERE student_name BETWEEN 'Kiara' AND 'Shantanu';
BETWEEN Operator Example 4

In such a case first we will sort the column named "student_name" and then we will apply the BETWEEN operator,


ADVERTISEMENT
ADVERTISEMENT




Comments and Discussions!




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.