Sort by multiple criteria (with.MySQL)
I learned about sorting by multiple criteria (with.MySQL).
I want to solve the coding test problem, find out how to solve it differently from the retrospective of the problem I solved, and get to know.
Let’s get to the problem first.
Problem
Please write a SQL statement that looks up the ID, name, and protection start date of all animals entering the animal shelter in order of name.
However, among animals with the same name, you must first show the animals that started protecting later.
Problem Description
The ANIMAL_INS table is a table that contains information about animals that have entered the animal shelter.
ANIMAL_INS table structures are as follows: ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, SEX_UPON_INTAKE indicate the animal’s ID, species, start date of protection, status, name, gender, and neutralization at the start of protection, respectively.
ANIMAL_INS table
NAME | TYPE | NULLABLE |
---|---|---|
ANIMAL_ID | VARCHAR(N) | FALSE |
ANIMAL_TYPE | VARCHAR(N) | FALSE |
DATETIME | DATETIME | FALSE |
INTAKE_CONDITION | VARCHAR(N) | FALSE |
NAME | VARCHAR(N) | TRUE |
SEX_UPON_INTAKE | VARCHAR(N) | FALSE |
problem solving
SELECT ANIMAL_ID, NAME, DATETIME
FROM ANIMAL_INS
ORDER BY NAME, DATETIME DESC;
Solution Description
In the ANIMAL_INS table required in the problem, the ID, name, and protection start date of all animals were searched in order of name, and if there was an animal with the same name, it was printed to show the animal that started protection later.