Views - saved queries#
A view is a named query. You select from it like a table, but it runs its query each time, so it always shows current data. Views hide complex joins, give users a simplified (or restricted) picture of the data, and keep reports consistent.
A normal view stores no data. PostgreSQL and Oracle also have materialized views, which store the result for speed and must be refreshed.
Indexes - making lookups fast#
Without an index, finding WHERE email = '...' means scanning every row (like reading a whole book to find one word). An index is a sorted structure (usually a B-tree) that lets the database jump straight to matching rows - like the index at the back of a book.
The first plan says SCAN students (read everything); after the index it says SEARCH students USING INDEX idx_students_city. With 30 rows you will not notice; with 30 million rows it is the difference between minutes and milliseconds.
| Index on... | Good idea when |
|---|---|
| primary keys | always (created automatically) |
| foreign keys | joins use them constantly |
columns in frequent WHERE, ORDER BY, JOIN |
e.g. email, city, created_at |
UNIQUE columns |
created automatically to enforce uniqueness |
Indexes are not free: each one uses storage and slows down INSERT, UPDATE and DELETE, because the index must be updated too. Add them for real query patterns, not on every column.
Index किताब के पीछे वाली विषय-सूची (index) जैसा है - पूरा data पढ़े बिना सीधे सही जगह पहुँचा देता है। जिन columns पर बार-बार search, join या sort होता है, उन पर index बनाइए। पर हर column पर index बनाना ठीक नहीं - insert/update धीमे हो जाते हैं।
Window functions#
A window function computes a value over a set of rows related to the current row - without collapsing them like GROUP BY does. The syntax is function() OVER (PARTITION BY ... ORDER BY ...).
Ranking: ROW_NUMBER, RANK, DENSE_RANK#
Nisha and Aditi both have 9.47: RANK gives both 1 and skips 2; DENSE_RANK does not skip; ROW_NUMBER always counts 1, 2, 3...
PARTITION BY - restart the calculation for each group#
"Top N per group" is one of the most asked SQL interview questions - and this CTE + ROW_NUMBER() pattern is the standard answer.
Aggregates as window functions#
OVER () with nothing inside means "the whole result".
Running totals and moving averages#
LAG and LEAD - compare with the previous or next row#
| Function | Gives |
|---|---|
ROW_NUMBER() |
1, 2, 3... (no ties) |
RANK() |
ties share a rank, then a gap |
DENSE_RANK() |
ties share a rank, no gap |
NTILE(n) |
splits rows into n buckets (quartiles...) |
SUM/AVG/COUNT(...) OVER |
group totals or running totals on every row |
LAG(col) / LEAD(col) |
value from the previous / next row |
FIRST_VALUE / LAST_VALUE |
first / last value in the window |
Summary#
- Views save queries under a name; they simplify and secure access.
- Indexes speed up WHERE/JOIN/ORDER BY on the indexed columns; check plans with
EXPLAIN. - Window functions (
... OVER (PARTITION BY ... ORDER BY ...)) rank, total and compare rows while keeping every row. - "Top N per group" = CTE +
ROW_NUMBER() OVER (PARTITION BY ...).