0

I am new to psql. I have a table 'pets' as below.

    name     | species |       owner        | gender |     color
-------------+---------+--------------------+--------+---------------
 Nagini      | snake   | Lord Voldemort     | female | green
 Hedwig      | owl     | Harry Potter       | female | snow white
 Scabbers    | rat     | Ron Weasley        | male   | unspecified
 Pigwidgeon  | owl     | Ron Weasley        | male   | grey
 Crookshanks | cat     | Herminone Granger  | male   | ginger
 Mrs Norris  | cat     | Argus Filch        | female | dust-coloured
 Trevor      | toad    | Neville Longbottom | male   | brown

How can I concatenate strings and output the string in a way that it gives "Ron Weasley has X pets" (where X = 2)?

I know 'select count(name) from pets where owner = 'Ron Weasley';' for the X=2 part but not sure about how to concatencate and output the string in PSQL.

Meruemu
  • 611
  • 1
  • 8
  • 28

1 Answers1

0
SELECT p.owner||' has ' count(p.name)||' pets' as conc_output
  FROM pets p
 WHERE owner = 'Ron Weasley';