SQL查询需求:获取数据表两列各自的最大值
Hey there! Let's figure out how to get the maximum values from Column1 and Column2 in your table.
First, let's align on your data and requirements clearly:
Your Table Data
Column1 | Column2
--------+--------
1 | 1
3 | 2
1 | 3
2 | 1
2 | 4
You want to retrieve the highest value from Column1 and the highest value from Column2 separately, right?
The SQL Query You Need
This is straightforward with SQL's MAX() aggregate function. Just run this query (replace your_table_name with the actual name of your table):
SELECT MAX(Column1) AS Max_Column1, MAX(Column2) AS Max_Column2 FROM your_table_name;
Quick Breakdown of the Query
MAX(Column1)scans all entries in Column1 and returns the largest value (which is 3 in your sample data)MAX(Column2)does the exact same for Column2 (and for your data, that's 4—quick heads up, your example result mentioned "3 : 2" but that looks like a typo, since Column2 has a 4 in the last row!)- The
ASkeywords assign friendly names to the result columns, so you can easily tell which max corresponds to which original column
Expected Result
When you run this query against your sample data, the output will look like this:
Max_Column1 | Max_Column2
------------+------------
3 | 4
That's all there is to it—this query efficiently grabs both max values in a single pass over your table.
内容的提问来源于stack exchange,提问作者Sami Meskaoui

