While equality and inequality filter conditions are very common, many times you wish to filter the FROM clause table rows based on values that are less than or greater than another value. MySQL (and SQL in general) supports the less than (<) and greater than (>) operators. Continue reading and see examples of each…
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
Another way you can think of these types of filter conditions is as a range-based filter (in a sense). This is where the less than and greater than conditional filter operators come in.
Less than and greater than comparison operators with numerical data
In the below query we are filtering for any rows that have a ‘category_id’ column value of less than 5:
SELECT *
FROM category
WHERE category_id < 5;
There are 4 rows that pass the criteria of the conditional ‘category_id < 5’ and are returned in the query results.
On the converse, if you are only interested in seeing any rows with a ‘category_id’ column value greater than 5, use the greater than comparison operator (>) in the WHERE clause conditional filter:
SELECT *
FROM category
WHERE category_id > 5;
📰 Get your brand, product, or service the attention it deserves with affordable classified ad placement in the OpenLampTech newsletter. I appreciate your support!
Less than or equal to and greater than or equal to
You may have noticed that in the previous example queries, the results did not include the bounds of the conditional filter. For instance, the conditional filter ‘category_id < 5’ does not include any rows where the actual ‘category_id’ value is 5. Only those that are less than 5.
To include the actual bounds of the filter condition, use the ‘or equals to’ comparison operator accordingly.
In the following query, the conditional filter ‘category_id <= 5’ will filter any rows that have a ‘category_id’ that is less than 5, along with those rows whose ‘category_id’ is equal to 5 (if any):
SELECT *
FROM category
WHERE category_id <= 5;
Equals to works the same for greater than comparisons just as the less than or equals example:
SELECT *
FROM category
WHERE category_id >= 5;
Be sure and visit any of these similar MySQL beginners series articles:
- MySQL SELECT and WHERE Clause Column Existence
- SELECT clause queries – MySQL Beginner Basics Series.
- Limit Rows with the WHERE clause – MySQL Beginner Series
Thank you for reading this post. Please share it with someone else who would enjoy it as well.
Josh Otwell has a passion to grow as a PHP Developer, SQL expert, and technical blogger/writer.
Disclaimer: The majority of examples in this post, are performed in a personal development/learning workstation environment and should not be considered production quality or ready. Your particular goals and needs may vary. Like always, just because you can do something doesn’t mean you should. My opinions are my own.
More ways I can help
- Need hosting for your next web application or WordPress site? I highly recommend Hostinger and use them to host my niche bass fishing site. The service is second to none.
- 🔒5 Truths I’ve Come To Realize As a Self-taught Developer
- Desktop and mobile wallpapers, digital downloads, photography services, Shopify and WooCommerce customizations, and content writing – all in one E-commerce Shop. Find your next digital purchase today!
- Take your Laravel applications next level with Battle Ready Laravel by Ash Allen. Learn how to improve the performance, maintainability, and security of your Laravel projects in this e-book.
Disclosure: Some of this blog post’s services and product links are affiliate links. At no additional cost to you, should you make a purchase by clicking through one of them, I will receive a commission.
The Newsletter for PHP and MySQL Developers
Receive a copy of my ebook, “10 MySQL Tips For Everyone”, absolutely free when you subscribe to the OpenLampTech newsletter.
📰 Get your brand, product, or service the attention it deserves with affordable classified ad placement in the OpenLampTech newsletter. I appreciate your support!