SQL Advanced

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/23

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 10:53 PM on 6/1/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai

No analytics yet

Send a link to your students to track their progress

24 Terms

1
New cards

WITH cte AS (…)

Common Table Expression — a named, reusable temporary result set scoped to one query. Improves readability. WITH cte AS (SELECT …) SELECT * FROM cte

2
New cards

Recursive CTE

A CTE that references itself. Used for hierarchical data (org charts, trees). WITH RECURSIVE cte AS (anchor UNION ALL recursive_part) SELECT * FROM cte

3
New cards

ROW_NUMBER()

Assigns a unique sequential integer to each row within a partition. No ties — always unique.

4
New cards

RANK()

Assigns rank within a partition. Ties get the same rank; the next rank skips (1,1,3,4).

5
New cards

DENSE_RANK()

Like RANK() but no gaps after ties (1,1,2,3). Use when you don't want rank gaps.

6
New cards

NTILE(n)

Divides rows into n roughly equal buckets and assigns each row a bucket number (1 to n).

7
New cards

LAG(col, n, default)

Returns the value of col from n rows BEFORE the current row. Default if no prior row.

8
New cards

LEAD(col, n, default)

Returns the value of col from n rows AFTER the current row. Default if no next row.

9
New cards

FIRST_VALUE(col)

Returns the first value of col in the window frame.

10
New cards

LAST_VALUE(col)

Returns the last value of col in the current window frame. Frame defaults to current row — add ROWS BETWEEN UNBOUNDED PRECEDING AND UNBOUNDED FOLLOWING to get true last.

11
New cards

OVER(PARTITION BY … ORDER BY …)

Defines the window for a window function. PARTITION BY = groups (like GROUP BY). ORDER BY = sort within the group. Can be used without PARTITION BY for global window.

12
New cards

SUM() OVER()

Running or partitioned total without collapsing rows. Rows are preserved. SUM(col) OVER(PARTITION BY grp ORDER BY date)

13
New cards

Window Frame: ROWS BETWEEN

Controls which rows are included in the window. ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW = running total. ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING = 3-row rolling window.

14
New cards

EXISTS

Returns TRUE if the subquery returns at least one row. SELECT * FROM t WHERE EXISTS (SELECT 1 FROM t2 WHERE t2.id = t.id)

15
New cards

NOT EXISTS

Returns TRUE if the subquery returns no rows. Often more efficient than NOT IN with NULLs.

16
New cards

STRING_AGG(col, sep)

Concatenates values from multiple rows into one string with a separator. (PostgreSQL / SQL Server 2017+). STRING_AGG(name, ', ')

17
New cards

GROUP_CONCAT(col)

MySQL equivalent of STRING_AGG. GROUP_CONCAT(col SEPARATOR ', ')

18
New cards

INTERSECT

Returns only rows that appear in BOTH query results.

19
New cards

EXCEPT / MINUS

Returns rows in the first result that do NOT appear in the second. (MINUS in Oracle)

20
New cards

PIVOT

Transforms distinct row values into columns. Syntax varies by database. Often done with CASE WHEN + GROUP BY for portability.

21
New cards

INDEX

A data structure that speeds up lookups at the cost of write overhead. CREATE INDEX idx ON table(col). Use on columns frequently used in WHERE, JOIN ON, ORDER BY.

22
New cards

EXPLAIN / QUERY PLAN

Shows how the database will execute a query — scans, joins, index usage. Use to identify slow operations.

23
New cards

Correlated Subquery

A subquery that references columns from the outer query. Runs once per row — can be slow. Often replaceable with a JOIN.

24
New cards

SELF JOIN

A table joined to itself. Requires aliases. SELECT a.emp, b.manager FROM emp a JOIN emp b ON a.mgr_id = b.emp_id