inblog logo
|
silver
    SQL문제풀기

    [SQL문제풀기] Consecutive Numbers

    silver's avatar
    silver
    Nov 09, 2025
    [SQL문제풀기] Consecutive Numbers
    Contents
    문제MySQL

    문제

    Consecutive Numbers - LeetCode
    Can you solve this real interview question? Consecutive Numbers - Table: Logs +-------------+---------+ | Column Name | Type | +-------------+---------+ | id | int | | num | varchar | +-------------+---------+ In SQL, id is the primary key for this table. id is an autoincrement column starting from 1.   Find all numbers that appear at least three times consecutively. Return the result table in any order. The result format is in the following example.   Example 1: Input: Logs table: +----+-----+ | id | num | +----+-----+ | 1 | 1 | | 2 | 1 | | 3 | 1 | | 4 | 2 | | 5 | 1 | | 6 | 2 | | 7 | 2 | +----+-----+ Output: +-----------------+ | ConsecutiveNums | +-----------------+ | 1 | +-----------------+ Explanation: 1 is the only number that appears consecutively for at least three times.
    Consecutive Numbers - LeetCode
    https://leetcode.com/problems/consecutive-numbers/description/?envType=study-plan-v2&envId=top-sql-50
    Consecutive Numbers - LeetCode

    MySQL

    내가 작성한 정답

    select distinct l1.num ConsecutiveNums from Logs l1 left join Logs l2 on l1.id+1 = l2.id left join Logs l3 on l2.id+1 = l3.id where l1.num = l2.num and l1.num= l3.num;
    Share article

    silver

    RSS·Powered by Inblog