Can SQL calculate aggregate functions across multiple tables?

后端 未结 5 1771
北恋
北恋 2021-01-05 01:14

Let\'s say I have two existing tables, \"dogs\" and \"cats\":

 dog_name | owner
 ---------+------
 Sparky   | Bob
 Rover    | Bob
 Snoopy   | Chuck
 Odie             


        
5条回答
  •  感情败类
    2021-01-05 01:56

    If your database can handle it, I'd double down on FerranB's solution and write a geeky NATURAL FULL JOIN solution. I mean, when was the last time you got the chance to do so?

    SELECT owner, COUNT(dog_name), COUNT(cat_name)
    FROM cats 
    NATURAL FULL JOIN dogs
    GROUP BY owner
    

提交回复
热议问题