MySQL Operators And Comparison Functions


Use GREATEST(value1,value2,...) function with column data

Here first of all we have to create a table "employees" and after that insert values into the table. Find the greatest values of  the column in the table.

mysql> CREATE TABLE Employees(
>id        int,
>first_name    VARCHAR(15),
>last_name     VARCHAR(15),
>start_date    DATE,
>end_date      DATE,
>salary        FLOAT(8,2),
>description   VARCHAR(15)
>);

mysql>insert into Employees(id,first_name, last_name, start_date, end_Date,   salary, Description)
> values (1,'Akshay',    'Martin',  '19960725',  '20060725', 1234.56,  'Programmer');

mysql>insert into Employees(id,first_name, last_name, start_date, end_Date,   salary, Description)
 >values(2,'Akash',   'Mathews',  '19760321', '19860221', 6661.78, 'Tester');

mysql>insert into Employees(id,first_name, last_name, start_date, end_Date,   salary, Description)
> values(3,'Amit',    'Smith',    '19781212', '19900315', 6544.78, 'Tester');

mysql>insert into Employees(id,first_name, last_name, start_date, end_Date,   salary, Description)
>values(4,'Deepak',    'Rice',     '19821024', '19990421', 2344.78,'Manager');

mysql> insert into Employees(id,first_name, last_name, start_date, end_Date,   salary, Description)
>values(5,'Manoj',   'Black',    '19840115', '19980808', 2334.78,'Tester');

mysql> insert into Employees(id,first_name, last_name, start_date, end_Date,   salary, Description)
 > values(6,'manish',    'Green',    '19870730', '19960104', 4322.78, 'Tester');

mysql> insert into Employees(id,first_name, last_name, start_date, end_Date,   salary, Description)
> values(7,'David',    'Larry',    '19901231', '19980212', 7897.78, 'Manager');

mysql> insert into Employees(id,first_name, last_name, start_date, end_Date,   salary, Description)
> values(8,'Rohtash',    'Cat',     '19960917',  '20020415', 1232.78,'Tester');

mysql> select * from  employees;

img 1.gif

Syntax

mysql> select GREATEST(salary, 5000) from employee;

img 1.1.gif

GREATEST(@id1, @id2, @id3, @id4, @id5): using Greatest function with user-defined variables.

Syntax

mysql>   SELECT *
        >    FROM employees
       >     WHERE ID=GREATEST(@id1, @id2, @id3, @id4, @id5);

img 2.gif


mysql> SELECT GREATEST(3.0,3.0,5.0,7.0);

img 3.gif


GREATEST(value1,value2,...) returns NULL if any argument is NULL.

Syntax
mysql> SELECT GREATEST(2,NULL);

img 4.gif

GREATEST(value1,value2,...) returns the largest (maximum-valued) argument within two or more arguments.

Syntax
mysql> SELECT GREATEST(2,0);

img 5.gif


SELECT GREATEST('B','A','C');

Synatax
mysql> SELECT GREATEST('B','A','C');

img 6.gif

Resources

Here are some useful resources:

Up Next
    Ebook Download
    View all
    Learn
    View all