Skip to content
We areTOP 1%

Group Sold Products By The Date

EasyLeetCodeSQL: CASE, String & Date Functions
Solve on LeetCode(opens in a new tab)
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews
Hint and exampleShow

Group by the date, count the distinct values and join them into one sorted string with GROUP_CONCAT in MySQL or STRING_AGG in PostgreSQL.

Example: orders(order_day, dish)

order_day  | dish
2024-05-01 | Tea
2024-05-01 | Bun
2024-05-01 | Tea
2024-05-02 | Dosa
SELECT order_day,
       COUNT(DISTINCT dish) AS dishes,
       GROUP_CONCAT(DISTINCT dish ORDER BY dish) AS menu
FROM orders
GROUP BY order_day;
-- 2024-05-01 | 2 | Bun,Tea
-- 2024-05-02 | 1 | Dosa

More in SQL: CASE, String & Date Functions

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account