Skip to content

Commit

Permalink
✏️ docs: fix typos and rearrange questions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilbottom committed Jun 19, 2024
1 parent c3c9548 commit 1063505
Show file tree
Hide file tree
Showing 25 changed files with 154 additions and 151 deletions.
6 changes: 3 additions & 3 deletions docs/challenging-sql-problems/challenging-sql-problems.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ As long as you know your database features and query patterns, these are fairly
1. [Temperature anomaly detection](problems/bronze/temperature-anomaly-detection.md)
2. [Personalised customer emails](problems/bronze/personalised-customer-emails.md)
3. [Suspicious login activity](problems/bronze/suspicious-login-activity.md)
4. [UK bank holidays](problems/bronze/uk-bank-holidays.md)
5. [Customer churn](problems/bronze/customer-churn.md)
6. [Fibonacci sequence](problems/bronze/fibonacci-sequence.md)
4. [Customer churn](problems/bronze/customer-churn.md)
5. [Fibonacci sequence](problems/bronze/fibonacci-sequence.md)
6. [UK bank holidays](problems/bronze/uk-bank-holidays.md)

### ⚪ Silver Tier

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
```sql
with user_history(user_id, last_update, activity_history) as (
values
(1, '2024-03-01', 81),
(2, '2024-03-01', 2688),
(1, '2024-03-01', 81),
(2, '2024-03-01', 2688),
(3, '2024-03-01', 13144)
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ We can apply the same logic to the other bits:
└-------------- 2024-02-24 (active)
```

└─
Since there are no more bits on the left, we can assume that the user was inactive on those days.

This user is therefore not a churned user since they were active at least once in the last 7 days.
Expand All @@ -114,7 +113,7 @@ This user is therefore not a churned user since they were active at least once i

The `activity_history` for user 2 is `2688`, which in binary is `101010000000`.

We can expand the binary representation to understand the activity again:
We can expand the binary representation to understand the activity:

```
1 0 1 0 1 0 0 0 0 0 0 0
Expand All @@ -133,13 +132,13 @@ We can expand the binary representation to understand the activity again:
└------------------------ 2024-02-19 (active)
```

User 2 _has_ churned user since they were not active in the last 7 days (between `2024-02-24` and `2024-03-01`) but were active at least once in the 7 days before that.
User 2 _has_ churned since they were not active in the last 7 days (between `2024-02-24` and `2024-03-01`) but were active at least once in the 7 days before that.

#### User 3

The `activity_history` for user 3 is `13144`, which in binary is `11001101011000`.

We can expand the binary representation to understand the activity again:
Again, we can expand the binary representation to understand the activity:

```
1 1 0 0 1 1 0 1 0 1 1 0 0 0
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
```sql
create table user_history (
user_id int primary key,
last_update date not null,
user_id int primary key,
last_update date not null,
activity_history bigint not null,
);
insert into user_history
values
(1, '2024-06-01', 1056256),
(1, '2024-06-01', 1056256),
(2, '2024-06-01', 907289368),
(3, '2024-06-01', 201335032),
(4, '2024-06-01', 9769312),
(4, '2024-06-01', 9769312),
(5, '2024-06-01', 246247510),
(6, '2024-06-01', 492660983)
;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
```sql
solution(n, f_n) as (
values
(1, 1),
(2, 1),
(3, 2),
(4, 3),
(5, 5),
(6, 8),
(7, 13),
(8, 21),
(9, 34),
(1, 1),
(2, 1),
(3, 2),
(4, 3),
(5, 5),
(6, 8),
(7, 13),
(8, 21),
(9, 34),
(10, 55)
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ The solution can be found at:
<!-- prettier-ignore -->
>? TIP: **Hint 2**
>
> Use the columns `n`, `f_n`, and `f_m` to keep track of the current term, the current Fibonacci number, and the previous Fibonacci number.
> Use the columns `n`, `f_n`, and `f_m` to keep track of the current term, the current Fibonacci number, and the previous Fibonacci number, respectively.
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
```sql
create table customers (
customer_id int primary key,
full_name varchar not null,
first_name varchar,
last_name varchar,
customer_id int primary key,
full_name varchar not null,
first_name varchar,
last_name varchar,
email_address varchar
);
insert into customers
Expand All @@ -25,8 +25,8 @@ values
create table customer_relationships (
parent_customer_id int references customers(customer_id),
child_customer_id int references customers(customer_id),
relationship_type varchar not null,
child_customer_id int references customers(customer_id),
relationship_type varchar not null,
primary key (parent_customer_id, child_customer_id)
);
insert into customer_relationships
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
```sql
with events(event_id, user_id, event_datetime, event_type) as (
values
(1, 1, '2024-01-01 03:00:00', 'login failed'),
(2, 1, '2024-01-01 03:01:00', 'login failed'),
(3, 1, '2024-01-01 03:02:00', 'login failed'),
(4, 1, '2024-01-01 03:03:00', 'login failed'),
(5, 1, '2024-01-01 03:04:00', 'login failed'),
(6, 1, '2024-01-01 03:05:00', 'login'),
(7, 2, '2024-01-01 10:00:00', 'login'),
(8, 2, '2024-01-01 15:00:00', 'logout'),
(9, 2, '2024-01-01 23:00:00', 'login failed')
(1, 1, '2024-01-01 03:00:00', 'login failed'),
(2, 1, '2024-01-01 03:01:00', 'login failed'),
(3, 1, '2024-01-01 03:02:00', 'login failed'),
(4, 1, '2024-01-01 03:03:00', 'login failed'),
(5, 1, '2024-01-01 03:04:00', 'login failed'),
(6, 1, '2024-01-01 03:05:00', 'login'),
(7, 2, '2024-01-01 10:00:00', 'login'),
(8, 2, '2024-01-01 15:00:00', 'logout'),
(9, 2, '2024-01-01 23:00:00', 'login failed')
)
```
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
> - `repayment_date` as the date of the repayment
> - `interest` as the interest component of the repayment
> - `principal` as the principal component of the repayment
> - `total` as the total repayment
> - `total` as the total value of the repayment
> - `balance` as the outstanding balance _after_ the repayment
>
> Order the output by `loan_id` and `repayment_number`.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ routes_schedule(
) as (
values
(1, 'train', 'London Gatwick', 'London St Pancras', '09:00:00+00:00', '21:00:00+00:00', '01:00:00', '00:30:00', 12.25),
(2, 'bus', 'London Gatwick', 'London St Pancras', '06:00:00+00:00', '06:00:00+00:00', null, '03:30:00', 8.50)
(2, 'bus', 'London Gatwick', 'London St Pancras', '06:00:00+00:00', '06:00:00+00:00', null, '03:30:00', 8.50)
),
routes_timetable(
Expand All @@ -29,6 +29,6 @@ routes_timetable(
values
(1, 'boat', 'New York', 'London Gatwick', '2024-01-01 04:30:00-05:00', '2024-01-01 22:00:00+00:00', 179.00),
(2, 'plane', 'New York', 'London Gatwick', '2024-01-01 18:00:00-05:00', '2024-01-02 10:45:00+00:00', 125.00),
(3, 'boat', 'London St Pancras', 'Paris', '2024-01-02 13:00:00+00:00', '2024-01-02 14:30:00+01:00', 75.00)
(3, 'boat', 'London St Pancras', 'Paris', '2024-01-02 13:00:00+00:00', '2024-01-02 14:30:00+01:00', 75.00)
)
```
8 changes: 4 additions & 4 deletions docs/challenging-sql-problems/problems/gold/travel-plans.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

> [!SUCCESS] Scenario
>
> You're helping a client plan a trip from New York to Paris, and they want you to find the fastest and cheapest route.
> You're helping a client plan a trip from New York to Paris, and they want you to find the fastest and cheapest routes.
>
> They have collected journey information into two tables:
> They have collected journey information for routes that they are happy to take into two tables:
>
> - `routes_timetable`
> - `routes_schedule`
Expand All @@ -17,7 +17,7 @@
>
> Given the tables that your client has provided, find the fastest and cheapest route from New York to Paris, leaving after `2024-01-01 12:00:00-05:00`.
>
> Give a minimum of 30 minutes and a maximum of 2 hours for "interchange" time (the time between arrival and departure at the same location). All costs are in the same currency (with no currency specified by the client).
> Give a minimum of 30 minutes and a maximum of 6 hours for "interchange" time, which is the time between arrival and departure at the same location. All costs are in the same currency (with no currency specified by the client).
>
> The output should have at most two rows (the fastest/cheapest routes, which may be the same route), with the columns:
>
Expand Down Expand Up @@ -50,7 +50,7 @@ The solution can be found at:
> | 1 | train | London Gatwick | London St Pancras | 09:00:00 +00:00 | 21:00:00 +00:00 | 01:00:00 | 00:30:00 | 12.25 |
> | 2 | bus | London Gatwick | London St Pancras | 06:00:00 +00:00 | 06:00:00 +00:00 | _null_ | 03:30:00 | 8.50 |
>
> **Routes Schedule**
> **Routes Timetable**
>
> | route_id | mode_of_transport | from_location | to_location | departure_datetime | arrival_datetime | cost |
> |---------:|:------------------|:------------------|:---------------|:-------------------|:------------------|-------:|
Expand Down
44 changes: 22 additions & 22 deletions docs/challenging-sql-problems/problems/gold/travel-plans.sql
Original file line number Diff line number Diff line change
@@ -1,47 +1,47 @@
```sql
create table routes_schedule (
schedule_id int primary key,
mode_of_transport varchar not null,
from_location varchar not null,
to_location varchar not null,
schedule_id int primary key,
mode_of_transport varchar not null,
from_location varchar not null,
to_location varchar not null,
earliest_departure timetz not null,
latest_departure timetz not null,
frequency time, /* `null` means that it's daily */
duration time not null,
cost decimal(8, 2) not null,
latest_departure timetz not null,
frequency time, /* `null` means that it's daily */
duration time not null,
cost decimal(8, 2) not null,
);
insert into routes_schedule
values
(1, 'train', 'London St Pancras', 'London Gatwick', '08:00:00+00:00', '20:00:00+00:00', '01:00:00', '00:30:00', 17.50),
(2, 'train', 'London St Pancras', 'London Gatwick', '07:30:00+00:00', '22:30:00+00:00', '02:30:00', '01:15:00', 12.00),
(3, 'bus', 'London St Pancras', 'London Gatwick', '06:15:00+00:00', '06:15:00+00:00', null, '03:30:00', 6.75),
(4, 'bus', 'London St Pancras', 'London Gatwick', '19:30:00+00:00', '19:30:00+00:00', null, '03:30:00', 6.75),
(3, 'bus', 'London St Pancras', 'London Gatwick', '06:15:00+00:00', '06:15:00+00:00', null, '03:30:00', 6.75),
(4, 'bus', 'London St Pancras', 'London Gatwick', '19:30:00+00:00', '19:30:00+00:00', null, '03:30:00', 6.75),
(5, 'train', 'London Gatwick', 'London St Pancras', '09:00:00+00:00', '21:00:00+00:00', '01:00:00', '00:30:00', 17.50),
(6, 'train', 'London Gatwick', 'London St Pancras', '07:15:00+00:00', '22:15:00+00:00', '02:30:00', '01:15:00', 12.00),
(7, 'bus', 'London Gatwick', 'London St Pancras', '06:00:00+00:00', '06:00:00+00:00', null, '03:30:00', 6.75),
(8, 'bus', 'London Gatwick', 'London St Pancras', '20:00:00+00:00', '20:00:00+00:00', null, '03:30:00', 6.75)
(7, 'bus', 'London Gatwick', 'London St Pancras', '06:00:00+00:00', '06:00:00+00:00', null, '03:30:00', 6.75),
(8, 'bus', 'London Gatwick', 'London St Pancras', '20:00:00+00:00', '20:00:00+00:00', null, '03:30:00', 6.75)
;
create table routes_timetable (
route_id int primary key,
mode_of_transport varchar not null,
from_location varchar not null,
to_location varchar not null,
route_id int primary key,
mode_of_transport varchar not null,
from_location varchar not null,
to_location varchar not null,
departure_datetime timestamptz not null,
arrival_datetime timestamptz not null,
cost decimal(8, 2) not null,
arrival_datetime timestamptz not null,
cost decimal(8, 2) not null,
);
insert into routes_timetable
values
(1, 'boat', 'London St Pancras', 'Paris', '2024-01-01 06:00:00+00:00', '2024-01-01 07:30:00+01:00', 45.00),
(1, 'boat', 'London St Pancras', 'Paris', '2024-01-01 06:00:00+00:00', '2024-01-01 07:30:00+01:00', 45.00),
(2, 'plane', 'London Gatwick', 'New York', '2024-01-01 13:05:00+00:00', '2024-01-01 20:55:00-05:00', 158.00),
(3, 'plane', 'London Gatwick', 'New York', '2024-01-02 20:40:00+00:00', '2024-01-03 04:30:00-05:00', 147.00),
(4, 'plane', 'London St Pancras', 'Paris', '2024-01-03 07:00:00+00:00', '2024-01-03 08:30:00+01:00', 70.00),
(4, 'plane', 'London St Pancras', 'Paris', '2024-01-03 07:00:00+00:00', '2024-01-03 08:30:00+01:00', 70.00),
(5, 'plane', 'Paris', 'New York', '2024-01-02 12:00:00+01:00', '2024-01-02 20:30:00-05:00', 180.00),
(6, 'plane', 'New York', 'London Gatwick', '2024-01-01 13:00:00-05:00', '2024-01-02 05:45:00+00:00', 160.00),
(7, 'boat', 'New York', 'London Gatwick', '2024-01-01 05:30:00-05:00', '2024-01-01 23:00:00+00:00', 195.00),
(8, 'boat', 'London St Pancras', 'Paris', '2024-01-01 18:00:00+00:00', '2024-01-01 19:30:00+01:00', 95.00),
(9, 'boat', 'London St Pancras', 'Paris', '2024-01-02 14:00:00+00:00', '2024-01-02 15:30:00+01:00', 40.00),
(8, 'boat', 'London St Pancras', 'Paris', '2024-01-01 18:00:00+00:00', '2024-01-01 19:30:00+01:00', 95.00),
(9, 'boat', 'London St Pancras', 'Paris', '2024-01-02 14:00:00+00:00', '2024-01-02 15:30:00+01:00', 40.00),
(10, 'plane', 'New York', 'Paris', '2024-01-01 18:00:00-05:00', '2024-01-02 17:45:00+01:00', 279.00)
;
```
4 changes: 2 additions & 2 deletions docs/challenging-sql-problems/problems/silver/bus-routes.sql
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
```sql
create table bus_stops (
bus_id int,
bus_id int,
from_stop varchar,
to_stop varchar,
to_stop varchar,
primary key (bus_id, from_stop)
);
insert into bus_stops
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
>
> The number of rows to show in the output should be the number of days in the `activity_history` column for the user with the most days.
>
> Just like in the [customer churn](../bronze/customer-churn.md) problem, the `last_update` column will always have the same date for all users.
>
> The output should have a single row per day with the columns:
>
> - `active_date` as the date of the activity, starting from the `last_update` date
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
```sql
solution(cohort, stage, mortgages, step_rate, total_rate) as (
values
('2024-01', 'full application', 1, 100, 100),
('2024-01', 'decision', 1, 100, 100),
('2024-01', 'documentation', 1, 100, 100),
('2024-01', 'valuation inspection', 1, 100, 100),
('2024-01', 'valuation made', 1, 100, 100),
('2024-01', 'valuation submitted', 1, 100, 100),
('2024-01', 'solicitation', 1, 100, 100),
('2024-01', 'funds released', 0, 0, 0)
('2024-01', 'full application', 1, 100.00, 100.00),
('2024-01', 'decision', 1, 100.00, 100.00),
('2024-01', 'documentation', 1, 100.00, 100.00),
('2024-01', 'valuation inspection', 1, 100.00, 100.00),
('2024-01', 'valuation made', 1, 100.00, 100.00),
('2024-01', 'valuation submitted', 1, 100.00, 100.00),
('2024-01', 'solicitation', 1, 100.00, 100.00),
('2024-01', 'funds released', 0, 0.00, 0.00)
)
```
18 changes: 10 additions & 8 deletions docs/challenging-sql-problems/problems/silver/funnel-analytics.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
> - `step_rate` as the percentage of mortgages that reached the stage compared to the previous stage
> - `total_rate` as the percentage of mortgages that reached the stage compared to the first stage
>
> Round the `step_rate` and `total_rate` to two decimal places.
>
> Note that each cohort should have _all_ the stages, even if there are no mortgages that reached that stage -- the `mortgages` column should be `0` in that case.
>
> Order the output by `cohort` and _the `stage` order_ (e.g. `full application` should come before `decision`, and so on).
Expand Down Expand Up @@ -64,14 +66,14 @@ The solution can be found at:
>
> | cohort | stage | mortgages | step_rate | total_rate |
> |:--------|:---------------------|----------:|----------:|-----------:|
> | 2024-01 | full application | 1 | 100 | 100 |
> | 2024-01 | decision | 1 | 100 | 100 |
> | 2024-01 | documentation | 1 | 100 | 100 |
> | 2024-01 | valuation inspection | 1 | 100 | 100 |
> | 2024-01 | valuation made | 1 | 100 | 100 |
> | 2024-01 | valuation submitted | 1 | 100 | 100 |
> | 2024-01 | solicitation | 1 | 100 | 100 |
> | 2024-01 | funds released | 0 | 0 | 0 |
> | 2024-01 | full application | 1 | 100.00 | 100.00 |
> | 2024-01 | decision | 1 | 100.00 | 100.00 |
> | 2024-01 | documentation | 1 | 100.00 | 100.00 |
> | 2024-01 | valuation inspection | 1 | 100.00 | 100.00 |
> | 2024-01 | valuation made | 1 | 100.00 | 100.00 |
> | 2024-01 | valuation submitted | 1 | 100.00 | 100.00 |
> | 2024-01 | solicitation | 1 | 100.00 | 100.00 |
> | 2024-01 | funds released | 0 | 0.00 | 0.00 |
>
--8<-- "docs/challenging-sql-problems/problems/silver/funnel-analytics--sample-output.sql"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
> [!QUESTION]
>
> Plot an image of the Mandelbrot set.
> Plot an image of the [Mandelbrot set](https://en.wikipedia.org/wiki/Mandelbrot_set).
>
> Start with a 51x51 grid of points ranging from -2 to 2 on both the x and y axes. A sample of some points in the grid are:
>
Expand Down Expand Up @@ -51,7 +51,7 @@ The solution can be found at:
- [mandelbrot-set.md](../../solutions/silver/mandelbrot-set.md)
A worked example is provided below to help illustrate the loan calculations.
A worked example is provided below to help illustrate the Mandelbrot set calculations.
---
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
```sql
with datasets(dataset_1__x, dataset_1__y, dataset_2__x, dataset_2__y) as (
values
(1, 2.00, 1, 9.12),
(1, 2.00, 1, 9.12),
(2, 4.00, 3, 31.18),
(3, 6.00, 5, 55.27),
(4, 8.00, 7, 61.12)
Expand Down
Loading

0 comments on commit 1063505

Please sign in to comment.