[Programmers / MySQL] Level 1 Find the Maximum Value (59415)
[Programmers / MySQL] Level 1 Find the Maximum Value (59415)
| Rank | Language Used |
|---|---|
| Level 1 | 🖼️ JAVA |
The ANIMAL_INS table holds information about animals that have come into an animal shelter. The ANIMAL_INS table structure is as follows, where ANIMAL_ID, ANIMAL_TYPE, DATETIME, INTAKE_CONDITION, NAME, and SEX_UPON_INTAKE represent the animal's ID, species, intake date, condition at intake, name, and sex/neuter status, respectively.
| 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 |
Write an SQL statement that finds when the most recently admitted animal came in.
For example, if the ANIMAL_INS table is as follows
| ANIMAL_ID | ANIMAL_TYPE | DATETIME | INTAKE_CONDITION | NAME | SEX_UPON_INTAKE |
|---|---|---|---|---|---|
| A399552 | Dog | 2013-10-14 15:38:00 | Normal | Jack | Neutered Male |
| A379998 | Dog | 2013-10-23 11:42:00 | Normal | Disciple | Intact Male |
| A370852 | Dog | 2013-11-03 15:04:00 | Normal | Katie | Spayed Female |
| A403564 | Dog | 2013-11-18 17:03:00 | Normal | Anna | Spayed Female |
The animal that was admitted most recently is Anna, and Anna was admitted at 2013-11-18 17:03:00. So running the SQL statement should produce the following.
| TIME |
|---|
| 2013-11-18 17:03:00 |
- The column name (labeled "TIME" in the example above) does not need to match exactly.
Query ANIMAL_INS, and retrieve only the single topmost record with the most recent DATETIME.
In MySQL, LIMIT 1 is used to retrieve only the single topmost record.
SQL
SELECT DATETIME FROM ANIMAL_INS ORDER BY DATETIME DESC LIMIT 1;
