forked from codeharborhub/codeharborhub.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request codeharborhub#3168 from pallasivasai/182
Adding Solution for 183
- Loading branch information
Showing
2 changed files
with
169 additions
and
0 deletions.
There are no files selected for viewing
73 changes: 73 additions & 0 deletions
73
dsa-solutions/lc-solutions/0100-0199/0182-Duplicate-Emails.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | [email protected] | | ||
| 2 | [email protected] | | ||
| 3 | [email protected] | | ||
|
||
**Output:** | ||
|
||
| Email | | ||
| ------- | | ||
| [email protected] | | ||
|
||
### 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 |
96 changes: 96 additions & 0 deletions
96
dsa-solutions/lc-solutions/0100-0199/183-Customers-Who-Never-Order.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |