Contents

Outputting a list of female patients under the age of 12 (with. MySQL)

   Nov 4, 2024     2 min read

This article is about printing a list of female patients under the age of 12 (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

In the PATIENT table, please write a SQL statement that looks up the patient name, patient number, gender code, age, and phone number of a female patient under the age of 12.

If you don’t have a phone number, please print it out as ‘NONE’ and arrange the results in descending order based on age, and if you are of the same age, arrange it in ascending order based on patient name.

The following is a PATIENT table containing patient information registered in a general hospital.

The PATIENT table is as follows, and PT_NO, PT_NAME, GEND_CD, AGE, and TLNO mean patient number, patient name, gender code, age, and phone number, respectively.

PATIENT Table

Column nameTypeNullable
PT_NOVARCHAR(10)FALSE
PT_NAMEVARCHAR(20)FALSE
GEND_CDVARCHAR(1)FALSE
AGEINTEGERFALSE
TLNOVARCHAR(50)TRUE

problem solving

SELECT PT_NAME, PT_NO, GEND_CD, AGE, IFNULL(TLNO, "NONE") AS TLNO
FROM PATIENT
WHERE AGE <= 12 AND GEND_CD = "W"
ORDER BY AGE DESC, PT_NAME ASC

Solution Description

NULL values were processed through the IFNULL function, and age and gender conditions were grouped into AND.

Sorting was in descending order by age, and if the age was the same, sorted in ascending order by patient name.