Count and Lexicographically Join Distinct Products Sold per Date

Quick Overview

For each sale date, count distinct product names and return them in lexical order, separated by commas.

Count and Lexicographically Join Distinct Products Sold per Date

Company: Google

Role: Software Engineer

Category: Data Manipulation (SQL/Python)

Difficulty: easy

Interview Round: Other

### Problem For each sell_date, report num_sold as the count of different product names and products as those distinct names joined by commas in lexicographic order. Sort result dates ascending. Repeated sales of one product on a date count once; preserve product spelling. ### Tables Table `Activities`: ```text +-------------+---------+ | Column Name | Type | +-------------+---------+ | sell_date | date | | product | varchar | +-------------+---------+ There is no primary key (column with unique values) for this table. It may contain duplicates. Each row of this table contains the product name and the date it was sold in a market. ``` ### Example ```text Activities (sell_date,product): (2021-06-01,apple), (2021-06-01,pear), (2021-06-01,apple), (2021-06-02,plum) Output (sell_date,num_sold,products): (2021-06-01,2,apple,pear), (2021-06-02,1,plum) The first products cell is the single string "apple,pear". ``` ### What a Strong Answer Covers - Count each different product once per date. - Return the names in the required order and sort the result by date. ### Follow-up Questions - How would you report how many times each product was sold on each date? - How would you return an array of product names if names could contain commas?

Overview: For each sale date, count distinct product names and return them in lexical order, separated by commas.

Google logo
Google
easySoftware EngineerOtherData Manipulation (SQL/Python)
0
0

Problem

For each sell_date, report num_sold as the count of different product names and products as those distinct names joined by commas in lexicographic order. Sort result dates ascending. Repeated sales of one product on a date count once; preserve product spelling.

Tables

Table Activities:

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| sell_date   | date    |
| product     | varchar |
+-------------+---------+
There is no primary key (column with unique values) for this table. It may contain duplicates.
Each row of this table contains the product name and the date it was sold in a market.

Example

Activities (sell_date,product): (2021-06-01,apple), (2021-06-01,pear), (2021-06-01,apple), (2021-06-02,plum)
Output (sell_date,num_sold,products): (2021-06-01,2,apple,pear), (2021-06-02,1,plum)
The first products cell is the single string "apple,pear".

What a Strong Answer Covers Guidance

  • Count each different product once per date.
  • Return the names in the required order and sort the result by date.

Follow-up Questions Guidance

  • How would you report how many times each product was sold on each date?
  • How would you return an array of product names if names could contain commas?