From a17f3aeaad981582bea4ba590e75f386a63a4799 Mon Sep 17 00:00:00 2001 From: pallasivasai Date: Sun, 14 Jul 2024 07:38:21 +0530 Subject: [PATCH 1/2] Solution for a problem 183 is added --- .../183-Customers-Who-Never-Order.md | 96 +++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0100-0199/183-Customers-Who-Never-Order.md diff --git a/dsa-solutions/lc-solutions/0100-0199/183-Customers-Who-Never-Order.md b/dsa-solutions/lc-solutions/0100-0199/183-Customers-Who-Never-Order.md new file mode 100644 index 000000000..7f8ed7f36 --- /dev/null +++ b/dsa-solutions/lc-solutions/0100-0199/183-Customers-Who-Never-Order.md @@ -0,0 +1,96 @@ +--- +id: customers-who-never-order +title: Customers Who Never Order +sidebar_label: 0183. Customers Who Never Order +tags: +- SQL +- Database +description: "Solution to Leetcode 183. Customers Who Never Order" +--- + +## Problem Description + +Given two tables, `Customers` and `Orders`, write a query to find all customers who never ordered anything. + +**Table: Customers** + +| Column Name | Type | +| ----------- | ------- | +| id | int | +| name | varchar | +id is the primary key for this table. + +**Table: Orders** + +| Column Name | Type | +| ----------- | ---- | +| id | int | +| customerId | int | +id is the primary key for this table. `customerId` is a foreign key referencing the `id` column in the `Customers` table. + +### Examples + +**Example 1:** + +**Input:** + +**Customers table:** + +| id | name | +| --- | ----- | +| 1 | Joe | +| 2 | Henry | +| 3 | Sam | +| 4 | Max | + +**Orders table:** + +| id | customerId | +| --- | ---------- | +| 1 | 3 | +| 2 | 1 | + +**Output:** + +| Customers | +| --------- | +| Henry | +| Max | + +### Approach + +1. Perform a `LEFT JOIN` between the `Customers` table and the `Orders` table on the `id` and `customerId` columns. +2. Use the `WHERE` clause to filter out the customers who have `NULL` in the `Orders` table, which indicates that they never placed an order. + +### Solution + +#### SQL + +```sql +-- SQL solution to find customers who never order anything + +SELECT + name AS Customers +FROM + Customers +LEFT JOIN + Orders +ON + Customers.id = Orders.customerId +WHERE + Orders.customerId IS NULL; +``` + +### Explanation + +- **LEFT JOIN**: Joins the `Customers` table with the `Orders` table. This join includes all customers, even those who have not placed any orders. +- **WHERE Orders.customerId IS NULL**: Filters the result to include only those customers who do not have a corresponding entry in the `Orders` table. + +### Complexity Analysis + +- **Time Complexity**: O(n), where n is the number of rows in the `Customers` table. +- **Space Complexity**: O(n), where n is the number of rows in the `Customers` table. + +### References + +- **LeetCode Problem**: Customers Who Never Order \ No newline at end of file From e5a71495996b933c0f5428c4e2760334e26a21b4 Mon Sep 17 00:00:00 2001 From: pallasivasai Date: Sun, 14 Jul 2024 08:01:22 +0530 Subject: [PATCH 2/2] adding Solution for a problem 182 --- .../0100-0199/0182-Duplicate-Emails.md | 73 +++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 dsa-solutions/lc-solutions/0100-0199/0182-Duplicate-Emails.md diff --git a/dsa-solutions/lc-solutions/0100-0199/0182-Duplicate-Emails.md b/dsa-solutions/lc-solutions/0100-0199/0182-Duplicate-Emails.md new file mode 100644 index 000000000..90661a3a0 --- /dev/null +++ b/dsa-solutions/lc-solutions/0100-0199/0182-Duplicate-Emails.md @@ -0,0 +1,73 @@ +--- +id: duplicate-emails +title: Duplicate Emails +sidebar_label: 0183. Duplicate Emails +tags: +- SQL +- Database +description: "Solution to Leetcode 183. Duplicate Emails" +--- + +## Problem Description + +Given a table `Person`, write a query to find all duplicate emails. + +**Table: Person** + +| Column Name | Type | +| ----------- | ------- | +| id | int | +| email | varchar | +id is the primary key for this table. Each row of this table contains an email. The emails will not contain uppercase letters. + +### Examples + +**Example 1:** + +**Input:** + +**Person table:** + +| id | email | +| --- | ------- | +| 1 | a@b.com | +| 2 | c@d.com | +| 3 | a@b.com | + +**Output:** + +| Email | +| ------- | +| a@b.com | + +### Approach + +1. Group the rows in the `Person` table by the `email` column. +2. Use the `HAVING` clause to filter out the groups that have a count greater than 1, indicating duplicate emails. + +### Solution + +#### SQL + +```sql +-- SQL solution to find duplicate emails + +SELECT email +FROM Person +GROUP BY email +HAVING COUNT(*) > 1; +``` + +### Explanation + +- **GROUP BY email**: Groups the records by the `email` column. +- **HAVING COUNT(*) > 1**: Filters the groups to include only those with more than one occurrence, indicating that the email is duplicated. + +### Complexity Analysis + +- **Time Complexity**: O(n), where n is the number of rows in the `Person` table. +- **Space Complexity**: O(n), where n is the number of rows in the `Person` table. + +### References + +- **LeetCode Problem**: Duplicate Emails \ No newline at end of file