In this article, we will learn the MySQL less than and greater than operators. These comparison operators are mainly to select, update and delete data from tables, to compare data and here we will look at the examples.
Basic rules of comparison
The result of the comparison can be TRUE (1), False (0) and NULL. We can use the comparison operators both with numbers and with strings. By the way, MySQL automatically turns the numbers into strings and strings into numbers upon need.
|1.||String comparisons are case-insensitive and use the current character set.|
|2.||NULL – an indefinite value that does not belong to more than one data type, corresponds to the concept of nothing, this value is a non-zero and non-empty string. The result of the comparison is always NULL if one or all of the arguments are equal to NULL.|
|3.||If we don’t compare the hexadecimal values with a number then the program treats them as strings with binary data.|
|4.||If we have 2 arguments and the first one is a column which type is TIMESTAMP or DATETIME and the second argument is a constant, then the program will automatically turn the constant into the TIMESTAMP type.|
|5.||If the arguments we compare are strings then the result will be a string as well.|
|6.||If the arguments we compare are integers then the result will be an integer as well.|
|7.||In all other cases, the arguments are compared as valid floating-point numbers.|
Table 1. The basic rules of comparison
Before we begin, we need to make some preparations for the examples. We need to create a “STAFF_SALARY” table using this script:
CREATE TABLE `STAFF_SALARY` ( `STAFF_ID` int DEFAULT NULL, `FROM_DATE` date DEFAULT NULL, `NAME` varchar(255) DEFAULT NULL, `SALARY` int DEFAULT NULL ) ;
And we need to add new records to this table:
INSERT INTO `STAFF_SALARY` (`STAFF_ID`,`FROM_DATE`,`NAME`,`SALARY`) VALUES (1,'2010-01-01','Karl',12500), (2,'2010-01-01','Andy',12500), (3,'2010-01-01','Maks',13500), (4,'2010-01-01','Alla',14500), (5,'2010-01-01','Stuart',15500);
MySQL less than or equal to
To find out if the desired value is less than our second argument, we use the operator <. If we need to know whether it is less than or equal to it, we use the <= operator.
When the inequality is strict, the value is not included in the result area, and when it is less than or equal, the value will be included in the possible result.
In this example, we need to get all the information about the workers whose salary is less than 13500, for this, our script will look like this:
SELECT * FROM STAFF_SALARY WHERE SALARY < 13500
Figure 1. The workers with a salary of less than 13500
In this example, we will get the information about the workers whose salary is less than or equal to 13500.
SELECT * FROM STAFF_SALARY WHERE SALARY <= 13500
Figure 2. The workers with the salary of less than or equal to 13500
As we can see, unlike the previous example, our result included the information of the workers.
MySQL greater than or equal to examples
We use the symbol > to find out if the number or a string is greater than our second argument. When we need to discover whether it is greater than or equal to it, we use the >= symbol.
The value is not included in the result area, when the inequality is strict. At the same time, the value will be included in the possible result, when it is greater than or equal.
In this example, we need to receive the names of the workers whose salary is greater than 13500 in the result.
SELECT * FROM STAFF_SALARY WHERE SALARY > 13500
Figure 3. The workers with a salary greater than 13500
In this example, we need to get the names of the workers with the salary greater than or equal to 13500.
SELECT * FROM STAFF_SALARY WHERE SALARY >= 13500
Figure 4. The workers with a salary greater than or equal to 13500
Instant livechat to an Expert!
Most of the time, the problem you will need to solve will be more complex than a simple method. If you want to save hours of research and frustration, try our live SQLExpert service!
Our SQLQuerychat Experts are available 24/7 to answer any questions you may have. We guarantee a connection within 30 seconds and a customized solution within 20 minutes.