Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
Tags
- LOWER
- Python
- map
- SQL
- MAX
- 파이썬
- len
- split
- count
- and
- sum
- 위코드
- js
- iNT
- Algorithm
- WECODE
- FOR
- or
- list
- index
- STR
- enumerate
- decorator
- If
- DART
- range
- Sorted
- lambda
- slice
- join
Archives
- Today
- Total
코드로 우주평화
SQL Basics: Simple JOIN and RANK 본문
For this challenge you need to create a simple SELECT statement that will return all columns from the people table, and join to the sales table so that you can return the COUNT of all sales and RANK each person by their sale_count.
people table schema
- id
- name
sales table schema
- id
- people_id
- sale
- price
You should return all people fields as well as the sale count as "sale_count" and the rank as "sale_rank".
Solution:
SELECT
p.id,
p.name,
COUNT(s.sale) AS sale_count,
RANK () OVER (ORDER BY COUNT(s.sale)) AS sale_rank
FROM
people p
JOIN sales s ON s.people_id = p.id
GROUP BY p.id
Result:
id | name | sale_count | sale_rank |
10 | Roma Walker III | 5 | 1 |
7 | Miss Aubrey Bode | 6 | 2 |
9 | Forest Romaguera | 9 | 3 |
5 | Lia Towne MD | 9 | 3 |
8 | Muriel Ward | 10 | 5 |
3 | Mariam Maggio III | 11 | 6 |
2 | Hiram Shields | 12 | 7 |
1 | Anastacio Macejkovic | 12 | 7 |
4 | Trycia Kulas | 13 | 9 |
6 | Ms. Vincenza Ratke | 13 | 9 |
'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글
SQL Basics - Monsters using CASE (0) | 2022.07.12 |
---|---|
SQL Basics: Simple NULL handling (0) | 2022.07.11 |
SQL Basics: Simple EXISTS (0) | 2022.07.09 |
GROCERY STORE: Real Price! (0) | 2022.07.08 |
GROCERY STORE: Support Local Products (0) | 2022.07.07 |