SQL Advanced

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

1/25

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 2:32 PM on 7/29/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

26 Terms

1
New cards

WITH cte AS (…)

Common Table Expression — a named, reusable temporary result set scoped to one query. Improves readability

2
New cards

Recursive CTE

A CTE that references itself. Used for hierarchical data (org charts, trees)

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).
SELECT name, ________ OVER (ORDER BY salary) AS quartile FROM employees

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. ___(col) ____(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 ______ (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

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

25
New cards

Anchor Member

Base Case Query that starts the recursion in a recursive Common Table Expression (CTE).
WITH RECURSIVE cte_name AS ( ANCHOR_MEMBER_QUERY UNION ALL RECURSIVE_MEMBER_QUERY )

26
New cards

Recursive Member

References the CTE itself, executes on previous result set, starting with the result of the Anchor Member. Ends when stopping condition is met (such as an empty result set)

WITH RECURSIVE cte_name AS ( ANCHOR_MEMBER_QUERY UNION ALL RECURSIVE_MEMBER_QUERY )