Sobes.tech
Junior — Senior

SQL query to get the owner's name, pet's name, and their age difference

livecode

Task condition

Construct a query that outputs the owner's name under the alias keeper_name, the pet's name under the alias animal_name, and the difference between the owner's age and the pet's age as age_gap. Include all rows from the owners table; if the owner's name is missing (NULL), replace it with "N/A", and set age_gap to 0 in such cases. The sorting should be first by the owner's name in alphabetical order, then by the descending order of the age difference.

owners table:

ID Name Age Pet_ID
1 Aubrey Little 20 1
2 Chett Crawfish 45 3
3 Jules Spinner 10 4
4 Magnus Burnsides 9 2
5 Veronica Dunn 8 NULL

pets table:

ID Name Age Animal
1 Dr. Harris Bonkers 1 Rabbit
2 Moon 9 Dog
3 Ripley 7 Cat
4 Tom 2 Cat
5 Maisie 10 Dog