EasyNo. 1511LeetCode Premium

Customer Order Frequency — LeetCode 1511 Python Solution

Topics
Database
Reading time
3 min

Table schema

SQLTable schema
Table: Customers +---------------+---------+ | Column Name | Type | +---------------+---------+ | customer_id | int | | name | varchar | | country | varchar | +---------------+---------+ customer_id is the column with unique values for this table. This table contains information about the customers in the company.

Example

SQLExample
+---------------+---------+
| Column Name   | Type    |
+---------------+---------+
| customer_id   | int     |
| name          | varchar |
| country       | varchar |
+---------------+---------+
customer_id is the column with unique values for this table.
This table contains information about the customers in the company.

Python solution

PythonSolution
import duckdb
import pandas as pd

def solution(customers: pd.DataFrame, product: pd.DataFrame, orders: pd.DataFrame) -> pd.DataFrame:
    con = duckdb.connect()
    con.register("Customers", customers)
    con.register("Product", product)
    con.register("Orders", orders)
    return con.execute("""SELECT customer_id, name
FROM
    Orders
    JOIN Product USING (product_id)
    JOIN Customers USING (customer_id)
WHERE YEAR(order_date) = 2020
GROUP BY 1
HAVING
    SUM(IF(MONTH(order_date) = 6, quantity * price, 0)) >= 100
    AND SUM(IF(MONTH(order_date) = 7, quantity * price, 0)) >= 100;""").df()

Complexity

Time
O(n log n)(typical)
Space · auxiliary
O(n)

Related problems

Related problems
No.ProblemDifficulty
0175Combine Two TablesEasy
0176Second Highest SalaryMedium
0177Nth Highest SalaryMedium
0178Rank ScoresMedium
0180Consecutive NumbersMedium
0181Employees Earning More Than Their ManagersEasy

Frequently asked questions

How hard is LeetCode 1511. Customer Order Frequency?

LeetCode 1511. Customer Order Frequency is rated Easy on LeetCode.

What topics does LeetCode 1511. Customer Order Frequency cover?

LeetCode 1511. Customer Order Frequency is tagged Database on LeetCode.

Is LeetCode 1511. Customer Order Frequency a premium problem?

Yes. LeetCode 1511. Customer Order Frequency is a LeetCode Premium problem, so the full statement and test cases require a paid LeetCode subscription.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity, invisible to screen sharing.

Free to download. Check it runs on your machine, then pick a plan.