Skip to content

Commit e0fabe1

Browse files
committed
Upload 7-Advanced-String-Functions-RegEx-Clause
1 parent 09314fa commit e0fabe1

File tree

4 files changed

+216
-9
lines changed

4 files changed

+216
-9
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
-- Source: https://leetcode.com/problems/find-users-with-valid-e-mails/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Users
4+
5+
-- +---------------+---------+
6+
-- | Column Name | Type |
7+
-- +---------------+---------+
8+
-- | user_id | int |
9+
-- | name | varchar |
10+
-- | mail | varchar |
11+
-- +---------------+---------+
12+
-- user_id is the primary key (column with unique values) for this table.
13+
-- This table contains information of the users signed up in a website. Some e-mails are invalid.
14+
15+
-- Write a solution to find the users who have valid emails.
16+
17+
-- A valid e-mail has a prefix name and a domain where:
18+
19+
-- The prefix name is a string that may contain letters (upper or lower case), digits, underscore '_', period '.', and/or dash '-'. The prefix name must start with a letter.
20+
-- The domain is '@leetcode.com'.
21+
-- Return the result table in any order.
22+
23+
-- The result format is in the following example.
24+
25+
-- Example 1:
26+
27+
-- Input:
28+
-- Users table:
29+
-- +---------+-----------+-------------------------+
30+
-- | user_id | name | mail |
31+
-- +---------+-----------+-------------------------+
32+
-- | 1 | Winston | winston@leetcode.com |
33+
-- | 2 | Jonathan | jonathanisgreat |
34+
-- | 3 | Annabelle | bella-@leetcode.com |
35+
-- | 4 | Sally | sally.come@leetcode.com |
36+
-- | 5 | Marwan | quarz#2020@leetcode.com |
37+
-- | 6 | David | david69@gmail.com |
38+
-- | 7 | Shapiro | .shapo@leetcode.com |
39+
-- +---------+-----------+-------------------------+
40+
-- Output:
41+
-- +---------+-----------+-------------------------+
42+
-- | user_id | name | mail |
43+
-- +---------+-----------+-------------------------+
44+
-- | 1 | Winston | winston@leetcode.com |
45+
-- | 3 | Annabelle | bella-@leetcode.com |
46+
-- | 4 | Sally | sally.come@leetcode.com |
47+
-- +---------+-----------+-------------------------+
48+
-- Explanation:
49+
-- The mail of user 2 does not have a domain.
50+
-- The mail of user 5 has the # sign which is not allowed.
51+
-- The mail of user 6 does not have the leetcode domain.
52+
-- The mail of user 7 starts with a period.
53+
54+
------------------------------------------------------------------------------
55+
56+
-- SQL Schema
57+
58+
Create table If Not Exists Users (user_id int, name varchar(30), mail varchar(50))
59+
Truncate table Users
60+
insert into Users (user_id, name, mail) values ('1', 'Winston', 'winston@leetcode.com')
61+
insert into Users (user_id, name, mail) values ('2', 'Jonathan', 'jonathanisgreat')
62+
insert into Users (user_id, name, mail) values ('3', 'Annabelle', 'bella-@leetcode.com')
63+
insert into Users (user_id, name, mail) values ('4', 'Sally', 'sally.come@leetcode.com')
64+
insert into Users (user_id, name, mail) values ('5', 'Marwan', 'quarz#2020@leetcode.com')
65+
insert into Users (user_id, name, mail) values ('6', 'David', 'david69@gmail.com')
66+
insert into Users (user_id, name, mail) values ('7', 'Shapiro', '.shapo@leetcode.com')
67+
68+
-- MS SQL Server Code
69+
70+
SELECT user_id, name, mail
71+
FROM Users
72+
WHERE mail LIKE '[A-Za-z]%@leetcode.com'
73+
AND mail NOT LIKE '%[^A-Za-z0-9.\_-]%@leetcode.com'
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
-- Source: https://leetcode.com/problems/second-highest-salary/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Employee
4+
5+
-- +-------------+------+
6+
-- | Column Name | Type |
7+
-- +-------------+------+
8+
-- | id | int |
9+
-- | salary | int |
10+
-- +-------------+------+
11+
-- id is the primary key (column with unique values) for this table.
12+
-- Each row of this table contains information about the salary of an employee.
13+
14+
-- Write a solution to find the second highest salary from the Employee table. If there is no second highest salary, return null (return None in Pandas).
15+
16+
-- The result format is in the following example.
17+
18+
-- Example 1:
19+
20+
-- Input:
21+
-- Employee table:
22+
-- +----+--------+
23+
-- | id | salary |
24+
-- +----+--------+
25+
-- | 1 | 100 |
26+
-- | 2 | 200 |
27+
-- | 3 | 300 |
28+
-- +----+--------+
29+
-- Output:
30+
-- +---------------------+
31+
-- | SecondHighestSalary |
32+
-- +---------------------+
33+
-- | 200 |
34+
-- +---------------------+
35+
-- Example 2:
36+
37+
-- Input:
38+
-- Employee table:
39+
-- +----+--------+
40+
-- | id | salary |
41+
-- +----+--------+
42+
-- | 1 | 100 |
43+
-- +----+--------+
44+
-- Output:
45+
-- +---------------------+
46+
-- | SecondHighestSalary |
47+
-- +---------------------+
48+
-- | null |
49+
-- +---------------------+
50+
51+
------------------------------------------------------------------------------
52+
53+
-- SQL Schema
54+
55+
Create table If Not Exists Employee (id int, salary int)
56+
Truncate table Employee
57+
insert into Employee (id, salary) values ('1', '100')
58+
insert into Employee (id, salary) values ('2', '200')
59+
insert into Employee (id, salary) values ('3', '300')
60+
61+
-- MS SQL Server Code
62+
63+
SELECT (
64+
SELECT DISTINCT salary
65+
FROM Employee
66+
ORDER BY salary DESC
67+
OFFSET 1 ROW
68+
FETCH NEXT 1 ROW ONLY
69+
) AS SecondHighestSalary;
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
-- Source: https://leetcode.com/problems/delete-duplicate-emails/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
-- Table: Person
4+
5+
-- +-------------+---------+
6+
-- | Column Name | Type |
7+
-- +-------------+---------+
8+
-- | id | int |
9+
-- | email | varchar |
10+
-- +-------------+---------+
11+
-- id is the primary key (column with unique values) for this table.
12+
-- Each row of this table contains an email. The emails will not contain uppercase letters.
13+
14+
-- Write a solution to delete all duplicate emails, keeping only one unique email with the smallest id.
15+
16+
-- For SQL users, please note that you are supposed to write a DELETE statement and not a SELECT one.
17+
18+
-- For Pandas users, please note that you are supposed to modify Person in place.
19+
20+
-- After running your script, the answer shown is the Person table. The driver will first compile and run your piece of code and then show the Person table. The final order of the Person table does not matter.
21+
22+
-- The result format is in the following example.
23+
24+
-- Example 1:
25+
26+
-- Input:
27+
-- Person table:
28+
-- +----+------------------+
29+
-- | id | email |
30+
-- +----+------------------+
31+
-- | 1 | john@example.com |
32+
-- | 2 | bob@example.com |
33+
-- | 3 | john@example.com |
34+
-- +----+------------------+
35+
-- Output:
36+
-- +----+------------------+
37+
-- | id | email |
38+
-- +----+------------------+
39+
-- | 1 | john@example.com |
40+
-- | 2 | bob@example.com |
41+
-- +----+------------------+
42+
-- Explanation: john@example.com is repeated two times. We keep the row with the smallest Id = 1.
43+
44+
------------------------------------------------------------------------------
45+
46+
-- SQL Schema
47+
48+
Create table If Not Exists Person (Id int, Email varchar(255))
49+
Truncate table Person
50+
insert into Person (id, email) values ('1', 'john@example.com')
51+
insert into Person (id, email) values ('2', 'bob@example.com')
52+
insert into Person (id, email) values ('3', 'john@example.com')
53+
54+
-- MS SQL Server Code
55+
56+
WITH DuplicateEmails AS (
57+
SELECT
58+
id
59+
, email
60+
, ROW_NUMBER() OVER(PARTITION BY email ORDER BY id) AS rank_id
61+
FROM Person
62+
)
63+
64+
DELETE FROM DuplicateEmails
65+
WHERE rank_id > 1;

README.md

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ This github repository contains my solutions for the Top SQL 50 problems using M
9494

9595
### Advanced String Functions / Regex Clause
9696

97-
| # | Problem | Level | Status |
98-
| ---- | ------------------------------------- | ------ | ------ |
99-
| 1667 | Fix Names in a Table | Easy | - |
100-
| 1527 | Patients with a Condition | Easy | - |
101-
| 196 | [Delete Duplicate Emails]() | Easy | Solved |
102-
| 176 | [Second Highest Salary]() | Medium | Solved |
103-
| 1484 | Group Sold Products By The Date | Easy | - |
104-
| 1327 | List the Products Ordered in a Period | Easy | - |
105-
| 1517 | [Find Users With Valid E-mails]() | Easy | Solved |
97+
| # | Problem | Level | Status |
98+
| ---- | ---------------------------------------------------------------------------------------------------------------- | ------ | ------ |
99+
| 1667 | Fix Names in a Table | Easy | - |
100+
| 1527 | Patients with a Condition | Easy | - |
101+
| 196 | [Delete Duplicate Emails](/7-Advanced-String-Functions-RegEx-Clause/196_Delete_Duplicate_Emails.sql) | Easy | Solved |
102+
| 176 | [Second Highest Salary](/7-Advanced-String-Functions-RegEx-Clause/176_Second_Highest_Salary.sql) | Medium | Solved |
103+
| 1484 | Group Sold Products By The Date | Easy | - |
104+
| 1327 | List the Products Ordered in a Period | Easy | - |
105+
| 1517 | [Find Users With Valid E-mails](/7-Advanced-String-Functions-RegEx-Clause/1517_Find_Users_With_Valid_Emails.sql) | Easy | Solved |

0 commit comments

Comments
 (0)