Neat Way to select every month in SQL

I am trying to find number of users every month. This is my SQL which I learn from another question.

The part for creating number of month is easy to understand but it is long. I am wondering is there a neater way to write the same SQL. Thanks.

SELECT
    meses.MONTH,
    COUNT(Users.user_ID) AS num_of_user
FROM
    (
    SELECT
        1 AS MONTH
    UNION
SELECT
    2 AS MONTH
UNION
SELECT
    3 AS MONTH
UNION
SELECT
    4 AS MONTH
UNION
SELECT
    5 AS MONTH
UNION
SELECT
    6 AS MONTH
UNION
SELECT
    7 AS MONTH
UNION
SELECT
    8 AS MONTH
UNION
SELECT
    9 AS MONTH
UNION
SELECT
    10 AS MONTH
UNION
SELECT
    11 AS MONTH
UNION
SELECT
    12 AS MONTH
) AS meses
LEFT JOIN
    Users
ON
    meses.month = MONTH(Users.joint_date) AND YEAR(Users.joint_date) = '2000'
GROUP BY
    meses.MONTN


Read more here: https://stackoverflow.com/questions/64952242/neat-way-to-select-every-month-in-sql

Content Attribution

This content was originally published by Hellllllllllllllllllllllllllll at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: