SQLite Tutorial

  • Home
  • Start Here
  • Views
  • Indexes
  • Triggers
  • Functions
    • Aggregate Functions
    • Date Functions
    • String Functions
    • Window Functions
  • API
    • SQLite Python
    • SQLite Node.js
    • SQLite Java
    • SQLite PHP
  • Try It
Home / SQLite Aggregate Functions / SQLite MIN

SQLite MIN

Summary: in this tutorial, you will learn how to use SQLite MIN function to get the minimum value in a set of values.

Introduction to SQLite MIN function

The following illustrates the syntax of the SQLite MIN function:

MIN([ALL|DISTINCT] expression);

The MIN function uses ALL by default. Similar to the MAX function, the MIN function ignores NULL values. Therefore, it returns the non-NULL minimum value in a set of values.

The expression can be a column or an expression that consists of columns and operators.

Note that the DISTINCT is not relevant to the MIN function.

If you use the SELECT statement with ORDER BY and WHERE clauses, the first minimum non-null value appears at the first row of the result set.

SELECT column FROM table WHERE column IS NOT NULL ORDER BY column DESC;

Try It

When does the MIN function return a NULL value? We’re glad that you asked.

The MIN function returns a NULL value if and only if there are only NULL values in the set.

SQLite MIN function examples

We will use the tracks table in the sample database for demonstrating the MIN function.

tracks table

To get the shortest track, you use the MIN function as follows:

SELECT min(Milliseconds) FROM tracks;

Try It

SQLite MIN function example

SQLite MIN function in a subquery

To get the complete shortest track information, you need to use a subquery.

The outer query gets the complete information from the tracks table based on the milliseconds returned by the subquery that uses the MIN function.

See the following query.

SELECT trackid, name, milliseconds FROM tracks WHERE milliseconds = ( SELECT min(Milliseconds) FROM tracks );

Try It

SQLite MIN function with the GROUP BY clause example

You can find the shortest track per album using the MIN function with GROUP BY clause. The GROUP BY clause groups a set of rows into groups. The MIN function finds the minimum value for each group.

The following statement illustrates the idea:

SELECT albumid, min(milliseconds) FROM tracks GROUP BY albumid;

Try It

SQLite MIN function with the HAVING clause example

You can use the HAVING clause to filter groups. For example, when combining with the MIN function, you can find the albums and their shortest tracks where the length of each shortest track is less than 10 seconds.

SELECT albumid, min(milliseconds) FROM tracks GROUP BY albumid HAVING MIN(milliseconds) < 10000;

Try It

SQLite MIN Function with HAVING clause

In this tutorial, you have learned how to use the SQLite MIN function to find the minimum value in a set of values.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite MAX
Next SQLite SUM

Getting Started

  • What Is SQLite
  • Download & Install SQLite
  • SQLite Sample Database
  • SQLite Commands

SQLite Tutorial

  • SQLite Select
  • SQLite Order By
  • SQLite Select Distinct
  • SQLite Where
  • SQLite Limit
  • SQLite BETWEEN
  • SQLite IN
  • SQLite Like
  • SQLite IS NULL
  • SQLite GLOB
  • SQLite Join
  • SQLite Inner Join
  • SQLite Left Join
  • SQLite Cross Join
  • SQLite Self-Join
  • SQLite Full Outer Join
  • SQLite Group By
  • SQLite Having
  • SQLite Union
  • SQLite Except
  • SQLite Intersect
  • SQLite Subquery
  • SQLite EXISTS
  • SQLite Case
  • SQLite Insert
  • SQLite Update
  • SQLite Delete
  • SQLite Replace
  • SQLite Transaction

SQLite Data Definition

  • SQLite Data Types
  • SQLite Date & Time
  • SQLite Create Table
  • SQLite Primary Key
  • SQLite Foreign Key
  • SQLite NOT NULL Constraint
  • SQLite UNIQUE Constraint
  • SQLite CHECK constraints
  • SQLite AUTOINCREMENT
  • SQLite Alter Table
  • SQLite Rename Column
  • SQLite Drop Table
  • SQLite Create View
  • SQLite Drop View
  • SQLite Index
  • SQLite Expression-based Index
  • SQLite Trigger
  • SQLite VACUUM
  • SQLite Transaction
  • SQLite Full-text Search

SQLite Tools

  • SQLite Commands
  • SQLite Show Tables
  • SQLite Describe Table
  • SQLite Dump
  • SQLite Import CSV
  • SQLite Export CSV

SQLite Functions

  • SQLite AVG
  • SQLite COUNT
  • SQLite MAX
  • SQLite MIN
  • SQLite SUM

SQLite Interfaces

  • SQLite PHP
  • SQLite Node.js
  • SQLite Java
  • SQLite Python

About SQLite Tutorial

SQLite Tutorial website helps you master SQLite quickly and easily. It explains the complex concepts in simple and easy-to-understand ways so that you can both understand SQLite fast and know how to apply it in your software development work more effectively.

Looking for a tutorial…

If you did not find the tutorial that you are looking for, you can use the following search box. In case the tutorial is not available, you can request for it using the request for a SQLite tutorial form.

Recent Tutorials

  • SQLite IIF
  • SQLite Generated Columns
  • SQLite Getting Started
  • SQLite Programming Interfaces
  • SQLite Concat
  • SQLite INSTEAD OF Triggers
  • SQLite Join
  • SQLite IS NULL

Site Links

  • Home
  • About
  • Contact
  • Resources
  • Privacy Policy

Copyright © 2021 SQLite Tutorial. All Rights Reserved.