Contents

Get the number you caught (with.MySQL)

   Nov 21, 2024     2 min read

This is an article about saving the number caught (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 outputs the number of fish that are 10cm or less in length among the caught fish.

Please name the column representing the number of fish as FISH_COUNT.

Problem Description

The FISH_INFO table used by the fishing app contains information on fish caught.

The structure of the FISH_INFO table is as follows, and ID, FISH_TYPE, LENGTH, and TIME indicate the ID of the fish caught, the type of fish (number), the length of the fish caught (cm), and the date of the fish caught.

FISH_INFO Table

Column nameTypeNullable
IDINTEGERFALSE
FISH_TYPEINTEGERFALSE
LENGTHFLOATTRUE
TIMEDATEFALSE

However, if the caught fish is less than 10 cm long, the LENGTH is NULL, and there is no NULL in the LENGTH.

problem solving

SELECT COUNT(*) AS FISH_COUNT
FROM FISH_INFO
WHERE LENGTH <= 10 OR LENGTH IS NULL;

Solution Description

This SQL query calculates and returns the number of fish that meet a specific condition.

The query extracts data from the FISH_INFO table, and the main components are.

First, the SELECT section specifies the value to look up.

COUNT(*) AS FISH_COUNT calculates the number of records (i.e., fish) that meet the condition and outputs the result under the alias FISH_COUNT.

This means the total number of fish that meet the conditions.

The FROM section then specifies the default table on which to run the query.

In this case, the FISH_INFO table is used.

Next, in the WHERE section, you set specific conditions to filter the data you need.

The condition, LENGTH IS NULL, selects a fish with no length information, i.e., a value of NULL.

This query calculates the total number of fish with a length of 10 or less and returns them to FISH_COUNT.