← Back to MySQL Course | Chapter 10: Aggregate Functions & Grouping | Lesson 5 of 7

MIN() और MAX()

MIN और MAX class के सबसे छोटे और सबसे लंबे बच्चे को चुनने जैसे हैं: वे किसी column में सबसे कम और सबसे ज़्यादा value ढूँढते हैं।
Syntax
sql
SELECT MIN(column_name), MAX(column_name)
FROM table_name;

Minimum Values ढूँढना

MIN() function एक column scan करता है और matched rows में मिली सबसे कम value return करता है। यह manually sort किए और पहला row चुने बिना सबसे सस्ती prices, सबसे पुरानी dates, या सबसे कम scores ढूँढने के लिए ideal है।

उदाहरण: Finding Minimum Values

sql
CREATE TABLE products (id INT, price INT);
INSERT INTO products VALUES (1, 50), (2, 10), (3, 75);
SELECT MIN(price) AS cheapest FROM products;

Maximum Values ढूँढना

MAX() function एक column scan करता है और matched rows में मिली सबसे बड़ी value return करता है। यह peak sales, top scores, या newest items ढूँढने के लिए उपयोगी है, खासकर एक date column के साथ pair होने पर।

उदाहरण: Finding Maximum Values

sql
CREATE TABLE orders (id INT, order_date TEXT, total INT);
INSERT INTO orders VALUES (1, '2024-01-01', 50), (2, '2024-06-01', 200);
SELECT MAX(total) AS peak_sale FROM orders;

MIN() और MAX() को साथ इस्तेमाल करना

आप एक साथ range के दोनों सिरे देखने के लिए एक single query में MIN() और MAX() दोनों call कर सकते हैं। यह आपको अपने dataset में values की पूरी range एक साथ देखने देता है, जैसे side by side सबसे पहली और सबसे नवीनतम order dates।

उदाहरण: Using MIN() and MAX() Together

sql
CREATE TABLE orders (id INT, order_date TEXT);
INSERT INTO orders VALUES (1, '2024-01-05'), (2, '2024-06-20'), (3, '2024-03-10');
SELECT MIN(order_date) AS earliest, MAX(order_date) AS latest FROM orders;

Non-Numeric MIN() और MAX()

MIN() और MAX() सिर्फ numbers तक limited नहीं हैं और text तथा date columns पर भी काम करते हैं। वे text columns पर alphabetical boundaries और date columns पर temporal boundaries ढूँढ सकते हैं, column के अपने natural ordering इस्तेमाल करते हुए।

उदाहरण: Non-Numeric MIN() and MAX()

sql
CREATE TABLE products (id INT, name TEXT);
INSERT INTO products VALUES (1, 'Zebra Print'), (2, 'Apple Case');
SELECT MIN(name) AS first_alphabetically, MAX(name) AS last_alphabetically FROM products;

GROUP BY के अंदर Ranges Aggregate करना

आप अलग categories के लिए boundaries ढूँढने के लिए MIN() और MAX() को एक GROUP BY clause के साथ combine कर सकते हैं, जैसे पूरे catalog के बजाय हर product category के अंदर highest और lowest price।

उदाहरण: Aggregating Ranges inside GROUP BY

sql
CREATE TABLE products (id INT, category TEXT, price INT);
INSERT INTO products VALUES (1, 'Books', 10), (2, 'Books', 40), (3, 'Toys', 20);
SELECT category, MIN(price) AS lowest, MAX(price) AS highest FROM products GROUP BY category;
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह उम्मीद करना कि MIN और MAX पूरा row return करेंगे, जबकि वे सिर्फ value return करते हैं (एक subquery या ORDER BY ... LIMIT 1 चाहिए)।
  2. उन्हें non-ISO format में text की तरह stored dates पर इस्तेमाल करना, ताकि text order गलत results दे।
  3. यह भूल जाना कि वे NULL values ignore करते हैं।
🔒

Chapter Quiz — Complete all 7 topics to unlock

0/7 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.