Skip to content

Commit 684c9e7

Browse files
committed
Initial commit
0 parents  commit 684c9e7

File tree

11 files changed

+290
-0
lines changed

11 files changed

+290
-0
lines changed

.gitattributes

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.sql linguist-language=PLpgSQL

.github/workflows/ci.yml

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: CI
2+
3+
on:
4+
pull_request:
5+
branches: [main, develop]
6+
7+
jobs:
8+
lint:
9+
runs-on: ubuntu-latest
10+
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Lint SQL
14+
run: |
15+
pip install sqlfluff
16+
sqlfluff lint leetcode/ --config .sqlfluff

.gitignore

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# macOS
2+
.DS_Store
3+
4+
# VS Code
5+
.vscode/*
6+
!.vscode/settings.json
7+
!.vscode/tasks.json
8+
!.vscode/launch.json
9+
!.vscode/extensions.json
10+
11+
# Ignore temporary files
12+
.tmp

.sqlfluff

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
[sqlfluff]
2+
dialect = postgres
3+
exclude_rules = LT01, CP02
4+
5+
[sqlfluff:rules]
6+
keywords_capitalisation_policy = upper
7+
max_line_length = 100

LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2025 Ivan Boyko <[email protected]>
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Leetcode (PostgreSQL)
2+
3+
[![MIT licensed](https://img.shields.io/badge/License-MIT-yellow.svg)](./LICENSE)
4+
[![CI](https://github.com/ivanbyone/hookify/actions/workflows/ci.yml/badge.svg)](https://github.com/Ivanbyone/leetcode-sql//actions)
5+
6+
## Description
7+
8+
📚 LeetCode SQL Solutions in PostgreSQL
9+
10+
A collection of LeetCode solutions in SQL using PostgreSQL. Includes:
11+
- Easy/Medium/Hard solutions
12+
- Optimized queries
13+
14+
🔹 Features:
15+
- Clean, formatted SQL code
16+
- Use of PostgreSQL-specific functions
17+
- Parsing complex queries
18+
- Regular updates
19+
20+
Useful for preparing for technical interviews and improving your SQL skills.
21+
22+
### Contributing
23+
24+
Style guide for contributors:
25+
26+
1. Add description to all tasks from Leetcode.
27+
2. Name file pattern: xxx.Task name.sql
28+
3. All keywords must be uppercase.
29+
4. For every task create new branch with pattern: task/task-name -> make PR to main.
30+
31+
Have a good contributing!
32+
33+
## Task List
34+
35+
1. [Easy](./leetcode/easy/)
36+
- [182. Duplicate Emails](./leetcode/easy/182.%20Duplicate%20Emails.sql)
37+
- [175. Combine Two Tables](./leetcode//easy/175.%20Combine%20Two%20Tables.sql)
38+
- [183. Customers Who Never Order](./leetcode/easy/183.%20Customers%20Who%20Never%20Order.sql)
39+
- [511. Game Play Analysis 1](./leetcode/easy/511.%20Game%20Play%20Analysis%201.sql)
40+
- [595. Big Countries](./leetcode/easy/595.%20Big%20Countries.sql)
41+
42+
## License
43+
44+
Check out LICENSE [page](./LICENSE).
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
Question 175. Combine Two Tables
3+
Link: https://leetcode.com/problems/combine-two-tables/description/
4+
5+
Table: Person
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| personId | int |
11+
| lastName | varchar |
12+
| firstName | varchar |
13+
+-------------+---------+
14+
personId is the primary key (column with unique values) for this table.
15+
This table contains information about the ID of some persons
16+
and their first and last names.
17+
18+
19+
Table: Address
20+
21+
+-------------+---------+
22+
| Column Name | Type |
23+
+-------------+---------+
24+
| addressId | int |
25+
| personId | int |
26+
| city | varchar |
27+
| state | varchar |
28+
+-------------+---------+
29+
addressId is the primary key (column with unique values)
30+
for this table.
31+
Each row of this table contains information about the city
32+
and state of one person with ID = PersonId.
33+
34+
35+
Write a solution to report the first name, last name, city,
36+
and state of each person in the Person table.
37+
If the address of a personId is not present in the Address table,
38+
report null instead.
39+
40+
Return the result table in any order.
41+
*/
42+
43+
SELECT
44+
Person.firstName,
45+
Person.lastName,
46+
Address.city,
47+
Address.state
48+
FROM
49+
Person
50+
LEFT JOIN
51+
Address
52+
ON
53+
Person.personId = Address.personId
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Question 182. Duplicate emails
3+
Link: https://leetcode.com/problems/duplicate-emails/description/
4+
5+
Table: Person
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| id | int |
11+
| email | varchar |
12+
+-------------+---------+
13+
id is the primary key (column with unique values) for this table.
14+
Each row of this table contains an email.
15+
The emails will not contain uppercase letters.
16+
17+
18+
Write a solution to report all the duplicate emails.
19+
Note that it's guaranteed that the email field is not NULL.
20+
21+
Return the result table in any order.
22+
*/
23+
24+
SELECT email
25+
FROM Person
26+
GROUP BY email
27+
HAVING COUNT(id) > 1;
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
Question 183. Customers Who Never Order.
3+
Link: https://leetcode.com/problems/customers-who-never-order/description/
4+
5+
Table: Customers
6+
7+
+-------------+---------+
8+
| Column Name | Type |
9+
+-------------+---------+
10+
| id | int |
11+
| name | varchar |
12+
+-------------+---------+
13+
id is the primary key (column with unique values) for this table.
14+
Each row of this table indicates the ID and name of a customer.
15+
16+
17+
Table: Orders
18+
19+
+-------------+------+
20+
| Column Name | Type |
21+
+-------------+------+
22+
| id | int |
23+
| customerId | int |
24+
+-------------+------+
25+
id is the primary key (column with unique values) for this table.
26+
customerId is a foreign key (reference columns)
27+
of the ID from the Customers table.
28+
Each row of this table indicates the ID
29+
of an order and the ID of the customer who ordered it.
30+
31+
32+
Write a solution to find all customers who never order anything.
33+
34+
Return the result table in any order.
35+
*/
36+
37+
SELECT Customers.name AS Customers
38+
FROM Customers
39+
LEFT JOIN
40+
Orders
41+
ON
42+
Customers.id = Orders.customerId
43+
WHERE Orders.id IS NULL
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
Question 511. Game Play Analysis 1.
3+
Link : https://leetcode.com/problems/game-play-analysis-i/description/
4+
5+
Table: Activity
6+
7+
+--------------+---------+
8+
| Column Name | Type |
9+
+--------------+---------+
10+
| player_id | int |
11+
| device_id | int |
12+
| event_date | date |
13+
| games_played | int |
14+
+--------------+---------+
15+
(player_id, event_date) is the primary key
16+
(combination of columns with unique values) of this table.
17+
This table shows the activity of players of some games.
18+
Each row is a record of a player who logged in and played
19+
a number of games (possibly 0) before logging out on someday using some device.
20+
21+
22+
Write a solution to find the first login date for each player.
23+
24+
Return the result table in any order.
25+
*/
26+
27+
SELECT
28+
player_id,
29+
MIN(event_date) AS first_login
30+
FROM Activity
31+
GROUP BY player_id

0 commit comments

Comments
 (0)