Yogesh Chauhan's Blog

MIN, MAX, COUNT, AVG and SUM in SQL

in SQL/MySQL on August 24, 2019

In this short blog post I am going to discuss about the functions in SQL with examples. Let's start with the MIN and MAX.

  1. MIN(): Returns the smallest value from the selected COLUMN.

Syntax:


SELECT MIN(column_name)
FROM table_name
WHERE condition;

Example:


SELECT MIN(LifeExpectancy) AS LifeExpectancy FROM country;

Returns:
Lowest Life Expectancy
37.2
  1. MAX(): Returns the largest value from the selected COLUMN.

Syntax: 


SELECT MAX(column_name)
FROM table_name
WHERE condition;

Example:


SELECT MAX(LifeExpectancy) AS LifeExpectancy FROM country;

Returns:
Highest Life Expectancy
83.5
  1. COUNT(): Returns the number of rows for the applied condition.

Syntax:


SELECT COUNT(column_name)
FROM table_name
WHERE condition;

Example:


SELECT COUNT(ID) AS ID FROM city;

Returns:
Total Countries
4079
  1. AVG(): Returns the average for a column. The column has to be numeric or it'll show an error.

Syntax:


SELECT AVG(column_name)
FROM table_name
WHERE condition;

Example:


SELECT AVG(Population) AS Population FROM country;

Returns:
Average Population
25434098.1172
  1. SUM(): Returns the sum for a column. The column has to be numeric or it'll show an error.

Syntax:


SELECT SUM(column_name)
FROM table_name
WHERE condition;

Example:


SELECT SUM(Population) AS Population FROM country;

Returns:
Total Population
6078749450

 


Most Read

#1 How to check if radio button is checked or not using JavaScript? #2 How to add Read More Read Less Button using JavaScript? #3 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #4 How to uninstall Cocoapods from the Mac OS? #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to Use SQL MAX() Function with Dates?

Recently Posted

#Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface #Jan 14 How to display a student’s individual transcript in Colleague? #Jan 11 How to install PuTTY on a MacOS?
You might also like these
How to scroll contents of a an element vertically using JavaScript?JavaScriptLearn to Establish Connection using MySQLi (object-oriented), MySQLi (procedural) and PDO with Example CodePHPFull and Partial ROLLUP in Postgresql with ExamplesPostgresWhat is a “promise” in JavaScript?JavaScriptHow to control file extensions with an .htaccess file?MiscellaneousHow to add Lifecycle Methods to a Class in React?React