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 MAX

SQLite MAX

Summary: in this tutorial, you will learn how to use SQLite MAX function to get the maximum value of all values in a group.

Introduction to SQLite MAX function

The SQLite MAX function is an aggregate function that returns the maximum value of all values in a group. You can use the MAX function to accomplish a lot of things.

For example, you can use the MAX function to find the most expensive products, find the biggest item in its group, etc.

The following illustrates the basic syntax of the MAX function.

MAX([ALL|DISTINCT] expression);
Code language: SQL (Structured Query Language) (sql)

The expression can be a column of a table or an expression that consists of operands, which are the columns, and operators like +, *, etc.

There are some important notes about MAX function:

  • First, the MAX function ignores NULL values.
  • Second, unlike the COUNT function, the DISTINCT clause is not relevant to the MAX function.
  • Third, because a column can store mixed types of data e.g., integer, real, text, blob, and NULL in SQLite, when comparing values to find the maximum value, the MAX function uses the rules mentioned in the data types tutorial.

The SQLite MAX function examples

We’ll use the tracks table in the sample database for the demonstration.

To get the largest track in bytes, you apply the MAX function to the bytes column as the following statement:

SELECT MAX(bytes) FROM tracks;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite MAX function example

SQLite MAX function in the subquery example

To get the complete information of the biggest track, you use the subquery as follows:

SELECT TrackId, Name, Bytes FROM tracks WHERE Bytes = (SELECT MAX(Bytes) FROM tracks);
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite MAX function with Subquery

First, the inner query returns highest bytes of all tracks using the MAX function. Then, the outer query gets the largest track whose size equals the largest size returned by the subquery.

SQLite MAX function and GROUP BY clause example

You can find the largest track in each album using the MAX function with the GROUP BY clause.

First, the GROUP BY clause groups the tracks into groups based on albums. Then, the MAX function returns the largest tracks for each group.

See the following query:

SELECT AlbumId, MAX(bytes) FROM tracks GROUP BY AlbumId;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite MAX with GROUP BY clause example

SQLite MAX function and HAVING clause

You can combine the MAX function with the HAVING clause to filter the groups based on their largest values.

For example, to find the albums and their largest track where the sizes of the largest tracks are greater than 6 MB (about ~ 6000000), you use the following statement:

SELECT albumid, max(bytes) FROM tracks GROUP BY albumid HAVING MAX(bytes) > 6000000;
Code language: SQL (Structured Query Language) (sql)

Try It

SQLite MAX function with HAVING

In this tutorial, you have learned how to use the SQLite MAX function to find the maximum value in a group of values.

  • Was this tutorial helpful ?
  • YesNo
Previous SQLite COUNT
Next SQLite MIN

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.