Beginner

598. JOIN with MIN and MAX

Use MIN and MAX with JOINs to identify the lowest and highest related values.

📘 Lesson 598 of 728 ⏱ 13 min 🎯 Intermediate

MIN returns the smallest value and MAX returns the largest value within a group of rows. They can be used after JOINing related tables.

Minimum and maximum values help identify ranges, extremes, first or last activity, and performance boundaries.

Find the first and highest order amount for each customer.

SQL
SELECT
    c.CustomerName,
    MIN(o.OrderDate) AS FirstOrderDate,
    MAX(o.OrderAmount) AS HighestOrder
FROM Customers c
INNER JOIN Orders o
    ON c.CustomerID = o.CustomerID
GROUP BY
    c.CustomerName;

Used in customer analysis, financial reporting, transaction monitoring, and performance analysis.

  • MIN returns the smallest value.
  • MAX returns the largest value.
  • They can be used with grouped JOIN results.
  • Date columns can also be analyzed.
  • The chosen JOIN affects which records are considered.
How can MIN and MAX be used with JOINs?

They can summarize the lowest and highest values from related records after the tables are joined.

Can MIN and MAX be used with dates?

Yes. They can identify the earliest and latest dates within a group.

Have you finished this lesson?

Introduction to SQL – All Lessons