// add row number
SELECT *, ROW_NUMBER() OVER () AS row_number
FROM enrolled
SELECT cid, sid, ROW_NUMBER() OVER (PARTITION BY cid) AS row_number
FROM enrolled
ORDER BY cid
We can also include an ORDER BY in the window grouping to sort entries in each group
SELECT *, ROW_NUMBER() OVER (ORDER BY cid)
FROM enrolled
ORDER BY cid
Find the student with the highest grade for each course.
SELECT * FROM (
SELECT *,
RANK() OVER (PARTITION BY cid
ORDER BY grade ASC)
AS RANK
FROM enrolled) AS ranking
WHERE ranking.rank = 1
WITH cteName (col1, col2) AS (SELECT 1,2)
SELECT col1 + col2 FROM cteName
WITH RECURSIVE cteSource (counter) AS (
(SELECT 1)
UNION ALL
(SELECT counter +1 FROM cteSource
WHERE counter < 10)
)
SELECT * FROM cteSource