SQL
Lab
LinkedIn Easy Window Ranking

First Job
Per
Company

Find the first job posted by each company.

Table Schema

Inspect
Table

interactive
ColumnTypeDescription
job_idintegerUnique job posting identifier.
company_idintegerIdentifier for the company posting the job.
titlestringJob listing title.
posted_datedateDate the job was posted.
locationstringJob location.
salaryintegerListed salary.
recruiter_idintegerRecruiter identifier.

Sample Data

Input
Output

Sample Input: job_listings
job_idcompany_idtitleposted_datelocationsalaryrecruiter_id
11Analyst2024-01-01NY6000011
21Engineer2024-02-01NY9000012
32Scientist2024-01-10SF12000013
42Manager2024-01-20SF11000014
53Analyst2024-01-05TX7000015
63Engineer2024-02-10TX9500016
Expected Output
company_idjob_idposted_date
112024-01-01
232024-01-10
352024-01-05

SQL Editor

Run
Query

postgresql
Waiting for query

company_idjob_idposted_date
112024-01-01
232024-01-10
352024-01-05

Hints

Unlock
Clues

Hint 01: Use ROW_NUMBER() to rank jobs inside each company.
Hint 02: PARTITION BY company_id and ORDER BY posted_date.
Hint 03: Keep rows where the rank equals 1.

Solution

Locked
Answer

Solution is locked until you decide to reveal it. Try the editor first, then open this when you want the reference answer.

WITH ranked_jobs AS (
  SELECT
    company_id,
    job_id,
    posted_date,
    ROW_NUMBER() OVER (
      PARTITION BY company_id
      ORDER BY posted_date
    ) AS rn
  FROM job_listings
)
SELECT
  company_id,
  job_id,
  posted_date
FROM ranked_jobs
WHERE rn = 1;

Explanation

Step By
Step

01

Rank each company's jobs by posted_date using ROW_NUMBER().

02

Give the earliest job rank 1 inside every company partition.

03

Select the company_id, job_id, and posted_date where rn equals 1.

Related Questions

Keep
Solving