INTRODUCTION
In this tutorial, I am going to explain about working with Operators and Comparison Functions in MySQL with examples. This detailed article will cover the following topics as follows. Let’s see.
- Introduction
- Operators in MySQL
- MySQL Comparison Functions
- Conclusion
First, let's create a database with a few tables containing some dummy data. Here, I am providing the database with the tables containing the records, on which I am showing you the various examples. Let's see.
- CREATE DATABASE MySQLComparsionFunctions;
- USE MySQLComparsionFunctions;
- CREATE TABLE Books(
- BookID INT PRIMARY KEY auto_increment,
- BookName varchar(100) NOT NULL,
- BookType varchar(100) NOT NULL,
- BookVendor varchar(100) NOT NULL,
- BookDescription varchar(250),
- BookInStock int NOT NULL
- );
- CREATE TABLE Library(
- BookNumber INT NOT NULL,
- BookCode VARCHAR(15) NOT NULL,
- BookIssue INT NOT NULL,
- CostEach DECIMAL(10, 2) NOT NULL,
- PRIMARY KEY(BookCode)
- );
By using the following query, let us check the following query.
1)
- SELECT * FROM mysqlcomparsionfunctions.books;

2)
- SELECT * FROM mysqlcomparsionfunctions.library;

Operators in MySQL
Operators may be used to build expressions. MySQL operators are very similar to mathematical operators.
Operators are classified into two categories, viz.
- Binary
- Unary
There are different types of operators in MySQL, let’s discuss them one by one.
- Arithmetic operators
- Boolean operators
- Relational operators
- Bitwise operators
- Other operators
- Unary operators
Note:
Binary operators work with two operands, but unary work with one.
To read more about MySQL Operators, Visit, Variables & Operators in MySQL.
MySQL Comparison Functions
Some of the MySQL Comparison Functions are GREATEST and LEAST, ISNULL. Let's discuss them, one by one.
GREATEST
Greatest function is used to find out the greatest value from two or more fields respectively.
Note
1) Greatest functions will return the "NULL" value if contains any "NULL" value in it.
2) If arguments contain the integer values and string values, then it compares them as numbers.
Syntax
GREATEST(value1, value2, value3, ...);
Example 1
- SELECT GREATEST(8, 9, 10),
- GREATEST(8, 9, 10, 100, 121, 151, 251),
- GREATEST(555, null, 101);

Example 2
Returns “NULL” if any argument is “NULL”.
Syntax
- SELECT GREATEST(2, NULL, 20, 251);

Example 3










Onkar SharmaPosted Oct 23, 2020, 12:37 PM
Nice Article, thanks for sharing this quality content with us...