Middle
SQL task: table with numbers from 1 to 100, return two columns — number and category (divisible by 15 → 3, by 3 → 1, by 5 → 2, otherwise → 0)
sobes.tech AI
Answer from AI
You can use the CASE statement in SQL to determine the category of a number:
SELECT number,
CASE
WHEN number % 15 = 0 THEN 3
WHEN number % 3 = 0 THEN 1
WHEN number % 5 = 0 THEN 2
ELSE 0
END AS category
FROM numbers_table
WHERE number BETWEEN 1 AND 100
ORDER BY number;
Here, numbers_table is a table with numbers from 1 to 100. The expression checks divisibility with priority: first by 15, then by 3, then by 5, otherwise 0.