PostgreSQL Tutorial

Data Types

Querying & Filtering Data

Managing Tables

Modifying Data

Conditionals

Control Flow

Transactions & Constraints

Working with JOINS & Schemas

Roles & Permissions

Working with Sets

Subquery & CTEs

User-defined Functions

Important In-Built Functions

PostgreSQL PL/pgSQL

Variables & Constants

Stored Procedures

Working with Triggers

Working with Views & Indexes

Errors & Exception Handling

PostgreSQL - MIN() Function

In PostgreSQL, the MIN() function is an aggregate function that returns the minimum value in a set of values. It is useful when you need to identify the smallest value in a specific column or result set.

Syntax:

MIN(expression)

Where expression can be a column name, calculation, or another SQL expression.

Examples:

  1. Basic Usage:

    Suppose you have a table named products with a column named price. To find the minimum price from this table:

    SELECT MIN(price) FROM products;
    
  2. Using MIN with a GROUP BY clause:

    If you want to determine the minimum order date for each customer in a table called orders that has columns customer_id and order_date:

    SELECT customer_id, MIN(order_date) 
    FROM orders 
    GROUP BY customer_id;
    
  3. Using MIN with date and time:

    The MIN() function can also be used on date and time columns. For instance, to determine the earliest registration date from a users table:

    SELECT MIN(registration_date) FROM users;
    

Notes:

  • The MIN() function, like the MAX() function, will ignore NULL values. If all values in the dataset are NULL, the function will return NULL.

  • Be cautious when using MIN() with character data. The "minimum" value will be based on the character set sequence.

  • If you find yourself frequently using the MIN() function on large datasets, consider indexing the relevant columns for improved performance.

In summary, the MIN() function in PostgreSQL provides an easy and efficient way to identify the smallest value in a dataset. Whether working with numbers, date/time, or character strings, the function can be utilized to quickly retrieve the minimum value, either from the entire dataset or within grouped segments.

  1. How to use MIN() function in PostgreSQL:

    • The MIN() function in PostgreSQL is used to find the minimum value in a set of values.
    SELECT MIN(column_name) FROM your_table;
    
  2. Finding the minimum value in a column with MIN() in PostgreSQL:

    • Use MIN() to find the minimum value in a specific column.
    SELECT MIN(price) FROM products;
    
  3. MIN() function with GROUP BY clause in PostgreSQL:

    • Apply MIN() with GROUP BY for finding minimum values within groups.
    SELECT category, MIN(price) AS min_price
    FROM products
    GROUP BY category;
    
  4. Using MIN() with multiple columns in PostgreSQL:

    • Use MIN() with multiple columns to find the minimum values for each column.
    SELECT MIN(column1) AS min_column1, MIN(column2) AS min_column2
    FROM your_table;
    
  5. MIN() vs. ORDER BY LIMIT 1 in PostgreSQL:

    • Compare MIN() with ORDER BY LIMIT 1 for finding the minimum value.
    SELECT MIN(column_name) FROM your_table;
    SELECT column_name FROM your_table ORDER BY column_name LIMIT 1;
    
  6. Handling NULL values with MIN() in PostgreSQL:

    • Handle NULL values using COALESCE when using MIN().
    SELECT MIN(COALESCE(column_name, 0)) FROM your_table;
    
  7. MIN() function with aggregate functions in PostgreSQL:

    • Combine MIN() with other aggregate functions for more complex queries.
    SELECT MIN(AVG(salary)) FROM employees;
    
  8. Using MIN() in subqueries in PostgreSQL:

    • Utilize MIN() in subqueries for advanced data retrieval.
    SELECT *
    FROM your_table
    WHERE column_name = (SELECT MIN(column_name) FROM another_table);
    
  9. MIN() with window functions in PostgreSQL:

    • Apply MIN() as a window function for analytical queries.
    SELECT column_name, MIN(column_name) OVER () AS min_overall
    FROM your_table;
    
  10. MIN() with DISTINCT in PostgreSQL:

    • Use MIN() with DISTINCT to find the minimum value among unique values.
    SELECT MIN(DISTINCT column_name) FROM your_table;
    
  11. Finding the minimum date or time with MIN() in PostgreSQL:

    • Use MIN() with date or timestamp columns to find the earliest date or time.
    SELECT MIN(order_date) FROM orders;
    
  12. MIN() and JOIN operations in PostgreSQL:

    • Apply MIN() in JOIN operations for combined analysis.
    SELECT customers.customer_id, MIN(orders.order_date) AS first_order_date
    FROM customers
    JOIN orders ON customers.customer_id = orders.customer_id
    GROUP BY customers.customer_id;
    
  13. Using MIN() with conditional statements in PostgreSQL:

    • Integrate CASE statements with MIN() for conditional analysis.
    SELECT MIN(CASE WHEN condition THEN column_name ELSE 0 END) FROM your_table;
    
  14. MIN() function with HAVING clause in PostgreSQL:

    • Use MIN() with the HAVING clause for conditional filtering.
    SELECT category, MIN(price) AS min_price
    FROM products
    GROUP BY category
    HAVING MIN(price) < 50;
    
  15. Using MIN() in stored procedures and functions in PostgreSQL:

    • Incorporate MIN() within stored procedures or functions for reusability.
    CREATE OR REPLACE FUNCTION get_min_value()
    RETURNS INT AS $$
    BEGIN
       RETURN (SELECT MIN(column_name) FROM your_table);
    END;
    $$ LANGUAGE plpgsql;