实现说明
实际执行的是 SELECT——scores 是涉及的对象。
真正干活的子句:ORDER BY 固定了输出顺序;LIMIT 限制返回行数。
常见问题
这段 SQL 涉及哪些关系?
scores。
核心语句
SELECT player, points
FROM scores
ORDER BY points DESC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS WITH TIES;
SELECT player, points
FROM scores
ORDER BY points DESC
FETCH FIRST 3 ROWS WITH TIES;
SELECT player, points
FROM scores
ORDER BY points DESC
LIMIT 3;
代码示例
-- Top 3, plus anyone tied with the third place
SELECT player, points
FROM scores
ORDER BY points DESC
FETCH FIRST 3 ROWS WITH TIES;
-- Contrast: a bare LIMIT cuts tied rows off arbitrarily
SELECT player, points
FROM scores
ORDER BY points DESC
LIMIT 3;
-- Skip the winner, then fetch second place along with its ties
SELECT player, points
FROM scores
ORDER BY points DESC
OFFSET 1 ROWS
FETCH NEXT 1 ROWS WITH TIES;