diff --git a/README.md b/README.md index bb732bc..216646d 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ # Leetcode (PostgreSQL) +![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white) [![MIT licensed](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE) [![CI](https://github.com/ivanbyone/leetcode-sql/actions/workflows/ci.yml/badge.svg)](https://github.com/Ivanbyone/leetcode-sql//actions) -![Postgres](https://img.shields.io/badge/postgres-%23316192.svg?style=for-the-badge&logo=postgresql&logoColor=white) ## Description @@ -46,6 +46,7 @@ Have a good contributing! 2. [Medium](./leetcode/medium/) - [176. Second Highest Salary](./leetcode/medium/176.%20Second%20Highest%20Salary.sql) - [184. Department Highest Salary](./leetcode/medium/184.%20Department%20Highest%20Salary.sql) + - [1158. Market Analysis 1](./leetcode/medium/1158.%20Market%20Analysis%201.sql) ## License diff --git a/leetcode/medium/1158. Market Analysis 1.sql b/leetcode/medium/1158. Market Analysis 1.sql new file mode 100644 index 0000000..c785f23 --- /dev/null +++ b/leetcode/medium/1158. Market Analysis 1.sql @@ -0,0 +1,60 @@ +/* +Question 1158. Market Analysis 1 +Link: https://leetcode.com/problems/market-analysis-i/description/ + +Table: Users + ++----------------+---------+ +| Column Name | Type | ++----------------+---------+ +| user_id | int | +| join_date | date | +| favorite_brand | varchar | ++----------------+---------+ +user_id is the primary key (column with unique values) of this table. +This table has the info of the users of an online shopping website where users can sell and buy items. + + +Table: Orders + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| order_id | int | +| order_date | date | +| item_id | int | +| buyer_id | int | +| seller_id | int | ++---------------+---------+ +order_id is the primary key (column with unique values) of this table. +item_id is a foreign key (reference column) to the Items table. +buyer_id and seller_id are foreign keys to the Users table. + + +Table: Items + ++---------------+---------+ +| Column Name | Type | ++---------------+---------+ +| item_id | int | +| item_brand | varchar | ++---------------+---------+ +item_id is the primary key (column with unique values) of this table. + + +Write a solution to find for each user, the join date and the number of orders they made as a buyer in 2019. + +Return the result table in any order. +*/ + +SELECT + u.user_id AS buyer_id, + u.join_date, + COUNT(o.order_id) AS orders_in_2019 +FROM Users AS u +LEFT JOIN + Orders AS o + ON + u.user_id = o.buyer_id + AND o.order_date BETWEEN '2019-01-01' AND '2019-12-31' +GROUP BY u.user_id, u.join_date