나는 이렇게 학습한다/Algorithm & SQL

SQL Basics: Simple JOIN with COUNT

daco2020 2022. 7. 19. 19:28
반응형

or this challenge you need to create a simple SELECT statement that will return all columns from the people table, and join to the toys table so that you can return the COUNT of the toys

people table schema

  • id
  • name

toys table schema

  • id
  • name
  • people_id

You should return all people fields as well as the toy count as "toy_count".

 

Solution:

SELECT
  p.id,
  p.name,
  COUNT(t.name) AS toy_count 
FROM 
  people p
JOIN 
  toys t 
  ON 
    p.id = t.people_id
GROUP BY
  p.id

 

Result:

id name toy_count
2 Baumbach, Greenfelder and Nader 4
1 Harvey, Moen and Considine 6

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Anagram Detection  (0) 2022.07.21
Sum of all the multiples of 3 or 5  (0) 2022.07.20
SQL Basics: Simple HAVING  (0) 2022.07.19
Hello SQL World!  (0) 2022.07.17
GROCERY STORE: Logistic Optimisation  (0) 2022.07.16