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 09af2c7

Browse files
Алексей ПоповАлексей Попов
Алексей Попов
authored and
Алексей Попов
committed
add dir with sql solutions
1 parent c2d026b commit 09af2c7

31 files changed

+460
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- link: https://leetcode.com/problems/customers-who-bought-all-products/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
SELECT
4+
customer_id
5+
FROM
6+
Customer
7+
GROUP BY
8+
customer_id
9+
HAVING
10+
COUNT(DISTINCT product_key) = (SELECT COUNT(DISTINCT product_key) FROM Product)
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
-- (sale_id, year) is the primary key (combination of columns with unique values) of this table.
2+
-- product_id is a foreign key (reference column) to Product table.
3+
-- Each row of this table shows a sale on the product product_id in a certain year.
4+
-- Note that the price is per unit.
5+
6+
-- link: https://leetcode.com/problems/product-sales-analysis-i/description/?envType=study-plan-v2&envId=top-sql-50
7+
8+
SELECT
9+
r.product_name AS product_name,
10+
l.year AS year,
11+
l.price AS price
12+
FROM
13+
Sales AS l
14+
INNER JOIN
15+
Product AS r
16+
ON
17+
l.product_id = r.product_id
18+
19+
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- link: https://leetcode.com/problems/product-sales-analysis-iii/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
SELECT
4+
l.product_id AS product_id,
5+
l.year AS first_year,
6+
l.quantity AS quantity,
7+
l.price AS price
8+
FROM
9+
Sales AS l
10+
INNER JOIN
11+
(
12+
SELECT
13+
product_id,
14+
MIN(year) AS first_year
15+
FROM
16+
Sales
17+
GROUP BY
18+
product_id
19+
) AS r
20+
ON
21+
l.product_id = r.product_id
22+
AND
23+
l.year = r.first_year
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
-- link: https://leetcode.com/problems/project-employees-i/submissions/1081391390/?envType=study-plan-v2&envId=top-sql-50
2+
3+
SELECT
4+
l.project_id AS project_id,
5+
ROUND(AVG(r.experience_years), 2) AS average_years
6+
FROM
7+
Project AS l
8+
INNER JOIN
9+
Employee AS r
10+
ON
11+
l.employee_id = r.employee_id
12+
GROUP BY
13+
l.project_id
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
-- link: https://leetcode.com/problems/user-activity-for-the-past-30-days-i/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
SELECT
4+
activity_date AS day,
5+
COUNT(DISTINCT user_id) AS active_users
6+
FROM
7+
Activity
8+
WHERE
9+
activity_date BETWEEN DATE_ADD('2019年07月27日', INTERVAL -19 DAY)
10+
AND '2019年07月17日'
11+
GROUP BY
12+
activity_date

‎sql_solutions/1148_article_views_I.sql

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- There is no primary key (column with unique values) for this table, the table may have duplicate rows.
2+
-- Each row of this table indicates that some viewer viewed an article (written by some author) on some date.
3+
-- Note that equal author_id and viewer_id indicate the same person.
4+
5+
-- link: https://leetcode.com/problems/article-views-i/description/?envType=study-plan-v2&envId=top-sql-50
6+
7+
SELECT
8+
DISTINCT author_id AS id
9+
FROM
10+
Views
11+
WHERE
12+
author_id = viewer_id
13+
ORDER BY
14+
id
15+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- link: https://leetcode.com/problems/immediate-food-delivery-ii/submissions/1082996119/?envType=study-plan-v2&envId=top-sql-50
2+
3+
SELECT
4+
ROUND(
5+
AVG(CASE WHEN order_date = customer_pref_delivery_date THEN 1 ELSE 0 END) * 100,
6+
2
7+
) AS immediate_percentage
8+
FROM
9+
Delivery
10+
WHERE
11+
(customer_id, order_date) IN (
12+
SELECT
13+
customer_id,
14+
MIN(order_date) AS first_order_date
15+
FROM
16+
Delivery
17+
GROUP BY
18+
customer_id)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- link: https://leetcode.com/problems/monthly-transactions-i/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
SELECT
4+
DATE_FORMAT(trans_date, '%Y-%m') AS month,
5+
country,
6+
COUNT(id) AS trans_count,
7+
SUM(CASE WHEN state = 'approved' THEN 1 ELSE 0 END) AS approved_count,
8+
SUM(amount) AS trans_total_amount,
9+
SUM(CASE WHEN state = 'approved' THEN amount ELSE 0 END) AS approved_total_amount
10+
FROM
11+
Transactions
12+
GROUP BY
13+
month,
14+
country
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
-- link: https://leetcode.com/problems/queries-quality-and-percentage/submissions/1081588590/?envType=study-plan-v2&envId=top-sql-50
2+
3+
SELECT
4+
query_name,
5+
ROUND(SUM(rating::DECIMAL / position) / COUNT(rating), 2) AS quality,
6+
ROUND(COUNT(rating) FILTER (WHERE rating < 3)::DECIMAL / COUNT(rating) * 100, 2) AS poor_query_percentage
7+
FROM
8+
Queries
9+
GROUP BY
10+
query_name
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
-- link: https://leetcode.com/problems/average-selling-price/description/?envType=study-plan-v2&envId=top-sql-50
2+
3+
SELECT
4+
l.product_id AS product_id,
5+
COALESCE(ROUND(SUM(l.price * r.units)::DECIMAL / SUM(r.units), 2), 0) AS average_price
6+
FROM
7+
Prices AS l
8+
LEFT JOIN
9+
UnitsSold AS r
10+
ON
11+
l.product_id = r.product_id
12+
AND
13+
r.purchase_date BETWEEN l.start_date AND l.end_date
14+
GROUP BY
15+
l.product_id

0 commit comments

Comments
(0)

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