blog.itcode.devblog.itcode.dev

[Programmers / MySQL] Level 2 How Many Cats and Dogs Are There (59040)

The ANIMAL_INS table holds information about animals that came into the animal shelter. The ANIMAL_INS table structure is as follows, where ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, and SEX_UPON_INTAKE respectively represent the animal's ID, species, intake date, condition at intake, name, and sex/neuter status.

[Programmers / MySQL] Level 2 How Many Cats and Dogs Are There (59040)

The ANIMAL_INS table holds information about animals that came into the animal shelter. The ANIMAL_INS table structure is as follows, where ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, and SEX_UPON_INTAKE respectively represent the animal's ID, species, intake date, condition at intake, name, and sex/neuter status.
RWB0104
@RWBwritten at 2021-12-28 08:19:20
Programmers

시리즈 모아보기

Programmers

74 / 78
RankLanguage Used
Level 1

🖼️ MySQL

🔗 How Many Cats and Dogs Are There

The ANIMAL_INS table holds information about animals that came into the animal shelter. The ANIMAL_INS table structure is as follows, where ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, and SEX_UPON_INTAKE respectively represent the animal's ID, species, intake date, condition at intake, name, and sex/neuter status.

NAMETYPENULLABLE
ANIMAL_IDVARCHAR(N)FALSE
ANIMAL_TYPEVARCHAR(N)FALSE
DATETIMEDATETIMEFALSE
INTAKE_CONDITIONVARCHAR(N)FALSE
NAMEVARCHAR(N)TRUE
SEX_UPON_INTAKEVARCHAR(N)FALSE

Write a SQL statement that finds how many cats and how many dogs came into the animal shelter. Retrieve cats before dogs.

For example, if the ANIMAL_INS table is as follows

ANIMAL_IDANIMAL_TYPEDATETIMEINTAKE_CONDITIONNAMESEX_UPON_INTAKE
A373219Cat2014-07-29 11:43:00NormalEllaSpayed Female
A377750Dog2017-10-25 17:17:00NormalLucySpayed Female
A354540Cat2014-12-11 11:48:00NormalTuxNeutered Male

There are 2 cats and 1 dog. So running the SQL statement should produce the following.

Therefore, running the SQL statement should produce the following.

ANIMAL_TYPEcount
Cat2
Dog1
  1. Retrieve animals whose ANIMAL_TYPE is Cat or Dog.
  2. Retrieve ANIMAL_TYPE and the count.
  3. Retrieve them ordered alphabetically by ANIMAL_TYPE.

Using GROUP BY, we can group by ANIMAL_TYPE to get the counts of Cat and Dog. Use ORDER BY to sort alphabetically by ANIMAL_TYPE.

SQL

SELECT ANIMAL_TYPE, COUNT(*) FROM ANIMAL_INS GROUP BY ANIMAL_TYPE ORDER BY ANIMAL_TYPE;
# Programmers# Algorithm# SQL# Level 2
ship
blog.itcode.dev

Notes from the π-th Alpaca

7.0.1
Developed by RWB since 2021.057th upgraded at 2026.08