{"id":41856,"date":"2025-02-07T04:42:47","date_gmt":"2025-02-06T18:42:47","guid":{"rendered":"https:\/\/database.guide\/?p=41856"},"modified":"2025-02-07T04:42:47","modified_gmt":"2025-02-06T18:42:47","slug":"examples-that-demonstrate-duckdbs-min-function","status":"publish","type":"post","link":"https:\/\/database.guide\/examples-that-demonstrate-duckdbs-min-function\/","title":{"rendered":"Examples that Demonstrate DuckDB&#8217;s MIN() Function"},"content":{"rendered":"\n<p class=\"\">DuckDB has a <code>min()<\/code> function just like most <a href=\"https:\/\/database.guide\/what-is-an-rdbms\/\" data-type=\"post\" data-id=\"222\">RDBMS<\/a>s that returns the minimum value from a set of values. However, DuckDB&#8217;s implementation also allows us to return the bottom <em><code>n<\/code><\/em> minimum values, which is not something we see in most other RDBMSs.<\/p>\n\n\n\n<p class=\"\">This article presents some examples of DuckDB&#8217;s implementation of the <code>min()<\/code> function, so as to demonstrate its basic usage, as well as its bottom <em><code>n<\/code><\/em> functionality.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">Syntax<\/h2>\n\n\n\n<p class=\"\">The <code>min()<\/code> function can be used in the following two ways:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>min(arg)\nmin(arg, n)<\/code><\/pre>\n\n\n\n<p class=\"\">Where <em><code>arg<\/code><\/em> is name of the column from which we want to find the minimum value.<\/p>\n\n\n\n<p class=\"\">The <em><code>n<\/code><\/em> argument is optional. If we provide that, it specifies how many results to return (i.e. the bottom <em><code>n<\/code><\/em> minimum values).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Example<\/h2>\n\n\n\n<p class=\"\">Suppose we create and populate the following table:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>-- Create the table\nCREATE TABLE sales_data (\n    id INTEGER,\n    amount DECIMAL(10, 2),\n    order_date DATE,\n    region TEXT\n);\n\n-- Insert sample data\nINSERT INTO sales_data (id, amount, order_date, region) VALUES\n(1, 200.00, '2023-01-01', 'North'),\n(2, 50.00, '2023-02-15', 'South'),\n(3, 150.00, '2023-03-10', 'North'),\n(4, 300.00, '2023-04-22', 'East'),\n(5, 250.00, '2023-05-05', 'South'),\n(6, 400.00, '2023-06-18', 'East'),\n(7, 350.00, '2023-07-30', 'North'),\n(8, 500.00, '2023-08-12', 'West'),\n(9, 600.00, '2023-09-25', 'West'),\n(10, 450.00, '2023-10-10', 'South');\n\n-- Return all data\nSELECT * FROM sales_data;<\/code><\/pre>\n\n\n\n<p class=\"\">Output:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+----+--------+------------+--------+<br>| id | amount | order_date | region |<br>+----+--------+------------+--------+<br>| 1  | 200.00 | 2023-01-01 | North  |<br>| 2  | 50.00  | 2023-02-15 | South  |<br>| 3  | 150.00 | 2023-03-10 | North  |<br>| 4  | 300.00 | 2023-04-22 | East   |<br>| 5  | 250.00 | 2023-05-05 | South  |<br>| 6  | 400.00 | 2023-06-18 | East   |<br>| 7  | 350.00 | 2023-07-30 | North  |<br>| 8  | 500.00 | 2023-08-12 | West   |<br>| 9  | 600.00 | 2023-09-25 | West   |<br>| 10 | 450.00 | 2023-10-10 | South  |<br>+----+--------+------------+--------+<\/pre>\n\n\n\n<p class=\"\">To find the minimum sales amount, we can use the following query:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    min(amount) AS min_sales\nFROM sales_data;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------+<br>| min_sales |<br>+-----------+<br>| 50.00     |<br>+-----------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Return the Bottom <em><code>n<\/code><\/em> Minimum Values<\/h2>\n\n\n\n<p class=\"\">We can provide a second argument to specify how many values to return:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    min(amount, 3) AS bottom_3_sales\nFROM sales_data;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------------------+<br>|     bottom_3_sales      |<br>+-------------------------+<br>| [50.00, 150.00, 200.00] |<br>+-------------------------+<\/pre>\n\n\n\n<p class=\"\">When we do this, the result is returned as a list containing the specified number of minimum values. In this example I specified that the bottom three minimum values be returned.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using <code>min()<\/code> with a <code>WHERE<\/code> Clause<\/h2>\n\n\n\n<p class=\"\">We can also use the <code>min()<\/code> function with a <code>WHERE<\/code> clause to filter the data before finding the minimum value. For example, if we want to find the minimum sales amount for a specific region, we can use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    min(amount) AS min_sales\nFROM sales_data\nWHERE region = 'West';<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-----------+<br>| min_sales |<br>+-----------+<br>| 500.00    |<br>+-----------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Finding the Minimum Value in a Date Column<\/h2>\n\n\n\n<p class=\"\">Here&#8217;s an example of <code>min()<\/code> being applied to a date column:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    min(order_date) AS first_order\nFROM sales_data;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+-------------+<br>| first_order |<br>+-------------+<br>| 2023-01-01  |<br>+-------------+<\/pre>\n\n\n\n<p class=\"\">Here are the bottom two dates:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    min(order_date, 2) AS oldest_orders\nFROM sales_data;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------------------------+<br>|      oldest_orders       |<br>+--------------------------+<br>| [2023-01-01, 2023-02-15] |<br>+--------------------------+<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Using <code>min()<\/code> with <code>GROUP BY<\/code><\/h2>\n\n\n\n<p class=\"\">The <code>min()<\/code> function can be combined with the <a href=\"https:\/\/database.guide\/sql-group-by-clause-for-beginners\/\" data-type=\"post\" data-id=\"11949\"><code>GROUP BY<\/code> clause<\/a> to find the minimum value for each group in a table. For example, if we want to find the minimum sales amount for each region in the <code>sales<\/code> table, we can use:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    region, \n    min(amount) AS min_sales\nFROM sales_data\nGROUP BY region;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+--------+-----------+<br>| region | min_sales |<br>+--------+-----------+<br>| North  | 150.00    |<br>| South  | 50.00     |<br>| East   | 300.00    |<br>| West   | 500.00    |<br>+--------+-----------+<\/pre>\n\n\n\n<p class=\"\">This query returns the lowest sales amount for each region.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Using <code>min()<\/code> with Window Functions<\/h2>\n\n\n\n<p class=\"\">DuckDB also supports <a href=\"https:\/\/database.guide\/understanding-window-functions-in-sql\/\" data-type=\"post\" data-id=\"31280\">window functions<\/a>, which allow us to perform calculations across a set of table rows that are somehow related to the current row. <\/p>\n\n\n\n<p class=\"\">We can use the <code>min()<\/code> function as a window function to find the minimum value within a specific window of rows. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>SELECT \n    id, \n    amount, \n    region, \n    min(amount) OVER (PARTITION BY region) AS min_sales_in_region\nFROM sales_data\nORDER BY region;<\/code><\/pre>\n\n\n\n<p class=\"\">Result:<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">+----+--------+--------+---------------------+<br>| id | amount | region | min_sales_in_region |<br>+----+--------+--------+---------------------+<br>| 4  | 300.00 | East   | 300.00              |<br>| 6  | 400.00 | East   | 300.00              |<br>| 1  | 200.00 | North  | 150.00              |<br>| 3  | 150.00 | North  | 150.00              |<br>| 7  | 350.00 | North  | 150.00              |<br>| 2  | 50.00  | South  | 50.00               |<br>| 5  | 250.00 | South  | 50.00               |<br>| 10 | 450.00 | South  | 50.00               |<br>| 8  | 500.00 | West   | 500.00              |<br>| 9  | 600.00 | West   | 500.00              |<br>+----+--------+--------+---------------------+<\/pre>\n\n\n\n<p class=\"\">This query returns the minimum sales amount for each region, alongside each individual sale.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>DuckDB has a min() function just like most RDBMSs that returns the minimum value from a set of values. However, DuckDB&#8217;s implementation also allows us to return the bottom n minimum values, which is not something we see in most other RDBMSs. This article presents some examples of DuckDB&#8217;s implementation of the min() function, so &#8230; <a title=\"Examples that Demonstrate DuckDB&#8217;s MIN() Function\" class=\"read-more\" href=\"https:\/\/database.guide\/examples-that-demonstrate-duckdbs-min-function\/\" aria-label=\"Read more about Examples that Demonstrate DuckDB&#8217;s MIN() Function\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[174],"tags":[73,93,20],"class_list":["post-41856","post","type-post","status-publish","format-standard","hentry","category-duckdb","tag-aggregate-functions","tag-functions","tag-what-is"],"_links":{"self":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/41856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/comments?post=41856"}],"version-history":[{"count":5,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/41856\/revisions"}],"predecessor-version":[{"id":41870,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/posts\/41856\/revisions\/41870"}],"wp:attachment":[{"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/media?parent=41856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/categories?post=41856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/database.guide\/wp-json\/wp\/v2\/tags?post=41856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}