| Lesson 10 | The MIN and MAX functions |
| Objective | Write a query that uses the SQL min and max SQL Functions |
The MIN() and MAX() functions are aggregate functions that help you find boundary values in a dataset. Instead of asking for a total or an average, these functions answer a different type of question: what is the smallest value, and what is the largest value? That makes them useful whenever you need to identify extremes such as the lowest quota, the highest score, the earliest order date, or the maximum result of a calculated expression.
These functions are important because many business and analytical questions are really questions about boundaries. A manager may want to know the smallest assigned quota in a sales team. An analyst may want to identify the earliest order date in a table. A reporting query may need the highest performance value or the least expensive item. In each case, MIN() and MAX() allow SQL to scan the qualifying rows and return the extreme values directly. :contentReference[oaicite:0]{index=0}
This lesson explains how MIN() and MAX() work, what kinds of data they can evaluate, how they behave with and without GROUP BY, and why string comparisons depend on collation or character-set ordering rules. It also shows why aliases improve readability and why these functions belong in the same family of aggregate tools as SUM() and AVG().
The simplest pattern is to apply the functions to a single column:
SELECT MIN(column_name), MAX(column_name)
FROM table_name;
In this form, SQL examines the qualifying rows and returns one summarized row containing the smallest and largest values for that column. Because these are aggregate functions, they work across the full set of rows matched by the query, not one row at a time.
A clear example appears when working with student identifiers:
SELECT MIN(StudentId) AS MinimumId,
MAX(StudentId) AS MaximumId
FROM students;
Here, MIN(StudentId) returns the smallest student identifier in the table, while MAX(StudentId) returns the largest. The aliases MinimumId and MaximumId make the result easier to read, which is especially helpful in reports or reusable queries. :contentReference[oaicite:1]{index=1}
Many learners first associate MIN() and MAX() with numeric values, but these functions are not limited to numbers. They can also be applied to string data, dates, times, and other comparable types supported by the database system. The result returned by each function has the same data type as the column or expression being evaluated.
That means these functions can answer several different kinds of questions:
This flexibility is one reason the functions are so useful. They do not only summarize raw numeric columns. They help identify extrema across several important SQL data types. :contentReference[oaicite:2]{index=2}
When MIN() and MAX() are applied to numeric data, the database compares values in algebraic order. Large negative numbers are smaller than small negative numbers, negative numbers are smaller than zero, and zero is smaller than all positive numbers. That means the functions behave exactly as you would expect in ordinary mathematics.
For example, to find the smallest and largest assigned quotas:
SELECT MIN(QUOTA), MAX(QUOTA)
FROM SALESREPS;
This query returns the lowest and highest quota values from the SALESREPS table. These types of questions are common in business reporting because managers often want to see the operating boundaries of a measure rather than every individual row. :contentReference[oaicite:3]{index=3}
Date values can also be evaluated with these functions. When SQL compares dates, earlier dates are smaller and later dates are larger. That makes MIN() useful for finding the earliest date in a table, while MAX() helps identify the latest.
A simple example is:
SELECT MIN(ORDER_DATE)
FROM ORDERS;
This query returns the earliest order date in the ORDERS table. The same idea can be applied to shipment dates, invoice dates, event timestamps, or any other temporal column. In real systems, this type of query is often used to identify the first recorded transaction, the most recent event, or the oldest outstanding item. :contentReference[oaicite:4]{index=4}
Another important point is that MIN() and MAX() can operate on expressions, not only on stored columns. This makes them more powerful because the database can first calculate a value and then identify the extreme result.
For example, the best sales performance of any salesperson can be computed using an arithmetic expression:
SELECT MAX(100 * (SALES/QUOTA))
FROM SALESREPS;
This query does not simply take the maximum value from a stored column. It first calculates the percentage performance of each salesperson and then returns the highest result. That is an important step in advanced SQL thinking, because it shows that aggregate functions can be combined with numeric expressions to answer more meaningful business questions. :contentReference[oaicite:5]{index=5}
Like other aggregate functions, MIN() and MAX() become even more useful when combined with GROUP BY. Grouping changes the scope of the calculation. Instead of returning one extreme value for the entire dataset, the query returns one minimum or maximum for each group.
For example, a grouped query might return:
This is how SQL produces group-level extrema in reports. Once you understand grouping, boundary-value functions become much more useful because the query can identify the extreme values inside logical categories rather than only for the full table. :contentReference[oaicite:6]{index=6}
Older examples sometimes show queries such as:
SELECT MyName, MyAddress, MyNumber,
MIN(MyNumber),
MAX(MyNumber)
FROM MyTable;
In standard SQL, that pattern is usually not valid unless the non-aggregated columns are properly grouped or another technique is used. Aggregate output and row-level detail columns cannot normally be mixed freely in the same SELECT list without clear grouping rules.
The safer teaching point is this: if you want grouped extrema, use GROUP BY. If you want row detail alongside repeated boundary values, that usually requires a different strategy, such as window functions, depending on the database platform. For this lesson, the main focus should remain the standard aggregate behavior of MIN() and MAX(). :contentReference[oaicite:7]{index=7}
One of the most important advanced ideas in this lesson is that string-based MIN() and MAX() do not follow a universal alphabetical truth. They follow the ordering rules of the system’s collation or character set. That means the smallest or largest string can vary from one system to another.
The legacy page highlights this by contrasting ASCII and EBCDIC collating sequences. On many personal computers and common servers using ASCII-oriented ordering, digits come before letters, and uppercase letters typically come before lowercase letters. On systems using EBCDIC, the relative ordering can be different. As a result, the minimum and maximum string values for the same set of data may change depending on the environment. :contentReference[oaicite:8]{index=8}
| ASCII | EBCDIC |
| ---------- | ---------- |
| 1234ABC | acme mfg. |
| 5678ABC | zeta corp. |
| ACME MFG. | Acme Mfg. |
| Acme Mfg. | ACME MFG. |
| ZETA CORP. | Zeta Corp. |
| Zeta Corp. | ZETA CORP. |
| acme mfg. | 1234ABC |
| zeta corp. | 5678ABC |
This comparison matters because a query using MIN(), MAX(), or even ORDER BY on string data can produce different outcomes across systems. That is why modern SQL work pays attention to collation settings and platform-specific ordering rules rather than assuming every string comparison behaves the same way. :contentReference[oaicite:9]{index=9}
The MIN() and MAX() functions matter because reports often need boundary values rather than totals. A manager may not always want the sum of all quotas, but they may need to know the lowest quota and the highest quota. An operations analyst may care about the earliest order date in a system. A performance query may need the highest calculated metric. These are all practical reporting questions, and SQL answers them efficiently through aggregate functions.
In that sense, MIN() and MAX() belong to the same broader family as SUM() and AVG(), but they serve a different analytical purpose. Instead of measuring total magnitude or average level, they identify the edges of the data. That makes them essential tools for summary reporting, auditing, and data exploration.
At this point, you should understand that MIN() and MAX() are aggregate functions used to find extreme values across a set of rows. You have seen that they work with numeric, date, and string data, that they can be applied to expressions, and that grouping changes the scope of the result. You have also seen that string results depend on system ordering rules, which is an important reminder that platform details matter in SQL.
As you work through the numeric-functions exercise, focus on a few key questions:
Those questions will help you move beyond memorizing syntax and toward understanding how extreme-value queries fit into practical SQL analysis.