Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions consecutive-numbers.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
"""
+-------------+---------+
| 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.

Input:
Logs table:
+----+-----+
| id | num |
+----+-----+
| 1 | 1 |
| 2 | 1 |
| 3 | 1 |
| 4 | 2 |
| 5 | 1 |
| 6 | 2 |
| 7 | 2 |
+----+-----+
Output:
+-----------------+
| ConsecutiveNums |
+-----------------+
| 1 |
+-----------------+

"""

-- method 1 : using LEAD()

with cte as
(
select
num,
lead(num, 1) over() num1,
lead(num, 2) over() num2
from
logs
)

select
distinct num as ConsecutiveNums
from
cte
where
num = num1 and num = num2


-- method 2: using DISTINCT and WHERE

select
distinct l1.num as ConsecutiveNums
from
logs l1, logs l2, logs l3
where
l1.id = l2.id - 1
and
l2.id = l3.id - 1
and
l1.num = l2.num
and
l1.num = l3.num
84 changes: 84 additions & 0 deletions the-number-of-passengers-in-each-bus-i.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
"""
Table: Buses
+--------------+------+
| Column Name | Type |
+--------------+------+
| bus_id | int |
| arrival_time | int |
+--------------+------+
bus_id is the column with unique values for this table.
Each row of this table contains information about the arrival time of a bus at the LeetCode station.
No two buses will arrive at the same time.

Table: Passengers
+--------------+------+
| Column Name | Type |
+--------------+------+
| passenger_id | int |
| arrival_time | int |
+--------------+------+
passenger_id is the column with unique values for this table.
Each row of this table contains information about the arrival time of a passenger at the LeetCode station.

Buses and passengers arrive at the LeetCode station.
If a bus arrives at the station at time tbus and a passenger arrived at time tpassenger where tpassenger <= tbus and the passenger did not catch any bus, the passenger will use that bus.

Write a solution to report the number of users that used each bus.

Return the result table ordered by bus_id in ascending order.

The result format is in the following example.

Input:
Buses table:
+--------+--------------+
| bus_id | arrival_time |
+--------+--------------+
| 1 | 2 |
| 2 | 4 |
| 3 | 7 |
+--------+--------------+
Passengers table:
+--------------+--------------+
| passenger_id | arrival_time |
+--------------+--------------+
| 11 | 1 |
| 12 | 5 |
| 13 | 6 |
| 14 | 7 |
+--------------+--------------+
Output:
+--------+----------------+
| bus_id | passengers_cnt |
+--------+----------------+
| 1 | 1 |
| 2 | 0 |
| 3 | 3 |
+--------+----------------+
"""

-- method 1 : using LEFT JOIN and GROUP BY

select
b.bus_id,
count(used_bus.t) as passengers_cnt
from
buses b
left join
(
select
p.passenger_id, min(b.arrival_time) as t
from
Passengers p join buses b
on
p.arrival_time <= b.arrival_time
group by
p.passenger_id
) as used_bus

on
b.arrival_time = used_bus.t
group by
b.bus_id
order by
b.bus_id
60 changes: 60 additions & 0 deletions user-activity-for-the-past-30-days-i.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
"""
Table: Activity
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| user_id | int |
| session_id | int |
| activity_date | date |
| activity_type | enum |
+---------------+---------+
This table may have duplicate rows.
The activity_type column is an ENUM (category) of type ('open_session', 'end_session', 'scroll_down', 'send_message').
The table shows the user activities for a social media website.
Note that each session belongs to exactly one user.

Write a solution to find the daily active user count for a period of 30 days ending 2019-07-27 inclusively.
A user was active on someday if they made at least one activity on that day.

Return the result table in any order.

The result format is in the following example.

Input:
Activity table:
+---------+------------+---------------+---------------+
| user_id | session_id | activity_date | activity_type |
+---------+------------+---------------+---------------+
| 1 | 1 | 2019-07-20 | open_session |
| 1 | 1 | 2019-07-20 | scroll_down |
| 1 | 1 | 2019-07-20 | end_session |
| 2 | 4 | 2019-07-20 | open_session |
| 2 | 4 | 2019-07-21 | send_message |
| 2 | 4 | 2019-07-21 | end_session |
| 3 | 2 | 2019-07-21 | open_session |
| 3 | 2 | 2019-07-21 | send_message |
| 3 | 2 | 2019-07-21 | end_session |
| 4 | 3 | 2019-06-25 | open_session |
| 4 | 3 | 2019-06-25 | end_session |
+---------+------------+---------------+---------------+
Output:
+------------+--------------+
| day | active_users |
+------------+--------------+
| 2019-07-20 | 2 |
| 2019-07-21 | 2 |
+------------+--------------+

"""

-- method 1: using DATEDIFF and COUNT

select
activity_date as 'day',
count(distinct user_id) as active_users
from
Activity
where
datediff('2019-07-27', activity_date) between 0 and 29
group by
activity_date