Leetcode Problem 1398. Customers Who Bought Products A and B but Not C

1398. Customers Who Bought Products A and B but Not C

Leetcode Solutions

Group By and Having Clause in SQL

  1. Perform a LEFT JOIN between the customers and orders tables on the customer_id column.
  2. Use the GROUP BY clause to group the results by customer_id.
  3. Apply the HAVING clause to filter groups where:
    • The sum of occurrences of product 'A' is greater than 0.
    • The sum of occurrences of product 'B' is greater than 0.
    • The sum of occurrences of product 'C' is exactly 0.
  4. Use the ORDER BY clause to sort the final result by customer_id.
erDiagram
    Customers {
        int customer_id
        varchar customer_name
    }
    Orders {
        int order_id
        int customer_id
        varchar product_name
    }
    Customers ||--o{ Orders : ""

Filter with custom function and `isin` in Pandas

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Code Diffs Compare
Full Screen
Copy Answer Code
Loading...