Top Reptile Observations
Beginner Mode

Scenario

You are a herpetologist studying reptiles and amphibians, with separate tables tracking field observation records and species information.

Task

Write a Snowflake SQL query that:

  1. Joins {{ ref("observations") }} with {{ ref("species") }} on species_id using an inner join
  2. Ranks all rows by count in descending order using RANK()
  3. Returns only the rows where the rank is 3 or better (ties at rank 3 are included)
  4. Returns the columns obs_id, species_id, species_name, location_id, and count

Schema

observations

Column Type Description
obs_id Integer Unique identifier of the observation
species_id Integer Identifier of the species observed
location_id Integer Identifier of the observation location
count Integer Number of individuals observed

species

Column Type Description
species_id Integer Unique identifier of the species
species_name String Common name of the species

Example

observations:

obs_id species_id location_id count
1 200 10 72
2 201 11 48
3 200 10 61
4 202 12 33
5 201 11 55

species:

species_id species_name
200 Chameleon
201 Tortoise
202 Skink
203 Tuatara

Expected Output:

obs_id species_id species_name location_id count
1 200 Chameleon 10 72
3 200 Chameleon 10 61
5 201 Tortoise 11 55

Note: Observation 4 (count 33) and observation 2 (count 48) fall below rank 3 and are excluded. Species 203 (Tuatara) has no observations and does not appear in the output.

Quick Solution

Code Environment

Sign in or try as guest to run your code.

Sign In

Track

Question Difficulty Company Access
Need more practice in this area? Explore more questions →