Skip to main content

Advanced Queries

Updated Aug 23, 2019 ·

Films Database

We'll use a films database with the four tables listed below.

films table
idtitlerelease_yearcountrydurationlanguagecertificationgrossbudget
1Inception2010USA148EnglishPG-13829895144160000000
2Parasite2019South Korea132KoreanR25759015211400000
3The Godfather1972USA175EnglishR2461209866000000
4Amélie2001France122FrenchR17420000010000000
5Coco2017USA105SpanishPG807082196175000000
people table
idnamebirthdatedeathdate
1Leonardo DiCaprio1974-11-11NULL
2Bong Joon-ho1969-09-14NULL
3Marlon Brando1924-04-032004-07-01
4Hayao Miyazaki1941-01-05NULL
5Christopher Nolan1970-07-30NULL
6Tom Hardy1977-09-14NULL
7Quentin Tarantino1963-03-27NULL
8Sofia Coppola1971-05-14NULL
9Natalie Portman1981-06-09NULL
10Ridley Scott1937-11-30NULL
11Emma Watson1990-04-15NULL
reviews table
idfilm_idnum_usernum_criticimdb_scorenum_votesfacebook_likes
11100002508.820500001500000
2285003008.66000001200000
3375001509.21600000900000
4490002008.6650000800000
5595002758.615000001000000
61110002609.021000001550000
7287003108.76200001250000
8376001609.31650000950000
roles table
idfilm_idperson_idrole
111Actor
222Director
333Actor
444Director
555Director
616Actor
727Director
838Actor
949Director
10510Director
11111Actor
1221Actor
1332Director
1443Actor
1554Director
1615Actor

The database schema outlines table names, field names, and data types.

TableFieldData Type
filmsidINT4
titleVARCHAR
release_yearINT4
countryVARCHAR
durationINT4
languageVARCHAR
certificationVARCHAR
grossINT8
budgetINT8
peopleidINT4
nameVARCHAR
birthdateDATE
deathdateDATE
reviewsidINT4
film_idINT4
num_userINT4
num_criticINT4
imdb_scoreFLOAT4
num_votesINT4
facebook_likesINT4
rolesidINT4
film_idINT4
person_idINT4
roleVARCHAR

COUNT

The COUNT function returns the number of records with a value in a specified field. To count birth dates in the people table, use:

SELECT COUNT(birthdate) AS count_birthdates
FROM people;

Expected output:

count_birthdates
11

COUNT Multiple Fields

To count more than one field, use COUNT multiple times. For example, to count names and birth dates:

SELECT COUNT(name) AS count_names, COUNT(birthdate) AS count_birthdates
FROM people;

Expected Output:

count_namescount_birthdates
1111

Using * with COUNT

To count the number of records in a table, use COUNT with an asterisk:

SELECT COUNT(*) AS total_records
FROM people;

Expected Output:

total_records
11

DISTINCT

The DISTINCT keyword selects unique values from a field, removing duplicates. For example, to see unique languages in the films table:

SELECT DISTINCT(language)
FROM films;

Expected Output:

language
English
Korean
French
Spanish

COUNT with DISTINCT

Combining COUNT with DISTINCT counts the number of unique values in a field. For instance, to count distinct birth dates in the people table:

SELECT COUNT(DISTINCT birthdate) AS unique_birthdates
FROM people;

Expected Output:

unique_birthdates
10

The number of unique birth dates differs from the total count because some people share the same birthday.