σ_p(r) Select — filter rows satisfying predicate p. Connectives: ∧ ∨ ¬π_{A1,…}(r) Project — keep only listed columns; duplicates are removedr ∪ s Union — combine; relations must have same arity & compatible domainsr − s Set difference — tuples in r but not s (same arity/compatibility)r × s Cartesian product — all pairs of tuples from r and sρ_x(E) Rename — gives expression E the name xSELECT A1, A2 FROM r1, r2 WHERE P — project → Cartesian product → filterSELECT DISTINCT removes duplicates; SELECT ALL (default) keeps themSELECT * returns all attributes; arithmetic expressions allowed: salary/12 AS monthly_salary< <= > >= = <>, AND OR NOT, BETWEEN x AND y% = any substring, _ = any single character. Case sensitive.ORDER BY attr DESC/ASC; can sort on multiple attributesUNION, INTERSECT, EXCEPT — eliminate duplicates automatically. Use ALL suffix to keep.IS NULL / IS NOT NULL to test.null = null evaluates to unknown, not truetrue AND unknown = unknown; false AND unknown = false; true OR unknown = trueavg, min, max, sum, countGROUP BY attr — groups rows; every non-aggregate in SELECT must be in GROUP BYHAVING condition — filters groups (applied after grouping; WHERE filters before)IN / NOT IN (subquery) — set membership test> SOME (subquery) — true if greater than at least one value (= SOME ≡ IN)> ALL (subquery) — true if greater than every value (≠ ALL ≡ NOT IN)EXISTS (subquery) — true if subquery is non-empty; used for correlated subqueriesNOT EXISTS — "find students who took ALL biology courses" pattern: double-except trickINSERT INTO r VALUES (...) or INSERT INTO r SELECT ...DELETE FROM r WHERE P — avg/subquery computed first, then all matching rows deleted at onceUPDATE r SET attr = expr WHERE P — use CASE WHEN ... THEN ... ELSE ... END for conditional updatesstudent natural join takes natural join course silently drops students in depts different from their course deptCREATE VIEW v AS <query> — saves the query expression, not a copy of dataCOMMIT WORK — makes changes permanent; ROLLBACK WORK — undoes all changesNOT NULL, PRIMARY KEY, UNIQUE (candidate key; can be null), CHECK (predicate)ON DELETE CASCADE / ON UPDATE CASCADE; alternatives: SET NULL, SET DEFAULTdate, time, timestamp, interval, blob (binary), clob (character large object)CREATE TYPE Dollars AS numeric(12,2); CREATE DOMAIN (can have constraints)CREATE INDEX idx ON table(attr) — speeds up lookups; not part of SQL standard but universally supportedSELECT, INSERT, UPDATE, DELETE — granted with GRANT priv ON rel TO user, revoked with REVOKECREATE ROLE r; GRANT r TO user; — roles inherit privileges and can be granted to other rolesWITH GRANT OPTION — lets the grantee re-grant the privilege; CASCADE vs RESTRICT on revokeDriverManager.getConnection(...) → Statement → executeQuery/executeUpdate → ResultSet? placeholders + setString/setInt. Always use for user input — prevents SQL injection.X' OR 'Y'='Y makes the WHERE always trueEXEC SQL <stmt>; in host language (C, Java, etc.); host vars prefixed with :DECLARE c CURSOR FOR ... ; OPEN c; FETCH c INTO :var; CLOSE c;CREATE FUNCTION f(arg type) RETURNS type BEGIN ... RETURN val; ENDSELECT * FROM TABLE(f('arg'))CREATE PROCEDURE p(IN x type, OUT y type) BEGIN ... END; called with CALL p(val, var)WHILE ... DO ... END WHILE; REPEAT ... UNTIL ... END REPEAT; FOR r AS SELECT ... DO ... END FOR; IF ... THEN ... ELSEIF ... ELSE ... END IFCREATE TRIGGER name BEFORE|AFTER INSERT|UPDATE|DELETE ON table FOR EACH ROW BEGIN ... ENDREFERENCING OLD ROW AS orow NEW ROW AS nrow — access values before/after changeFOR EACH STATEMENT — one execution per SQL statement (more efficient for bulk updates)WITH RECURSIVE rec(cols) AS (base_query UNION recursive_query) SELECT * FROM rec;rank() OVER (ORDER BY GPA DESC) — ranks with gaps (ties both get 1, next is 3)dense_rank() — no gaps (ties both get 1, next is 2)PARTITION BY dept OVER (...) — rank within each group independentlyntile(n) — divide rows into n equal buckets; percent_rank; cume_dist; row_numbersum(val) OVER (ORDER BY date ROWS BETWEEN 1 PRECEDING AND 1 FOLLOWING) — sliding window aggregates (e.g., moving average)GROUP BY CUBE(a,b,c) — all 2ⁿ subsets of attributes (every combination of groupings)GROUP BY ROLLUP(a,b,c) — hierarchical prefixes only: (a,b,c), (a,b), (a), ()