
- Learn MySQL
- MySQL - Home
- MySQL - Introduction
- MySQL - Installation
- MySQL - Administration
- MySQL - PHP Syntax
- MySQL - Connection
- MySQL - Create Database
- MySQL - Drop Database
- MySQL - Select Database
- MySQL - Data Types
- MySQL - Create Tables
- MySQL - Drop Tables
- MySQL - Insert Query
- MySQL - Select Query
- MySQL - Where Clause
- MySQL - Update Query
- MySQL - Delete Query
- MySQL - Like Clause
- MySQL - Sorting Results
- MySQL - Using Join
- MySQL - NULL Values
- MySQL - Regexps
- MySQL - Transactions
- MySQL - Alter Command
- MySQL - Indexes
- MySQL - Temporary Tables
- MySQL - Clone Tables
- MySQL - Database Info
- MySQL - Using Sequences
- MySQL - Handling Duplicates
- MySQL - SQL Injection
- MySQL - Database Export
- MySQL - Database Import
Get the highest score value from a single column and the greatest from two columns in MySQL
Let us first create a table −
mysql> create table DemoTable790 ( Score1 int, Score2 int ); Query OK, 0 rows affected (0.45 sec)
Insert some records in the table using insert command −
mysql> insert into DemoTable790 values(98,76); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable790 values(78,89); Query OK, 1 row affected (0.12 sec) mysql> insert into DemoTable790 values(85,68); Query OK, 1 row affected (0.14 sec)
Display all records from the table using select statement −
mysql> select *from DemoTable790;
This will produce the following output -
+--------+--------+ | Score1 | Score2 | +--------+--------+ | 98 | 76 | | 78 | 89 | | 85 | 68 | +--------+--------+ 3 rows in set (0.00 sec)
The max() is as follows to get the maximum value from a single column −
mysql> select max(Score1) from DemoTable790;
This will produce the following output -
+-------------+ | max(Score1) | +-------------+ | 98 | +-------------+ 1 row in set (0.00 sec)
The greatest() is as follows to get the greatest value from two columns −
mysql> select greatest(Score1,Score2) from DemoTable790;
This will produce the following output -
+-------------------------+ | greatest(Score1,Score2) | +-------------------------+ | 98 | | 89 | | 85 | +-------------------------+ 3 rows in set (0.03 sec)
- Related Articles
- How to order by the highest value from two columns in MySQL?
- How to get the 2nd highest value from a table with Student Score?
- Select distinct names from two columns in MySQL and display the result in a single column
- MySQL query to get first two highest column values from a table?
- Get the minimum and maximum value from a VARCHAR column and display the result in separate MySQL columns?
- MySQL query to get the highest value from a single row with multiple columns\n\n\n\n\n\n\n\n\n\n
- Concatenate date and time from separate columns into a single column in MySQL
- Get the count of duplicate values from a single column in MySQL?
- Subtracting a number from a single MySQL column value?
- How to get the greatest of two columns values in MySQL?
- Select distinct values from three columns and display in a single column with MySQL
- Get the minimum value from a list with multiple columns in MySQL?
- SELECT not null column from two columns in MySQL?
- MySQL query to combine two columns in a single column?
- How can we combine values of two or more columns of MySQL table and get that value in a single column?

Advertisements