Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# IDE and Editor files
.vscode/
.idea/
*.swp
*.swo
*~

# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

# Database files (if any)
*.db
*.sqlite
*.sqlite3

# Log files
*.log

# Temporary files
/tmp/
temp/
*.tmp

# Node modules (if any JavaScript tooling is added)
node_modules/

# Python cache (if any Python tooling is added)
__pycache__/
*.pyc
*.pyo

# Build outputs (if any)
build/
dist/
out/
140 changes: 140 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# Contributing to Leetcode SQL Solutions 🀝

Thank you for your interest in contributing to this repository! This guide will help you get started.

## How to Contribute

### 1. Adding New Solutions

1. **Check for Duplicates**: Ensure the problem hasn't been solved already
2. **Choose Correct Folder**: Place solutions in appropriate difficulty folder (Easy/Medium/Hard)
3. **Follow Naming Convention**: Use `{ProblemNumber}_{ProblemName}/` format
4. **Use the Template**: Copy and fill out the solution template from `Templates/solution_template.sql`

### 2. Improving Existing Solutions

- Alternative solutions are welcome
- Performance optimizations
- Better explanations and comments
- Bug fixes

### 3. Repository Improvements

- Documentation updates
- Schema additions
- Template improvements
- Organizational enhancements

## Submission Guidelines

### File Structure
```
Difficulty/
└── ProblemNumber_ProblemName/
└── ProblemNumber_ProblemName.sql
```

### Solution Requirements

**Must Include:**
- Complete problem statement (as comment)
- Table schema definition
- Example input/output
- Working SQL solution
- Detailed explanation
- Time/space complexity (when applicable)

**Good to Have:**
- Alternative approaches
- Edge case considerations
- Performance notes
- Related problems references

### Code Style

- Use clear, readable SQL formatting
- Include meaningful comments
- Follow standard SQL conventions
- Use consistent indentation (2 or 4 spaces)
- UPPERCASE for SQL keywords (SELECT, FROM, WHERE, etc.)

### Example Format
```sql
/*
Problem: 175 - Combine Two Tables
Difficulty: Easy
Source: https://leetcode.com/problems/combine-two-tables/
Date Solved: YYYY-MM-DD

Problem Statement:
[Full problem description]

Table Schema:
[Schema definitions]
*/

-- Solution approach explanation
SELECT
column1,
column2
FROM
table1 t1
LEFT JOIN table2 t2 ON t1.id = t2.id;

/*
Explanation:
[Detailed explanation of approach and logic]
*/
```

## Testing Your Solutions

1. **Verify Syntax**: Ensure SQL syntax is correct
2. **Test with Sample Data**: Use provided examples to verify output
3. **Consider Edge Cases**: Test with empty tables, NULL values, etc.
4. **Performance Check**: For complex queries, consider execution efficiency

## Pull Request Process

1. **Fork the Repository**
2. **Create Feature Branch**: `git checkout -b solution/problem-number-name`
3. **Follow File Structure**: Use correct naming and placement
4. **Add Complete Solution**: Include all required components
5. **Test Thoroughly**: Verify your solution works
6. **Submit PR**: Include clear description of changes
7. **Respond to Feedback**: Address any review comments promptly

## Quality Standards

### SQL Code Quality
- βœ… Correct and efficient solution
- βœ… Proper formatting and indentation
- βœ… Clear variable and alias names
- βœ… Appropriate comments and documentation

### Documentation Quality
- βœ… Complete problem statement
- βœ… Clear explanation of approach
- βœ… Example input/output included
- βœ… Complexity analysis (when applicable)

## Getting Help

- **Issues**: Open an issue for questions or problems
- **Discussions**: Use discussions for general questions
- **Review**: All contributions are reviewed before merging

## Recognition

Contributors will be recognized in the repository and their contributions are greatly appreciated!

## Code of Conduct

- Be respectful and constructive
- Help others learn and improve
- Focus on problem-solving and learning
- Maintain high quality standards

---

Happy coding and thank you for contributing! πŸš€
46 changes: 46 additions & 0 deletions Database-Design/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# Database Design Problems πŸ—οΈ

This folder contains LeetCode problems focused on database design, schema optimization, and data modeling concepts.

## Problem Categories

### Schema Design
- Table normalization and denormalization
- Relationship modeling (1:1, 1:N, N:N)
- Primary and foreign key design
- Index strategy planning

### Data Integrity
- Constraint implementation
- Referential integrity maintenance
- Data validation rules
- Transaction design

### Performance Optimization
- Query optimization techniques
- Index design and usage
- Partitioning strategies
- Materialized view concepts

### Data Architecture
- ETL process design
- Data warehouse modeling
- OLTP vs OLAP considerations
- Scalability planning

## Key Concepts

- **Normalization Forms**: 1NF, 2NF, 3NF, BCNF
- **ACID Properties**: Atomicity, Consistency, Isolation, Durability
- **CAP Theorem**: Consistency, Availability, Partition tolerance
- **Design Patterns**: Star schema, snowflake schema, fact/dimension tables

## Study Resources

Focus on understanding:
- When to normalize vs denormalize
- Trade-offs between storage and performance
- Indexing strategies for different query patterns
- Scalability considerations for growing datasets

These problems test your understanding of database fundamentals and system design principles!
98 changes: 98 additions & 0 deletions Easy/175_Combine_Two_Tables/175_Combine_Two_Tables.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Problem: 175 - Combine Two Tables
Difficulty: Easy
Source: https://leetcode.com/problems/combine-two-tables/
Date Solved: 2024-09-29

Problem Statement:
Write a SQL query for a report that provides the following information for each person in the Person table, regardless if there is an address for the person or not:
- FirstName, LastName, City, State

Table Schema:
Table: Person
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| PersonId | int |
| FirstName | varchar |
| LastName | varchar |
+-------------+---------+
PersonId is the primary key column for this table.

Table: Address
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| AddressId | int |
| PersonId | int |
| City | varchar |
| State | varchar |
+-------------+---------+
AddressId is the primary key column for this table.

Example:
Input:
Person table:
+----------+----------+-----------+
| PersonId | FirstName| LastName |
+----------+----------+-----------+
| 1 | Wang | Allen |
| 2 | Alice | Bob |
+----------+----------+-----------+

Address table:
+----------+----------+---------------+----------+
| AddressId| PersonId | City | State |
+----------+----------+---------------+----------+
| 1 | 2 | New York City | New York |
| 2 | 3 | Leetcode | California|
+----------+----------+---------------+----------+

Output:
+-----------+----------+---------------+----------+
| FirstName | LastName | City | State |
+-----------+----------+---------------+----------+
| Allen | Wang | Null | Null |
| Bob | Alice | New York City | New York |
+-----------+----------+---------------+----------+

Constraints:
- Some people may not have address information
- We want information for ALL people in Person table
*/

-- Solution:
-- Use LEFT JOIN to include all people from Person table, even if they don't have an address

SELECT
p.FirstName,
p.LastName,
a.City,
a.State
FROM
Person p
LEFT JOIN Address a ON p.PersonId = a.PersonId;

/*
Explanation:
This problem requires a LEFT JOIN because we need to include ALL people from the Person table,
regardless of whether they have a corresponding address in the Address table.

- LEFT JOIN ensures all rows from the left table (Person) are included
- If there's no matching address, the City and State will be NULL
- INNER JOIN would exclude people without addresses (incorrect for this problem)
- RIGHT JOIN would exclude people with addresses but include orphaned addresses (also incorrect)

Time complexity: O(n + m) where n is number of people and m is number of addresses
Space complexity: O(1) - no additional space needed for the join operation

Alternative Solutions:
This is the most straightforward approach. You could also use:
- A subquery approach, but it would be less efficient
- UNION with conditional logic, but it's unnecessarily complex

Notes:
- This is a classic example of when to use LEFT JOIN vs INNER JOIN
- The key insight is understanding the requirement "regardless if there is an address"
- Always read problem requirements carefully to determine the correct JOIN type
*/
49 changes: 49 additions & 0 deletions Easy/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Easy SQL Problems 🟒

This folder contains LeetCode SQL problems classified as "Easy" difficulty.

## Characteristics of Easy Problems

- **Basic SQL Operations**: SELECT, WHERE, JOIN, GROUP BY
- **Simple Logic**: Straightforward filtering and aggregation
- **Common Patterns**: Basic joins, simple subqueries, date functions
- **Problem Numbers**: Typically in the 175-500+ range

## Problem Categories

### Data Retrieval
- Basic SELECT statements
- Filtering with WHERE clauses
- Sorting with ORDER BY

### Joins
- INNER JOIN operations
- LEFT JOIN for missing data scenarios
- Simple two-table joins

### Aggregations
- COUNT, SUM, AVG functions
- Basic GROUP BY operations
- Simple HAVING clauses

### Date/Time Operations
- Date comparisons
- Date formatting
- Simple date arithmetic

## Study Tips

1. **Master the Basics**: Ensure you understand fundamental SQL syntax
2. **Practice Joins**: Many easy problems involve simple table joins
3. **Read Carefully**: Problem statements are usually straightforward
4. **Test Edge Cases**: Consider NULL values and empty result sets

## Example Problem Types

- Combine Two Tables
- Duplicate Emails
- Customers Who Never Order
- Employee Earning More Than Their Managers
- Delete Duplicate Emails

Start here if you're new to SQL or want to build confidence before tackling harder problems!
Loading