Percentage of jobs whose latest posting in the last 180 days was revoked
Company: LinkedIn
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: medium
Interview Round: Other
Each row of `job_postings` is one posting of a job, and a job can be posted several times. Using the fixed reference date 2024-06-30, consider only postings from the last 180 days, and for each job keep only its most recent posting in that window (by `post_dt`). Compute the percentage of those latest postings whose status is `REVOKED`, rounded to 2 decimals, as `revoked_percentage`.
```hint Keep the fraction
Both counts in the ratio are integers in PostgreSQL. Make sure the division keeps its fractional part before you round.
```
### Input Table(s)
Table `job_postings`, primary key `posting_id`:
| Column | Type | Meaning |
|---|---|---|
| posting_id | INT | Posting identifier |
| job_id | INT | Job that was posted, NOT NULL |
| company_id | INT | Company that owns the job, NOT NULL (not needed for this task) |
| post_dt | DATE | Date of the posting, NOT NULL |
| status | VARCHAR(20) | `ACTIVE` or `REVOKED`, NOT NULL |
### Output Contract
| Column | Meaning |
|---|---|
| revoked_percentage | Percentage (0 to 100 scale) of jobs whose latest posting in the window is `REVOKED`, rounded to 2 decimals |
The result is exactly one row with one column, so no ordering is needed.
### Example
`job_postings`:
| posting_id | job_id | company_id | post_dt | status |
|---|---|---|---|---|
| 1 | 100 | 10 | 2024-06-01 | ACTIVE |
| 2 | 100 | 10 | 2024-06-15 | REVOKED |
| 3 | 101 | 11 | 2024-05-20 | ACTIVE |
| 4 | 102 | 12 | 2024-06-25 | REVOKED |
| 5 | 103 | 13 | 2024-01-01 | REVOKED |
Expected output:
| revoked_percentage |
|---|
| 66.67 |
The window runs from 2024-01-02 to 2024-06-30. Job 100's latest posting is posting 2 (2024-06-15, REVOKED), job 101's is posting 3 (ACTIVE) and job 102's is posting 4 (REVOKED). Job 103's only posting, on 2024-01-01, falls one day before the window and is ignored. Two of the three jobs are revoked, so the result is 66.67.
### Constraints and Clarifications
- The table holds 1 to 10,000 postings. `status` is exactly `ACTIVE` or `REVOKED`, and every `post_dt` is on or before 2024-06-30.
- "The last 180 days" means 2024-06-30 minus 180 days through 2024-06-30, both ends inclusive: 2024-01-02 to 2024-06-30.
- The percentage is over jobs. Each job with at least one posting in the window counts once, through its latest posting there. Postings outside the window are ignored, and a job with no posting in the window is not counted at all.
- If two postings of the same job share the latest `post_dt`, the one with the larger `posting_id` is the latest.
- If no posting falls in the window, return one row whose `revoked_percentage` is NULL.
Overview: Write a SQL query that keeps each job's most recent posting within a 180-day window ending on a fixed date and reports what percentage of those latest postings were revoked. It tests latest-row-per-group selection with ROW_NUMBER, deterministic tie-breaking, inclusive date windows and percentage math on integer counts.
Each row of `job_postings` is one posting of a job, and a job can be posted several times. Using the fixed reference date 2024-06-30, consider only postings from the last 180 days, and for each job keep only its most recent posting in that window (by `post_dt`). Compute the percentage of those latest postings whose status is `REVOKED`, rounded to 2 decimals, as `revoked_percentage`.
Table `job_postings` (primary key `posting_id`):
- `posting_id` INTEGER: posting identifier
- `job_id` INTEGER NOT NULL: job that was posted
- `company_id` INTEGER NOT NULL: company that owns the job (not needed for this task)
- `post_dt` DATE NOT NULL: date of the posting
- `status` VARCHAR(20) NOT NULL: `ACTIVE` or `REVOKED`
Clarifications:
- The table holds 1 to 10,000 postings. `status` is exactly `ACTIVE` or `REVOKED`, and every `post_dt` is on or before 2024-06-30.
- "The last 180 days" means 2024-06-30 minus 180 days through 2024-06-30, both ends inclusive: 2024-01-02 to 2024-06-30.
- The percentage is over jobs, on a 0 to 100 scale. Each job with at least one posting in the window counts once, through its latest posting there. Postings outside the window are ignored, and a job with no posting in the window is not counted at all.
- If two postings of the same job share the latest `post_dt`, the one with the larger `posting_id` is the latest.
- If no posting falls in the window, return one row whose `revoked_percentage` is NULL.
Return exactly one row with one column, `revoked_percentage`. No ordering is needed.
Tables
job_postings(posting_id INTEGER, job_id INTEGER, company_id INTEGER, post_dt DATE, status VARCHAR(20))
Hints
- Both counts in the ratio are integers in PostgreSQL. Make sure the division keeps its fractional part before you round.
- The window includes both of its ends, so a posting dated 2024-01-02 or 2024-06-30 is inside it and one dated 2024-01-01 is outside.