diff --git a/combine-two-tables.sql b/combine-two-tables.sql new file mode 100644 index 0000000..fe9bee8 --- /dev/null +++ b/combine-two-tables.sql @@ -0,0 +1,63 @@ +""" +Table: Person ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| personId | int | +| lastName | varchar | +| firstName | varchar | ++-------------+---------+ +personId is the primary key (column with unique values) for this table. +This table contains information about the ID of some persons and their first and last names. + + +Table: Address ++-------------+---------+ +| Column Name | Type | ++-------------+---------+ +| addressId | int | +| personId | int | +| city | varchar | +| state | varchar | ++-------------+---------+ +addressId is the primary key (column with unique values) for this table. +Each row of this table contains information about the city and state of one person with ID = PersonId. + +Write a solution to report the first name, last name, city, and state of each person in the Person table. +If the address of a personId is not present in the Address table, report null instead. + +Return the result table in any order. + +The result format is in the following example. + +Input: +Person table: ++----------+----------+-----------+ +| personId | lastName | firstName | ++----------+----------+-----------+ +| 1 | Wang | Allen | +| 2 | Alice | Bob | ++----------+----------+-----------+ +Address table: ++-----------+----------+---------------+------------+ +| addressId | personId | city | state | ++-----------+----------+---------------+------------+ +| 1 | 2 | New York City | New York | +| 2 | 3 | Leetcode | California | ++-----------+----------+---------------+------------+ +Output: ++-----------+----------+---------------+----------+ +| firstName | lastName | city | state | ++-----------+----------+---------------+----------+ +| Allen | Wang | Null | Null | +| Bob | Alice | New York City | New York | ++-----------+----------+---------------+----------+ + +""" + +select + firstName, lastName, city, state +from + Person p left join Address a +on + p.personId = a.personId diff --git a/customers-with-strictly-increasing-purchases.sql b/customers-with-strictly-increasing-purchases.sql new file mode 100644 index 0000000..faa0263 --- /dev/null +++ b/customers-with-strictly-increasing-purchases.sql @@ -0,0 +1,74 @@ +""" + +Table: Orders ++--------------+------+ +| Column Name | Type | ++--------------+------+ +| order_id | int | +| customer_id | int | +| order_date | date | +| price | int | ++--------------+------+ +order_id is the column with unique values for this table. +Each row contains the id of an order, the id of customer that ordered it, the date of the order, and its price. + + +Write a solution to report the IDs of the customers with the total purchases strictly increasing yearly. + +The total purchases of a customer in one year is the sum of the prices of their orders in that year. If for some year the customer did not make any order, we consider the total purchases 0. +The first year to consider for each customer is the year of their first order. +The last year to consider for each customer is the year of their last order. +Return the result table in any order. + +The result format is in the following example. + +Input: +Orders table: ++----------+-------------+------------+-------+ +| order_id | customer_id | order_date | price | ++----------+-------------+------------+-------+ +| 1 | 1 | 2019-07-01 | 1100 | +| 2 | 1 | 2019-11-01 | 1200 | +| 3 | 1 | 2020-05-26 | 3000 | +| 4 | 1 | 2021-08-31 | 3100 | +| 5 | 1 | 2022-12-07 | 4700 | +| 6 | 2 | 2015-01-01 | 700 | +| 7 | 2 | 2017-11-07 | 1000 | +| 8 | 3 | 2017-01-01 | 900 | +| 9 | 3 | 2018-11-07 | 900 | ++----------+-------------+------------+-------+ +Output: ++-------------+ +| customer_id | ++-------------+ +| 1 | ++-------------+ + +""" + +with cte as +( + select + customer_id, + year(order_date) as yr, + sum(price) as price + from + orders + group by + customer_id, yr +) + +select + c1.customer_id +from + cte c1 left join cte c2 +on + c1.customer_id = c2.customer_id +and + c1.yr + 1 = c2.yr +and + c1.price < c2.price +group by + c1.customer_id +having + count(*) - count(c2.customer_id) = 1 diff --git a/game-play-analysis-ii.sql b/game-play-analysis-ii.sql new file mode 100644 index 0000000..30b1427 --- /dev/null +++ b/game-play-analysis-ii.sql @@ -0,0 +1,108 @@ +""" + +Table: Activity ++--------------+---------+ +| Column Name | Type | ++--------------+---------+ +| player_id | int | +| device_id | int | +| event_date | date | +| games_played | int | ++--------------+---------+ +(player_id, event_date) is the primary key (combination of columns with unique values) of this table. +This table shows the activity of players of some games. +Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on someday using some device. + + +Write a solution to report the device that is first logged in for each player. + +Return the result table in any order. + +The result format is in the following example. + + +Input: +Activity table: ++-----------+-----------+------------+--------------+ +| player_id | device_id | event_date | games_played | ++-----------+-----------+------------+--------------+ +| 1 | 2 | 2016-03-01 | 5 | +| 1 | 2 | 2016-05-02 | 6 | +| 2 | 3 | 2017-06-25 | 1 | +| 3 | 1 | 2016-03-02 | 0 | +| 3 | 4 | 2018-07-03 | 5 | ++-----------+-----------+------------+--------------+ +Output: ++-----------+-----------+ +| player_id | device_id | ++-----------+-----------+ +| 1 | 2 | +| 2 | 3 | +| 3 | 1 | ++-----------+-----------+ + +""" + +-- method 1: using first_value +select + distinct player_id, + first_value(device_id) over(partition by player_id order by event_date) as device_id +from + Activity + + +-- method 2: using rank +with cte as +( + select + distinct player_id, device_id, + rank() over(partition by player_id order by event_date) as rnk + from + Activity +) + +select player_id, device_id from cte where rnk = 1 + + +-- method 3: using subquery +select + a.player_id, a.device_id +from + Activity a +where + a.event_date in + ( + select + min(b.event_date) + from + Activity b + where + a.player_id = b.player_id + ) + + +-- method 4: using cte +with cte as +( + select + a.player_id, min(a.event_date) as 'min_date' + from + Activity a + group by + a.player_id +) + +select + a.player_id, a.device_id +from + Activity a +where + a.event_date in (select c.min_date from cte c where c.player_id = a.player_id ) + + +-- method 5: using last_value +select + distinct player_id, + last_value(device_id) over(partition by player_id order by event_date desc range between unbounded preceding and unbounded following ) as 'device_id' +from + Activity diff --git a/game-play-analysis-iii.sql b/game-play-analysis-iii.sql new file mode 100644 index 0000000..dea0807 --- /dev/null +++ b/game-play-analysis-iii.sql @@ -0,0 +1,51 @@ +""" + +Table: Activity ++--------------+---------+ +| Column Name | Type | ++--------------+---------+ +| player_id | int | +| device_id | int | +| event_date | date | +| games_played | int | ++--------------+---------+ +(player_id, event_date) is the primary key (column with unique values) of this table. +This table shows the activity of players of some games. +Each row is a record of a player who logged in and played a number of games (possibly 0) before logging out on someday using some device. + +Write a solution to report for each player and date, how many games played so far by the player. +That is, the total number of games played by the player until that date. Check the example for clarity. + +Return the result table in any order. + +The result format is in the following example. + + +Input: +Activity table: ++-----------+-----------+------------+--------------+ +| player_id | device_id | event_date | games_played | ++-----------+-----------+------------+--------------+ +| 1 | 2 | 2016-03-01 | 5 | +| 1 | 2 | 2016-05-02 | 6 | +| 1 | 3 | 2017-06-25 | 1 | +| 3 | 1 | 2016-03-02 | 0 | +| 3 | 4 | 2018-07-03 | 5 | ++-----------+-----------+------------+--------------+ +Output: ++-----------+------------+---------------------+ +| player_id | event_date | games_played_so_far | ++-----------+------------+---------------------+ +| 1 | 2016-03-01 | 5 | +| 1 | 2016-05-02 | 11 | +| 1 | 2017-06-25 | 12 | +| 3 | 2016-03-02 | 0 | +| 3 | 2018-07-03 | 5 | ++-----------+------------+---------------------+ + +""" + +select + player_id, event_date, + sum(games_played) over(partition by player_id order by event_date ) as games_played_so_far +from Activity diff --git a/shortest-distance-in-a-plane.sql b/shortest-distance-in-a-plane.sql new file mode 100644 index 0000000..0ae09fd --- /dev/null +++ b/shortest-distance-in-a-plane.sql @@ -0,0 +1,47 @@ +""" + +Table: Point2D ++-------------+------+ +| Column Name | Type | ++-------------+------+ +| x | int | +| y | int | ++-------------+------+ +(x, y) is the primary key column (combination of columns with unique values) for this table. +Each row of this table indicates the position of a point on the X-Y plane. + + +The distance between two points p1(x1, y1) and p2(x2, y2) is sqrt((x2 - x1)2 + (y2 - y1)2). + +Write a solution to report the shortest distance between any two points from the Point2D table. Round the distance to two decimal points. + +The result format is in the following example. + +Input: +Point2D table: ++----+----+ +| x | y | ++----+----+ +| -1 | -1 | +| 0 | 0 | +| -1 | -2 | ++----+----+ +Output: ++----------+ +| shortest | ++----------+ +| 1.00 | ++----------+ + +""" + +select + min(round(sqrt(pow(p2.x - p1.x, 2) + pow(p2.y-p1.y, 2)), 2)) as shortest +from + Point2D p1 join Point2D p2 +on + p1.x <= p2.x and p1.y < p2.y + or + p1.x <= p2.x and p1.y > p2.y + or + p1.x < p2.x and p1.y = p2.y