Count each company's net change in product launches from 2019 to 2020
Quick Overview
Write a PostgreSQL query that compares how many products each company launched in 2020 with how many it launched in 2019 and reports the net change. It tests conditional aggregation in a single GROUP BY, keeping companies active in only one of the two years, and a deterministic sort order.
Count each company's net change in product launches from 2019 to 2020
Company: Tesla
Role: Data Scientist
Category: Data Manipulation (SQL/Python)
Difficulty: easy
Interview Round: Other
The `product_launches` table records every product a company has launched. For each company that launched at least one product in 2019 or 2020, report the net change in launches between those two years: the number of products the company launched in 2020 minus the number it launched in 2019.
### Input Table(s)
`product_launches`
| Column | Type | Meaning |
|---|---|---|
| product_id | INT | Unique id of the product |
| company_name | VARCHAR(100) | Company that launched the product (NOT NULL) |
| product_name | VARCHAR(100) | Name of the product (NOT NULL) |
| launch_date | DATE | Date the product launched (NOT NULL) |
Primary key: `product_id`. A company can have any number of rows.
### Output Contract
- Columns, in this order: `company_name`, `net_difference`.
- `net_difference` is an integer: (products launched in 2020) minus (products launched in 2019).
- One row per company that launched at least one product in 2019 or 2020.
- Order by `net_difference` descending, then `company_name` ascending.
- No rounding is needed.
### Example
`product_launches`
| product_id | company_name | product_name | launch_date |
|---|---|---|---|
| 1 | Acme | Rocket Skates | 2019-03-14 |
| 2 | Acme | Anvil Pro | 2020-06-01 |
| 3 | Acme | Giant Magnet | 2020-11-20 |
| 4 | Globex | Hammock X | 2019-02-10 |
| 5 | Globex | Hammock Y | 2019-08-05 |
| 6 | Globex | Hammock Z | 2020-04-22 |
| 7 | Initech | Stapler | 2018-12-30 |
| 8 | Initech | Report Tool | 2021-01-02 |
| 9 | Northwind | Tea Box | 2020-09-09 |
| 10 | Contoso | Desk Lamp | 2019-07-01 |
| 11 | Contoso | Desk Lamp 2 | 2020-07-01 |
| 12 | Hooli | Nucleus | 2019-05-05 |
Expected result:
| company_name | net_difference |
|---|---|
| Acme | 1 |
| Northwind | 1 |
| Contoso | 0 |
| Globex | -1 |
| Hooli | -1 |
Acme launched 2 products in 2020 and 1 in 2019. Northwind launched only in 2020 and Hooli only in 2019, so each counts 0 for the missing year. Contoso launched one product in each year and still appears with 0. Initech is left out because its launches fall in 2018 and 2021. Ties on `net_difference` are broken by `company_name`.
### Constraints and Clarifications
- The table has at most 10,000 rows.
- `launch_date` ranges from 2018-01-01 to 2021-12-31. Launches in 2018 or 2021 never count, and on their own they do not put a company in the result.
- No column contains NULL.
- Each row is a distinct product. Two rows with the same `product_name` count as two launches.
- A company that launched in only one of the two years still appears, so `net_difference` can be negative.
- Company names are compared exactly as stored.
```hint Handling the missing year
Think about what a company that launched in only one of the two years should show for the other year, and make sure your query does not drop it.
```
Overview: Write a PostgreSQL query that compares how many products each company launched in 2020 with how many it launched in 2019 and reports the net change. It tests conditional aggregation in a single GROUP BY, keeping companies active in only one of the two years, and a deterministic sort order.
The product_launches table records every product a company has launched. For each company that launched at least one product in 2019 or 2020, report the net change in launches between those two years: the number of products the company launched in 2020 minus the number it launched in 2019.
Input Table(s)
product_launches
Column
Type
Meaning
product_id
INT
Unique id of the product
company_name
VARCHAR(100)
Company that launched the product (NOT NULL)
product_name
VARCHAR(100)
Name of the product (NOT NULL)
launch_date
DATE
Date the product launched (NOT NULL)
Primary key: product_id. A company can have any number of rows.
Output Contract
Columns, in this order:
company_name
,
net_difference
.
net_difference
is an integer: (products launched in 2020) minus (products launched in 2019).
One row per company that launched at least one product in 2019 or 2020.
Order by
net_difference
descending, then
company_name
ascending.
No rounding is needed.
Example
product_launches
product_id
company_name
product_name
launch_date
1
Acme
Rocket Skates
2019-03-14
2
Acme
Anvil Pro
2020-06-01
3
Acme
Giant Magnet
2020-11-20
4
Globex
Hammock X
2019-02-10
5
Globex
Hammock Y
2019-08-05
6
Globex
Hammock Z
2020-04-22
7
Initech
Stapler
2018-12-30
8
Initech
Report Tool
2021-01-02
9
Northwind
Tea Box
2020-09-09
10
Contoso
Desk Lamp
2019-07-01
11
Contoso
Desk Lamp 2
2020-07-01
12
Hooli
Nucleus
2019-05-05
Expected result:
company_name
net_difference
Acme
1
Northwind
1
Contoso
0
Globex
-1
Hooli
-1
Acme launched 2 products in 2020 and 1 in 2019. Northwind launched only in 2020 and Hooli only in 2019, so each counts 0 for the missing year. Contoso launched one product in each year and still appears with 0. Initech is left out because its launches fall in 2018 and 2021. Ties on net_difference are broken by company_name.
Constraints and Clarifications
The table has at most 10,000 rows.
launch_date
ranges from 2018-01-01 to 2021-12-31. Launches in 2018 or 2021 never count, and on their own they do not put a company in the result.
No column contains NULL.
Each row is a distinct product. Two rows with the same
product_name
count as two launches.
A company that launched in only one of the two years still appears, so
net_difference
can be negative.