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 |
Tags
- r/2
- between
- asc
- 오블완
- Where
- left join
- stratascratch
- desc
- %_%
- join
- CSV
- 중복 컬럼
- spa
- limit
- 공공데이터
- SAP HANA
- ERP
- SERVICE_KEY_IS_NOT_REGISTERED_ERROR
- union
- SQL
- json
- S/4 HANA
- over()
- R/3
- HAVING
- DateDiff
- group by
- 티스토리챌린지
- order by
- dense_rank()
Archives
- Today
- Total
RE:cord
[SQL] Finding the Nth Highest Values (JOIN/ORDER BY /LIMIT/DENSE_RANK () OVER) 본문
SQL
[SQL] Finding the Nth Highest Values (JOIN/ORDER BY /LIMIT/DENSE_RANK () OVER)
beie 2024. 11. 30. 18:53<1>


-- 1. join / 2. select
SELECT worker_title, salary
FROM worker JOIN title ON worker.worker_id=title.worker_ref_id
WHERE salary >= (SELECT MAX(salary) FROM worker)

<2>

-- using LIMIT
SELECT *
FROM worker
WHERE salary >= (
SELECT DISTINCT salary
FROM worker
ORDER BY salary DESC
LIMIT 4, 1);
-- using DENSE_RANK
SELECT first_name, salary, salary_rank
FROM (SELECT first_name,salary, DENSE_RANK() OVER (ORDER BY salary DESC) AS salary_rank
FROM worker) worker_order
WHERE salary_rank <= 5;

* LIMIT
- LIMIT n: Retrieves the first n rows.
- LIMIT offset, n: Skips the first offset rows and retrieves the next n rows.
- LIMIT cannot handle duplicate values effectively on its own, so it is often used in subqueries for better control.
'SQL' 카테고리의 다른 글
[SQL] Analyzing User Retention (유저 리텐션 분석) (DATEDIFF / BETWEEN AND / HAVING) (0) | 2024.12.04 |
---|---|
[SQL] Search for Data Containing Keywords (WHERE LIKE '%A%') (0) | 2024.12.03 |
[SQL] Count the number of group (GROUP BY / JOIN / LEFT JOIN / SUM) (2) | 2024.11.27 |
[SQL] Find the Nth group (DENSE_RANK / OVER / SUBQUERY) (0) | 2024.11.24 |
[SQL] AVG / GROUP BY / ORDER BY DESC (2) | 2024.11.22 |