📔
Databases
  • Table of contents
  • SQL
    • Getting Started
      • PgAdmin Tool
    • Data Types
      • Arrays
      • Date/Time/Stamps
      • JSON
      • Internal Functions
      • Sequences
      • User Defined Data Types
    • Database
      • Schema
    • Table
      • Aggregation
      • Usefull Functions
      • Combining Tables
      • Constraints
      • Common Table Expression
      • GROUP BY and HAVING
      • OPERATORS
      • ORDER BY and DISTINCT
      • Views
      • WHERE Clause
    • Order of SQL Execution
    • Advance Table
      • Internals
      • Managing Tables
      • Partitioning Tables
      • Pivotal or Crosstab Tables
    • Joins
      • Cross & Natural Joins
      • Full, Multiple & Self Joins
      • Inner Join
      • Left and Right JOIN
    • Functions
      • Cursors
      • PL/pgSQL
      • Stored Procedures
      • Triggers
        • More on Triggers
    • Indexing
      • Custom Indexes
      • GIN Index
      • Indexes
      • SQL
      • Unique Index
    • Summarization
      • SubQueries
      • Window
  • MongoDB
    • Mongo Administration
    • MongoDB Aggregation
    • MQL
  • Redis
  • Cassandra
    • CQL
    • Data Modelling
      • Advance Data Modelling
    • Cassandra Administration
    • Cassandra Features
Powered by GitBook
On this page
  • count
  • Sum
  • Min and Max
  • Average, Greatest, Latest

Was this helpful?

  1. SQL
  2. Table

Aggregation

  • COUNT (column)

  • SUM (column)

  • MIN & MAX

  • LEAST & GREATEST

  • AVG

count

SELECT COUNT(DISTINCT (movie_lang))
from movies;

 count 
-------
     8

-- with where clause

SELECT COUNT(movie_lang)
FROM movies
where movie_lang = 'English';

 count 
-------
    38

Sum

select sum(revenues_domestic)
from movies_revenues;

  sum   
--------
 5719.5

select SUM(revenues_domestic::numeric)
from movies_revenues
where revenues_domestic::numeric > 200;

  sum   
--------
 3425.6

SELECT SUM(DISTINCT revenues_domestic)
FROM movies_revenues;

  sum   
--------
 5708.4

Min and Max

SELECT min(movie_length), MAX(movie_length)
FROM movies

 min | max 
-----+-----
  87 | 168

Average, Greatest, Latest

SELECT GREATEST(10, 20, 30, 40), LEAST(10, 20, 30, 40);

 greatest | least
----------+-------
       40 |    10

SELECT GREATEST('A', 'B', 'C', 'D'), LEAST('A', 'B', 'C', 'D');

 greatest | least
----------+-------
 D        | A

SELECT GREATEST('A', 'B', 'C', 1);

-- ERROR:  invalid input syntax for type integer: "A"
-- LINE 1: SELECT GREATEST('A', 'B', 'C', 1);

SELECT AVG(movie_length)
FROM movies;

         avg          
----------------------
 126.1320754716981132
PreviousTableNextUsefull Functions

Last updated 3 years ago

Was this helpful?