From 2d87d15f096e137afa62c6922631dca2c9fd5ba7 Mon Sep 17 00:00:00 2001 From: ankitabmungalpara Date: Thu, 12 Dec 2024 11:28:51 -0500 Subject: [PATCH 1/3] Create consecutive-numbers.sql --- consecutive-numbers.sql | 72 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 consecutive-numbers.sql diff --git a/consecutive-numbers.sql b/consecutive-numbers.sql new file mode 100644 index 0000000..c96d43c --- /dev/null +++ b/consecutive-numbers.sql @@ -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 From 2db312bbca4765d2792643ec59e1c443d221e0c0 Mon Sep 17 00:00:00 2001 From: ankitabmungalpara Date: Thu, 12 Dec 2024 13:34:46 -0500 Subject: [PATCH 2/3] Create the-number-of-passengers-in-each-bus-i.sql --- the-number-of-passengers-in-each-bus-i.sql | 84 ++++++++++++++++++++++ 1 file changed, 84 insertions(+) create mode 100644 the-number-of-passengers-in-each-bus-i.sql diff --git a/the-number-of-passengers-in-each-bus-i.sql b/the-number-of-passengers-in-each-bus-i.sql new file mode 100644 index 0000000..36fd09f --- /dev/null +++ b/the-number-of-passengers-in-each-bus-i.sql @@ -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 From 010e7949b9b1319ef3dde5ea996b368527012635 Mon Sep 17 00:00:00 2001 From: ankitabmungalpara Date: Thu, 12 Dec 2024 13:51:37 -0500 Subject: [PATCH 3/3] Create user-activity-for-the-past-30-days-i.sql --- user-activity-for-the-past-30-days-i.sql | 60 ++++++++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 user-activity-for-the-past-30-days-i.sql diff --git a/user-activity-for-the-past-30-days-i.sql b/user-activity-for-the-past-30-days-i.sql new file mode 100644 index 0000000..8952202 --- /dev/null +++ b/user-activity-for-the-past-30-days-i.sql @@ -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