Contents

Get the right number of members (with.MySQL)

   Nov 19, 2024     1 min read

This is an article about finding the number of members who meet the conditions (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 USER_INFO table, please write a SQL statement that will print out how many of the members who joined in 2021 are over 20 years old and under 29 years old.

Problem Description

The following is a USER_INFO table that contains membership information that you have signed up for a clothing shopping mall.

The USER_INFO table is structured as below, and USER_ID, GENDER, AGE, and JOINED represent the membership ID, gender, age, and date of joining, respectively.

USER_INFO Table

Column nameTypeNullable
USER_IDINTEGERFALSE
GENDERTINYINT(1)TRUE
AGEINTEGERTRUE
JOINEDDATEFALSE

The GENDER column is empty or has a value of 0 or 1, where 0 represents a man and 1 represents a woman.

problem solving

SELECT COUNT(USER_ID) AS USERS
FROM USER_INFO
WHERE YEAR(JOINED) = '2021' AND AGE >= 20 AND AGE <= 29;

Solution Description

The COUNT function was used to count the users who met the conditions.

I wrote the 2021 and age limit in the conditional statement and filtered it.