Grouping similarly weighted animals

Question

You're given the following table showing the weights (in pounds) of various animals:

Table: animal_weights

animal weight
Blue whale 136000
Humpback whale 30000
Gray whale 28500
Aardvark 60
Leopard 54
Cheetah 54
... ...

Write a SQL query that evaluates each animal in our list and returns all animals within the same list that are within 10% of the evaluating animal's weight. For example, if we're evaluating the humpback whale, we would want to aggregate a list of animals that are between 27,000 and 33,000 pounds. Given the partial table above, our query should return:

animal similar_weight
Blue whale null
Humpback whale Grey whale
Gray whale Humpback whale
Aardvark Leopard, Cheetah
Leopard Cheetah
Cheetah Leopard

You can view/query the table in an interactive SQL fiddle here.

Solution

Access restricted

Subscribe to premium account to see the solution.

Get premium now