SQL BETWEEN Command to fetch records from a range

SQL BETWEEN Many times we may require to find out records between a range of values.
We can specify one lower limit and one upper limit for column and the query will return all the records between these two values.

SQL BETWEEN to get rows within a range with all other commands


We will apply here BETWEEN command to a numeric field and see how the records are returned from a Mysql table. Here is our table.
idnameclassmarksex
1John DeoFour75female
2Max RuinThree85male
3ArnoldThree5male
4Krish StarFour60female
5John MikeFour60female
6Alex JohnFour55male

 
On this table we will apply our BETWEEN command to get all the  records within some upper and lower limits. Say for our mark column upper limit is 75 and lower limit is 60. So we will get all the records within these limits and note that limit 60 and 75 both are inclusive. Here is our sql BETWEEN command.

SELECT * FROM `student` WHERE mark BETWEEN 60 AND 75
idnameclassmarksex
1John DeoFour75female
4Krish StarFour60female
5John MikeFour60female
18HonnyFive75male
20JacklyNine65female
21Babby JohnFour69female
34Gain ToeSeven69male

You can see we have all the records between 60 and 75 ( both inclusive).
Note that we have to first start with lower limit and then upper limit. So the records between 60 and 75 will be displayed ( NOT BETWEEN 75 and 60 )

How many records are there within this limit ( USE count() )?
SELECT COUNT(*) FROM student WHERE mark BETWEEN 60 AND 75;
7

Using NOT

SELECT * FROM `student` WHERE mark NOT BETWEEN 50 AND 100
Output
idnameclassmarksex
19TinnyNine18male

Displaying from highest to lowest mark

Using order by we can change the above display and show from highest to lowest.
SELECT * FROM `student` 
WHERE mark BETWEEN 60 AND 75 
ORDER BY mark DESC
By using DESC we are getting results from highest to lowest. By default it is from lowest range to highest range. Output is here
idnameclassmarksex
1John DeoFour75female
18HonnyFive75male
21Babby JohnFour69female
34Gain ToeSeven69male
20JacklyNine65female
4Krish StarFour60female

Displaying from one class only

We will restrict the students of class four only
SELECT * FROM `student` 
WHERE mark BETWEEN 60 AND 75 
AND class = 'Four'
idnameclassmarksex
1John DeoFour75female
4Krish StarFour60female
5John MikeFour60female
21Babby JohnFour69female

Displaying from limited class using IN

Only from Class Four and Class Seven
SELECT * FROM `student` 
WHERE mark BETWEEN 60 AND 75 
AND class IN('Four', 'Seven')
idnameclassmarksex
1John DeoFour75female
4Krish StarFour60female
5John MikeFour60female
21Babby JohnFour69female
34Gain ToeSeven69male

Displaying from limited class using NOT IN

SELECT * FROM `student` 
WHERE mark BETWEEN 60 AND 75 
AND class NOT IN('Four', 'Seven')
idnameclassmarksex
18HonnyFive75male
20JacklyNine65female

Number of students from each class

How many number of students have secured mark between 60 and 75 in each class by using GROUP BY
SELECT class, COUNT(id) AS no  
FROM `student`  
WHERE mark BETWEEN 60 AND 75  
GROUP BY class
classno
Five1
Four4
Nine1
Seven1
We can create a comparative statement in a grid view by using SQL IF and between command
SELECT class,  
SUM( IF( mark < 50, 1, 0 ) ) AS grade_C,  
SUM( IF( mark BETWEEN 50 AND 70, 1, 0 ) ) AS grade_B,  
SUM( IF( mark > 70, 1, 0 ) ) AS grade_A  
FROM `student`  
GROUP BY class
Out put is here
Classgrade_Cgrade_Bgrade_A
Eight 0 0 1
Five0 0 3
Four0 5 4
Nine1 1 0
Seven0 3 7
Six 0 1 6
Three 0 1 2

SQL IF condition

Sample code in PHP

Query using PHP script Here is the sample code in PHP to to get data using between query.

connect to database using PHP PDO.
<?php
require "config.php"; // Database connection

$count = "SELECT * FROM `student` WHERE mark BETWEEN 60 AND 75";

echo "<table>";
echo "<tr><th>id</th><th>name</th><th>class</th><th>mark</th></tr>";

foreach ($dbo->query($count) as $row) {
    echo "<tr><td>$row[id]</td><td>$row[name]</td><td>$row[class]</td><td>$row[mark]</td></tr>";
}

echo "</table>";
?>

Questions


Selecting records between two date ranges



Selecting records Case Condition Checking

Subscribe to our YouTube Channel here



plus2net.com
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
©2000-2025   plus2net.com   All rights reserved worldwide Privacy Policy Disclaimer