Skip to content

Commit 51bba25

Browse files
committed
Section 05 : Advanced Select and Joins - part 02 - Done
1 parent f3dbfbc commit 51bba25

File tree

3 files changed

+63
-0
lines changed

3 files changed

+63
-0
lines changed
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
SELECT
2+
product_id,
3+
10 AS price
4+
FROM
5+
Products
6+
GROUP BY
7+
product_id
8+
HAVING
9+
MIN(change_date) > '2019-08-16'
10+
UNION ALL
11+
SELECT
12+
product_id,
13+
new_price AS price
14+
FROM
15+
Products
16+
WHERE
17+
(product_id, change_date) IN (
18+
SELECT
19+
product_id,
20+
MAX(change_date)
21+
FROM
22+
Products
23+
WHERE
24+
change_date <= '2019-08-16'
25+
GROUP BY
26+
product_id
27+
)
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
SELECT person_name
2+
FROM
3+
(
4+
SELECT person_name, SUM(weight) OVER(ORDER BY turn) as com_weight
5+
FROM queue
6+
) alias
7+
WHERE com_weight <= 1000
8+
ORDER BY com_weight DESC
9+
LIMIT 1
10+
11+
12+
13+
-- Better soltion:
14+
SELECT person_name
15+
FROM (
16+
SELECT person_name,
17+
weight,
18+
turn,
19+
SUM(weight) OVER (ORDER BY turn) AS total_weight
20+
FROM Queue
21+
) AS cumulative
22+
WHERE total_weight <= 1000
23+
ORDER BY turn DESC
24+
LIMIT 1;
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
SELECT "Low Salary" AS Category, SUM(income < 20000) AS accounts_count
3+
FROM Accounts
4+
UNION
5+
SELECT "Average Salary" Category, SUM(income >= 20000 AND income <= 50000) AS accounts_count
6+
FROM Accounts
7+
UNION
8+
SELECT "High Salary" category, SUM(income > 50000) AS accounts_count
9+
FROM Accounts
10+
11+
12+

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy