Skip to content

Commit fbefd4c

Browse files
feat: Organize repository for self-guided LeetCode tutorials
This commit introduces a new structure and documentation to transform the repository into a self-guided LeetCode SQL tutorial platform. Key changes include: - **Enhanced `README.md`:** Updated the main README with a description of the tutorial format and a "How to Use This Repository" section. - **`DOCUMENTATION_GUIDE.md`:** Added a new guide that defines a standard structure for individual LeetCode problem files. This includes sections for Problem Title, LeetCode Link, Problem Description, SQL Solution, Explanation, and Key Concepts. - **Sample File Restructuring:** Reformatted three existing SQL files (`Easy/Ads performance.sql`, `Medium/Active Businesses.sql`, `Hard/Average Salary.sql`) to demonstrate the new documentation structure. These files now include placeholders for detailed explanations and key concepts. - **`CONTRIBUTING.md`:** Added a new file with guidelines for community contributions, referencing the `DOCUMENTATION_GUIDE.md`. - **`LICENSE`:** Added an MIT License file to clarify usage rights. This reorganization aims to make the repository more user-friendly for learning SQL and to provide a consistent framework for documenting solutions.
1 parent 4458941 commit fbefd4c

File tree

7 files changed

+390
-131
lines changed

7 files changed

+390
-131
lines changed

CONTRIBUTING.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Contributing to SQL LeetCode Tutorials
2+
3+
Thank you for considering contributing to this repository! Your help is appreciated in making this a comprehensive resource for learning SQL through LeetCode problems.
4+
5+
## How to Contribute
6+
7+
We welcome contributions in various forms:
8+
9+
* **Adding New Solutions:** If you've solved a LeetCode SQL problem that isn't here yet.
10+
* **Improving Explanations:** If you can provide a clearer or more detailed explanation for an existing solution.
11+
* **Adding Key Concepts:** If you can identify and list relevant SQL concepts for a solution.
12+
* **Fixing Errors:** If you find any errors in problem descriptions, solutions, or explanations.
13+
14+
## Contribution Guidelines
15+
16+
To ensure consistency and quality, please follow these guidelines:
17+
18+
1. **Documentation Standard:** All contributions must adhere to the structure outlined in the `DOCUMENTATION_GUIDE.md`. Please familiarize yourself with it before submitting your changes.
19+
2. **File Naming:**
20+
* Use the official LeetCode problem title for the filename, replacing spaces with hyphens (e.g., `Easy/Find-Customers-With-Positive-Revenue-This-Year.sql`).
21+
* Place the file in the appropriate difficulty directory (`Easy/`, `Medium/`, `Hard/`).
22+
3. **Problem Information:**
23+
* Ensure the "Problem Title (from LeetCode)" and "LeetCode Link" sections are accurately filled out.
24+
4. **Clear Explanations:** If adding or modifying an explanation, make it clear, concise, and easy to understand. Break down complex logic.
25+
5. **Accurate Key Concepts:** List the primary SQL concepts demonstrated by the solution.
26+
6. **One Problem Per Pull Request:** If you're submitting multiple new solutions, please create a separate pull request for each problem. This makes reviewing easier.
27+
28+
## Setting Up Your Environment (General Guide)
29+
30+
1. **Fork the Repository:** Click the 'Fork' button at the top right of this page.
31+
2. **Clone Your Fork:** `git clone https://github.com/YOUR_USERNAME/SQL-Leetcode-Challenge.git`
32+
3. **Create a New Branch:** `git checkout -b your-feature-branch-name` (e.g., `add-solution-median-employee-salary`)
33+
4. **Make Your Changes:** Add your solution or improvements, ensuring you follow the `DOCUMENTATION_GUIDE.md`.
34+
5. **Commit Your Changes:** `git commit -am "feat: Add solution for Median Employee Salary"` (Follow conventional commit message standards if possible).
35+
6. **Push to Your Fork:** `git push origin your-feature-branch-name`
36+
7. **Open a Pull Request:** Go to the original repository page on GitHub and click 'New pull request'.
37+
38+
## Code of Conduct
39+
40+
While this is a small project, please be respectful and constructive in all communications.
41+
42+
---
43+
44+
Thank you for contributing!

DOCUMENTATION_GUIDE.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Documentation Guide for LeetCode Problems
2+
3+
This guide outlines the standard format for documenting SQL LeetCode problems in this repository. Adhering to this structure will ensure consistency and make it easier for users to learn.
4+
5+
Each problem solution file should be a Markdown file (`.md`) or a SQL file (`.sql`) that includes Markdown-formatted comments at the top, and should contain the following sections:
6+
7+
## Problem Title (from LeetCode)
8+
9+
* Clearly state the official title of the LeetCode problem.
10+
11+
## LeetCode Link
12+
13+
* Provide a direct hyperlink to the problem on the LeetCode website.
14+
* Example: `[Problem Name](https://leetcode.com/problems/problem-name/)`
15+
16+
## Problem Description
17+
18+
* Copy and paste the complete problem description from LeetCode.
19+
* Ensure all formatting (like tables, examples, constraints) is preserved as much as possible using Markdown.
20+
21+
## SQL Solution
22+
23+
* Present the complete SQL solution.
24+
* Use SQL code blocks for proper formatting and syntax highlighting.
25+
26+
```sql
27+
-- Your SQL solution here
28+
SELECT column_name
29+
FROM table_name
30+
WHERE condition;
31+
```
32+
33+
## Explanation
34+
35+
* This is a critical section for learning.
36+
* Provide a clear, step-by-step breakdown of your SQL solution.
37+
* Explain the thought process:
38+
* Why did you choose certain SQL clauses (e.g., `JOIN` vs. `LEFT JOIN`)?
39+
* How does the logic flow to arrive at the correct answer?
40+
* Break down complex queries into smaller, understandable parts.
41+
* Explain any specific functions or techniques used.
42+
* Use clear and concise language.
43+
44+
## Key Concepts
45+
46+
* List the main SQL concepts and techniques demonstrated in the solution.
47+
* This helps users identify what they are learning or can practice with this problem.
48+
* Examples:
49+
* `INNER JOIN`
50+
* `GROUP BY`
51+
* `HAVING`
52+
* `Window Functions (e.g., ROW_NUMBER(), RANK())`
53+
* `Common Table Expressions (CTEs)`
54+
* `Subqueries`
55+
* `CASE WHEN statements`
56+
* `Date/Time Functions`
57+
* `String Manipulation`
58+
59+
---
60+
61+
**Note:** If you are documenting a solution within a `.sql` file, please use comment blocks for each section heading, for example:
62+
63+
```sql
64+
/*
65+
## Problem Title (from LeetCode)
66+
...
67+
*/
68+
69+
/*
70+
## LeetCode Link
71+
...
72+
*/
73+
74+
/*
75+
## Problem Description
76+
...
77+
*/
78+
79+
/*
80+
## SQL Solution
81+
*/
82+
83+
-- Your SQL solution here
84+
SELECT column_name
85+
FROM table_name
86+
WHERE condition;
87+
88+
/*
89+
## Explanation
90+
...
91+
*/
92+
93+
/*
94+
## Key Concepts
95+
...
96+
*/
97+
```
98+
99+
By following this guide, we can create a valuable and consistent learning resource.

Easy/Ads performance.sql

Lines changed: 83 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,77 @@
1-
-- Question 13
2-
-- Table: Ads
3-
4-
-- +---------------+---------+
5-
-- | Column Name | Type |
6-
-- +---------------+---------+
7-
-- | ad_id | int |
8-
-- | user_id | int |
9-
-- | action | enum |
10-
-- +---------------+---------+
11-
-- (ad_id, user_id) is the primary key for this table.
12-
-- Each row of this table contains the ID of an Ad, the ID of a user and the action taken by this user regarding this Ad.
13-
-- The action column is an ENUM type of ('Clicked', 'Viewed', 'Ignored').
1+
/*
2+
## Problem Title (from LeetCode)
3+
(Placeholder: Add LeetCode problem title here)
4+
*/
5+
6+
/*
7+
## LeetCode Link
8+
(Placeholder: Add direct link to LeetCode problem here)
9+
*/
10+
11+
/*
12+
## Problem Description
13+
Table: Ads
14+
15+
+---------------+---------+
16+
| Column Name | Type |
17+
+---------------+---------+
18+
| ad_id | int |
19+
| user_id | int |
20+
| action | enum |
21+
+---------------+---------+
22+
(ad_id, user_id) is the primary key for this table.
23+
Each row of this table contains the ID of an Ad, the ID of a user and the action taken by this user regarding this Ad.
24+
The action column is an ENUM type of ('Clicked', 'Viewed', 'Ignored').
1425
1526
16-
-- A company is running Ads and wants to calculate the performance of each Ad.
17-
18-
-- Performance of the Ad is measured using Click-Through Rate (CTR) where:
19-
20-
21-
22-
-- Write an SQL query to find the ctr of each Ad.
23-
24-
-- Round ctr to 2 decimal points. Order the result table by ctr in descending order and by ad_id in ascending order in case of a tie.
25-
26-
-- The query result format is in the following example:
27-
28-
-- Ads table:
29-
-- +-------+---------+---------+
30-
-- | ad_id | user_id | action |
31-
-- +-------+---------+---------+
32-
-- | 1 | 1 | Clicked |
33-
-- | 2 | 2 | Clicked |
34-
-- | 3 | 3 | Viewed |
35-
-- | 5 | 5 | Ignored |
36-
-- | 1 | 7 | Ignored |
37-
-- | 2 | 7 | Viewed |
38-
-- | 3 | 5 | Clicked |
39-
-- | 1 | 4 | Viewed |
40-
-- | 2 | 11 | Viewed |
41-
-- | 1 | 2 | Clicked |
42-
-- +-------+---------+---------+
43-
-- Result table:
44-
-- +-------+-------+
45-
-- | ad_id | ctr |
46-
-- +-------+-------+
47-
-- | 1 | 66.67 |
48-
-- | 3 | 50.00 |
49-
-- | 2 | 33.33 |
50-
-- | 5 | 0.00 |
51-
-- +-------+-------+
52-
-- for ad_id = 1, ctr = (2/(2+1)) * 100 = 66.67
53-
-- for ad_id = 2, ctr = (1/(1+2)) * 100 = 33.33
54-
-- for ad_id = 3, ctr = (1/(1+1)) * 100 = 50.00
55-
-- for ad_id = 5, ctr = 0.00, Note that ad_id = 5 has no clicks or views.
56-
-- Note that we don't care about Ignored Ads.
57-
-- Result table is ordered by the ctr. in case of a tie we order them by ad_id
58-
59-
-- Solution
27+
A company is running Ads and wants to calculate the performance of each Ad.
28+
29+
Performance of the Ad is measured using Click-Through Rate (CTR) where:
30+
31+
32+
33+
Write an SQL query to find the ctr of each Ad.
34+
35+
Round ctr to 2 decimal points. Order the result table by ctr in descending order and by ad_id in ascending order in case of a tie.
36+
37+
The query result format is in the following example:
38+
39+
Ads table:
40+
+-------+---------+---------+
41+
| ad_id | user_id | action |
42+
+-------+---------+---------+
43+
| 1 | 1 | Clicked |
44+
| 2 | 2 | Clicked |
45+
| 3 | 3 | Viewed |
46+
| 5 | 5 | Ignored |
47+
| 1 | 7 | Ignored |
48+
| 2 | 7 | Viewed |
49+
| 3 | 5 | Clicked |
50+
| 1 | 4 | Viewed |
51+
| 2 | 11 | Viewed |
52+
| 1 | 2 | Clicked |
53+
+-------+---------+---------+
54+
Result table:
55+
+-------+-------+
56+
| ad_id | ctr |
57+
+-------+-------+
58+
| 1 | 66.67 |
59+
| 3 | 50.00 |
60+
| 2 | 33.33 |
61+
| 5 | 0.00 |
62+
+-------+-------+
63+
for ad_id = 1, ctr = (2/(2+1)) * 100 = 66.67
64+
for ad_id = 2, ctr = (1/(1+2)) * 100 = 33.33
65+
for ad_id = 3, ctr = (1/(1+1)) * 100 = 50.00
66+
for ad_id = 5, ctr = 0.00, Note that ad_id = 5 has no clicks or views.
67+
Note that we don't care about Ignored Ads.
68+
Result table is ordered by the ctr. in case of a tie we order them by ad_id
69+
*/
70+
71+
/*
72+
## SQL Solution
73+
*/
74+
6075
with t1 as(
6176
select ad_id, sum(case when action in ('Clicked') then 1 else 0 end) as clicked
6277
from ads
@@ -76,4 +91,14 @@ from
7691
select *
7792
from t1 join t2
7893
on t1.ad_id = t2.ad) a
79-
order by ctr desc, ad_id
94+
order by ctr desc, ad_id
95+
96+
/*
97+
## Explanation
98+
(Placeholder: Explanation to be added.)
99+
*/
100+
101+
/*
102+
## Key Concepts
103+
(Placeholder: List key SQL concepts used, e.g., JOIN, GROUP BY, CTEs, etc.)
104+
*/

0 commit comments

Comments
 (0)