Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4e9523a

Browse files
Merge pull request #91 from iamAntimPal/Branch-1
Branch 1
2 parents bc61827 + bb626d0 commit 4e9523a

File tree

7 files changed

+663
-0
lines changed
  • LeetCode SQL 50 Solution

7 files changed

+663
-0
lines changed
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
# 📩 Find Users With Valid E-Mails - LeetCode 1517
2+
3+
## 📌 Problem Statement
4+
You are given a table **Users** that contains user registration details, including their emails. Some of these emails may be **invalid**.
5+
6+
A valid email:
7+
- Has a **prefix name** and a **domain**.
8+
- The prefix:
9+
- **Must start with a letter** (uppercase or lowercase).
10+
- Can contain **letters**, **digits**, **underscore (`_`)**, **period (`.`)**, and/or **dash (`-`)**.
11+
- The domain must be **"@leetcode.com"**.
12+
13+
Your task is to **find users with valid emails**.
14+
15+
---
16+
17+
## 📊 Table Structure
18+
19+
### **Users Table**
20+
| Column Name | Type |
21+
| ----------- | ------- |
22+
| user_id | int |
23+
| name | varchar |
24+
| mail | varchar |
25+
26+
- `user_id` is the **primary key**.
27+
28+
---
29+
30+
## 📊 Example 1:
31+
32+
### **Input:**
33+
#### **Users Table**
34+
| user_id | name | mail |
35+
| ------- | --------- | ----------------------- |
36+
| 1 | Winston | winston@leetcode.com |
37+
| 2 | Jonathan | jonathanisgreat |
38+
| 3 | Annabelle | bella-@leetcode.com |
39+
| 4 | Sally | sally.come@leetcode.com |
40+
| 5 | Marwan | quarz#2020@leetcode.com |
41+
| 6 | David | david69@gmail.com |
42+
| 7 | Shapiro | .shapo@leetcode.com |
43+
44+
### **Output:**
45+
| user_id | name | mail |
46+
| ------- | --------- | ----------------------- |
47+
| 1 | Winston | winston@leetcode.com |
48+
| 3 | Annabelle | bella-@leetcode.com |
49+
| 4 | Sally | sally.come@leetcode.com |
50+
51+
### **Explanation:**
52+
-**Valid emails:**
53+
- `winston@leetcode.com` ✅ (Starts with a letter, correct domain)
54+
- `bella-@leetcode.com` ✅ (Starts with a letter, correct domain)
55+
- `sally.come@leetcode.com` ✅ (Starts with a letter, correct domain)
56+
-**Invalid emails:**
57+
- `jonathanisgreat` ❌ (No domain)
58+
- `quarz#2020@leetcode.com` ❌ (Contains `#`, which is not allowed)
59+
- `david69@gmail.com` ❌ (Wrong domain)
60+
- `.shapo@leetcode.com` ❌ (Starts with a period `.`)
61+
62+
---
63+
64+
## 🖥 SQL Solution
65+
66+
### **Using `REGEXP_LIKE` (MySQL)**
67+
#### **Explanation:**
68+
- Use `REGEXP_LIKE(mail, '^[A-Za-z]+[A-Za-z0-9_.-]*@leetcode\\.com$')`
69+
- `^` → Start of string.
70+
- `[A-Za-z]+` → First character **must** be a **letter**.
71+
- `[A-Za-z0-9_.-]*` → Rest can be **letters, numbers, `_`, `.`, or `-`**.
72+
- `@leetcode\\.com$` → Must end with `"@leetcode.com"`.
73+
74+
```sql
75+
SELECT *
76+
FROM Users
77+
WHERE REGEXP_LIKE(mail, '^[A-Za-z]+[A-Za-z0-9_.-]*@leetcode\\.com$');
78+
```
79+
80+
---
81+
82+
## 📁 File Structure
83+
```
84+
📂 Find-Users-With-Valid-Emails
85+
│── 📜 README.md
86+
│── 📜 solution.sql
87+
│── 📜 test_cases.sql
88+
```
89+
90+
---
91+
92+
## 🔗 Useful Links
93+
- 📖 [LeetCode Problem](https://leetcode.com/problems/find-users-with-valid-e-mails/)
94+
- 🔍 [MySQL REGEXP_LIKE Documentation](https://dev.mysql.com/doc/refman/8.0/en/regexp.html)
95+
- 📝 [SQL Regular Expressions Cheatsheet](https://www.w3schools.com/sql/sql_regex.asp)
96+
her learning**
97+
## Would you like any modifications? 🚀
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# 🏥 Patients With a Condition - LeetCode 1527
2+
3+
## 📌 Problem Statement
4+
You are given a **Patients** table that stores patient health records, including their medical conditions.
5+
6+
Each patient's **conditions** column contains **0 or more condition codes**, separated by spaces.
7+
8+
Your task is to **find all patients who have Type I Diabetes**.
9+
- Type I Diabetes is identified by a condition **starting with the prefix "DIAB1"**.
10+
11+
Return the result **in any order**.
12+
13+
---
14+
15+
## 📊 Table Structure
16+
17+
### **Patients Table**
18+
| Column Name | Type |
19+
| ------------ | ------- |
20+
| patient_id | int |
21+
| patient_name | varchar |
22+
| conditions | varchar |
23+
24+
- `patient_id` is the **primary key**.
25+
- `conditions` contains **space-separated condition codes**.
26+
27+
---
28+
29+
## 📊 Example 1:
30+
31+
### **Input:**
32+
#### **Patients Table**
33+
| patient_id | patient_name | conditions |
34+
| ---------- | ------------ | ------------ |
35+
| 1 | Daniel | YFEV COUGH |
36+
| 2 | Alice | |
37+
| 3 | Bob | DIAB100 MYOP |
38+
| 4 | George | ACNE DIAB100 |
39+
| 5 | Alain | DIAB201 |
40+
41+
### **Output:**
42+
| patient_id | patient_name | conditions |
43+
| ---------- | ------------ | ------------ |
44+
| 3 | Bob | DIAB100 MYOP |
45+
| 4 | George | ACNE DIAB100 |
46+
47+
### **Explanation:**
48+
-**Bob's condition:** `"DIAB100 MYOP"`**Starts with `"DIAB1"`** → ✅ **Valid**
49+
-**George's condition:** `"ACNE DIAB100"`**Contains `"DIAB1"`** → ✅ **Valid**
50+
-**Daniel's condition:** `"YFEV COUGH"`**No `"DIAB1"`**
51+
-**Alice's condition:** `""` (Empty)
52+
-**Alain's condition:** `"DIAB201"` → Does **not** start with `"DIAB1"`
53+
54+
---
55+
56+
## 🖥 SQL Solution
57+
58+
### **Using `LIKE` with wildcards**
59+
#### **Explanation:**
60+
- We need to check if `"DIAB1"` appears **at the beginning** or **somewhere in the conditions column**.
61+
- `LIKE 'DIAB1%'` → Matches if `"DIAB1"` is at the **start** of the column.
62+
- `LIKE '% DIAB1%'` → Matches if `"DIAB1"` appears **after a space (as part of multiple conditions).**
63+
64+
```sql
65+
SELECT patient_id, patient_name, conditions
66+
FROM Patients
67+
WHERE conditions LIKE 'DIAB1%' OR conditions LIKE '% DIAB1%';
68+
```
69+
70+
---
71+
72+
## 📁 File Structure
73+
```
74+
📂 Patients-With-Condition
75+
│── 📜 README.md
76+
│── 📜 solution.sql
77+
│── 📜 test_cases.sql
78+
```
79+
80+
---
81+
82+
## 🔗 Useful Links
83+
- 📖 [LeetCode Problem](https://leetcode.com/problems/patients-with-a-condition/)
84+
- 🔍 [SQL LIKE Operator](https://www.w3schools.com/sql/sql_like.asp)
85+
- 📝 [MySQL String Functions](https://dev.mysql.com/doc/refman/8.0/en/string-functions.html)
86+
## Would you like any refinements? 🚀
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# 🏬 Customer Who Visited but Did Not Make Any Transactions - LeetCode 1581
2+
3+
## 📌 Problem Statement
4+
You are given two tables, **Visits** and **Transactions**.
5+
6+
- The **Visits** table records the customers who visited the mall.
7+
- The **Transactions** table records the **transactions made** during a visit.
8+
9+
Your task is to **find customers who visited but did not make any transactions**.
10+
Also, return the **number of times** these customers **visited without making any transactions**.
11+
12+
Return the result **in any order**.
13+
14+
---
15+
16+
## 📊 Table Structure
17+
18+
### **Visits Table**
19+
| Column Name | Type |
20+
| ----------- | ---- |
21+
| visit_id | int |
22+
| customer_id | int |
23+
24+
- `visit_id` is the **unique identifier** for each visit.
25+
26+
### **Transactions Table**
27+
| Column Name | Type |
28+
| -------------- | ---- |
29+
| transaction_id | int |
30+
| visit_id | int |
31+
| amount | int |
32+
33+
- `transaction_id` is the **unique identifier** for each transaction.
34+
- `visit_id` represents the visit **associated with this transaction**.
35+
36+
---
37+
38+
## 📊 Example 1:
39+
40+
### **Input:**
41+
#### **Visits Table**
42+
| visit_id | customer_id |
43+
| -------- | ----------- |
44+
| 1 | 23 |
45+
| 2 | 9 |
46+
| 4 | 30 |
47+
| 5 | 54 |
48+
| 6 | 96 |
49+
| 7 | 54 |
50+
| 8 | 54 |
51+
52+
#### **Transactions Table**
53+
| transaction_id | visit_id | amount |
54+
| -------------- | -------- | ------ |
55+
| 2 | 5 | 310 |
56+
| 3 | 5 | 300 |
57+
| 9 | 5 | 200 |
58+
| 12 | 1 | 910 |
59+
| 13 | 2 | 970 |
60+
61+
### **Output:**
62+
| customer_id | count_no_trans |
63+
| ----------- | -------------- |
64+
| 54 | 2 |
65+
| 30 | 1 |
66+
| 96 | 1 |
67+
68+
### **Explanation:**
69+
-**Customer 23:** Visited **once**, made **1 transaction** → ❌ Not included
70+
-**Customer 9:** Visited **once**, made **1 transaction** → ❌ Not included
71+
-**Customer 30:** Visited **once**, made **0 transactions** → ✅ Included (`count_no_trans = 1`)
72+
-**Customer 54:** Visited **3 times**, made **transactions in 1 visit**, **but 2 visits had no transactions** → ✅ Included (`count_no_trans = 2`)
73+
-**Customer 96:** Visited **once**, made **0 transactions** → ✅ Included (`count_no_trans = 1`)
74+
75+
---
76+
77+
## 🖥 SQL Solution
78+
79+
### **Using `NOT IN` to Find Visits Without Transactions**
80+
#### **Explanation:**
81+
- First, **find all visit IDs** that **had at least one transaction** (`SELECT DISTINCT visit_id FROM Transactions`).
82+
- Then, filter out these visit IDs from the **Visits** table.
83+
- Finally, count the number of such visits **per customer**.
84+
85+
```sql
86+
SELECT customer_id, COUNT(*) AS count_no_trans
87+
FROM Visits
88+
WHERE visit_id NOT IN (SELECT DISTINCT visit_id FROM Transactions)
89+
GROUP BY customer_id;
90+
```
91+
92+
---
93+
94+
## 📁 File Structure
95+
```
96+
📂 Customer-No-Transaction
97+
│── 📜 README.md
98+
│── 📜 solution.sql
99+
│── 📜 test_cases.sql
100+
```
101+
102+
---
103+
104+
## 🔗 Useful Links
105+
- 📖 [LeetCode Problem](https://leetcode.com/problems/customer-who-visited-but-did-not-make-any-transactions/)
106+
- 🔍 [SQL NOT IN Operator](https://www.w3schools.com/sql/sql_in.asp)
107+
- 📝 [MySQL Aggregate Functions](https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.html)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /