Menu

How to compute maximum of multiple columns, aks row wise max?

Written by Jagdeesh | 2 min read

Problem

Given a table with multiple columns, compute the maximum value for each row across the specified columns.

Input

sql
-- Create an example table
CREATE TABLE ExampleTable (
    ID INT AUTO_INCREMENT PRIMARY KEY,
    ColumnA INT,
    ColumnB INT,
    ColumnC INT
);

-- Insert data into the table
INSERT INTO ExampleTable (ColumnA, ColumnB, ColumnC)
VALUES
    (10, 20, 30),
    (25, 15, 35),
    (40, 10, 5),
    (30, 30, 30),
    (5, 45, 20);

ExampleTable:

ID ColumnA ColumnB ColumnC
1 10 20 30
2 25 15 35
3 40 10 5
4 30 30 30
5 5 45 20

Desired Solution

ID RowMax
1 30
2 35
3 40
4 30
5 45

Solution:

To find the maximum value for each row across multiple columns in MySQL, you can use the GREATEST function.

sql
SELECT ID,
       GREATEST(ColumnA, ColumnB, ColumnC) AS RowMax
FROM ExampleTable;

Output:

ID RowMax
1 30
2 35
3 40
4 30
5 45

Explanation:

The above SQL query will return the maximum value for each row across ColumnA, ColumnB, and ColumnC.

In MySQL, the GREATEST function is used to return the greatest value in a list of expressions. It can compare numbers, strings, dates, etc., and will return the highest value among the given list.

For example, the expression GREATEST(10, 20, 30) will return 30 as it’s the largest number in the provided list.

  1. SQL for Data Science – Level 1
  2. SQL for Data Science – Level 2
  3. SQL for Data Science – Level 3
  1. Introduction to SQL
  2. SQL Window Functons – Made Simple and Easy
  3. SQL Subquery

More SQL Questions

  1. How to access previous row value in SQL?
  2. How to exclude specific columns using select except?
  3. How to transpose columns to rows in SQL?
  4. How to select first row in each GROUP BY group?
Free Course
Master Core Python — Your First Step into AI/ML

Build a strong Python foundation with hands-on exercises designed for aspiring Data Scientists and AI/ML Engineers.

Start Free Course
Trusted by 50,000+ learners
Jagdeesh
Written by
Related Course
Master SQL — Hands-On
Join 5,000+ students at edu.machinelearningplus.com
Explore Course
Get the full course,
completely free.
Join 57,000+ students learning Python, SQL & ML. One year of access, all resources included.
📚 10 Courses
🐍 Python & ML
🗄️ SQL
📦 Downloads
📅 1 Year Access
No thanks
🎓
Free AI/ML Starter Kit
Python · SQL · ML · 10 Courses · 57,000+ students
🎉   You're in! Check your inbox (or Promotions/Spam) for the access link.
⚡ Before you go

Python.
SQL. NumPy.
All free.

Get the exact 10-course programming foundation that Data Science professionals use.

🐍
Core Python — from first line to expert level
📈
NumPy & Pandas — the #1 libraries every DS job needs
🗃️
SQL Levels I–III — basics to Window Functions
📄
Real industry data — Jupyter notebooks included
R A M S K
57,000+ students
★★★★★ Rated 4.9/5
⚡ Before you go
Python. SQL.
All Free.
R A M S K
57,000+ students  ★★★★★ 4.9/5
Get Free Access Now
10 courses. Real projects. Zero cost. No credit card.
New learners enrolling right now
🔒 100% free ☕ No spam, ever ✓ Instant access
🚀
You're in!
Check your inbox for your access link.
(Check Promotions or Spam if you don't see it)
Or start your first course right now:
Start Free Course →
Scroll to Top
Scroll to Top
Course Preview

Machine Learning A-Z™: Hands-On Python & R In Data Science

Free Sample Videos:

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science

Machine Learning A-Z™: Hands-On Python & R In Data Science