Quick Overview

Write a SQL query that groups job postings by user, counts each user's distinct job titles, and returns one row with how many users posted a single title and how many posted several. It tests two-level aggregation, COUNT DISTINCT and conditional counting.

Count users who posted one job title versus several distinct job titles

Company: LinkedIn

Role: Data Scientist

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

Count users by the number of distinct `job_title` values they posted. Return one row with `same_job` (users with exactly one distinct title) and `different_jobs` (users with more than one distinct title). ```hint Count at the right grain Decide what a single row per user should hold before you count users, and remember that posting the same title twice is still one title. ``` ### Input Table(s) `job_postings` | column | type | meaning | |---|---|---| | id | INTEGER | Posting identifier (primary key) | | job_title | VARCHAR(255) | Title of the job posted, never NULL | | user_id | INTEGER | User who posted the job, never NULL | | date_posted | TIMESTAMP | When the job was posted | ### Output Contract Return exactly one row with these columns, in this order: | column | meaning | |---|---| | same_job | Number of users with exactly one distinct `job_title` | | different_jobs | Number of users with more than one distinct `job_title` | Both values are integer counts. The result is always a single row, so no ordering applies. ### Example `job_postings` | id | job_title | user_id | date_posted | |---|---|---|---| | 1 | Software Engineer | 101 | 2024-01-10 09:00:00 | | 2 | Software Engineer | 101 | 2024-01-15 10:00:00 | | 3 | Data Analyst | 102 | 2024-01-12 11:00:00 | | 4 | Product Manager | 102 | 2024-01-13 12:00:00 | | 5 | Designer | 103 | 2024-01-14 13:00:00 | Expected result: | same_job | different_jobs | |---|---| | 2 | 1 | User 101 posted only "Software Engineer" (1 distinct title) and user 103 posted only "Designer" (1 distinct title), so `same_job` is 2. User 102 posted 2 distinct titles, so `different_jobs` is 1. ### Constraints and Clarifications - The table has between 1 and 100,000 rows. - `id` is unique. `user_id` and `job_title` are never NULL, so every user has at least one distinct title and falls into exactly one of the two counts. - A user may post the same job title multiple times. Repeats of one title count as a single distinct title. - Titles are compared exactly as stored. - `date_posted` does not affect the answer.

Overview: Write a SQL query that groups job postings by user, counts each user's distinct job titles, and returns one row with how many users posted a single title and how many posted several. It tests two-level aggregation, COUNT DISTINCT and conditional counting.

Table `job_postings` has one row per job posting, with columns `id` (INTEGER, primary key, the posting identifier), `job_title` (VARCHAR(255), the title of the job posted, never NULL), `user_id` (INTEGER, the user who posted the job, never NULL) and `date_posted` (TIMESTAMP, when the job was posted). Count users by the number of distinct `job_title` values they posted. Return exactly one row with these columns, in this order: `same_job`, the number of users with exactly one distinct `job_title`, and `different_jobs`, the number of users with more than one distinct `job_title`. Both values are integer counts, and a count that no user qualifies for is 0. A user may post the same job title multiple times, and repeats of one title count as a single distinct title. Titles are compared exactly as stored. Because `user_id` and `job_title` are never NULL, every user has at least one distinct title and falls into exactly one of the two counts. The table has at least one row, and `date_posted` does not affect the answer. The result is always a single row, so no ordering applies.

Tables

job_postings(id INTEGER, job_title VARCHAR(255), user_id INTEGER, date_posted TIMESTAMP)

Hints

  1. Decide what a single row per user should hold before you count users, and remember that posting the same title twice is still one title.
  2. Every user belongs to exactly one of the two counts, and a count that no user falls into should come back as 0.
LinkedInData ScientisteasyData Manipulation (SQL/Python)

Users Posting One Job Title or Several

Table job_postings has one row per job posting, with columns id (INTEGER, primary key, the posting identifier), job_title (VARCHAR(255), the title of the job posted, never NULL), user_id (INTEGER, the user who posted the job, never NULL) and date_posted (TIMESTAMP, when the job was posted). Count users by the number of distinct job_title values they posted. Return exactly one row with these columns, in this order: same_job, the number of users with exactly one distinct job_title, and different_jobs, the number of users with more than one distinct job_title. Both values are integer counts, and a count that no user qualifies for is 0. A user may post the same job title multiple times, and repeats of one title count as a single distinct title. Titles are compared exactly as stored. Because user_id and job_title are never NULL, every user has at least one distinct title and falls into exactly one of the two counts. The table has at least one row, and date_posted does not affect the answer. The result is always a single row, so no ordering applies.

Expected Output

Tables

job_postings
job_postings
ColumnType
idINTEGER
job_titleVARCHAR(255)
user_idINTEGER
date_postedTIMESTAMP

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Language
Run code to see results here.

Browse More Questions