We played a bit with using select ...
queries to search and aggregate information from db tables. But the most
scary situation is when you need to update some records on live database!
Now we have a list of persons who own some pets. We have a list of pets too - they are linked to owner with
owner
column, corresponding to id
in the first table. E.g. like this:
create table owners (id int, name text);
insert into owners values (1, 'Jake');
create table pets (id int, name text, breed text, owner int);
insert into pets values (10, 'Snoopy', 'dog', 1);
The task is very simple: update names of pets, adding their owner's name. E.g. the dog Snoopy
belonging to
Jake
in the example above should become Jake's Snoopy
.
Our database is slightly inconsistent this time (it happens in reality!) - some owner id's in pets
table could
be missing from owners
table (suppose, some owners were removed due to various reasons) - in such cases
simply leave the name as is.
There are no "output" for update
statement, so there are no example, but you'll figure out by checker
responses if anything is wrong. When testing your query in browser, simply add something like
select name from pets
(temporarily) after your update
statement, separating it with semicolon.