A · Compressed recap
DDL: PRIMARY KEY = UNIQUE + NOT NULL; UNIQUE allows many NULLs; CHECK rejects only FALSE; FK actions default to NO ACTION. DML: UPDATE uses old values (swap works); DELETE keeps the table, DROP removes it. SELECT: bags; order FROM → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY; BETWEEN inclusive; LIKE % and _. NULL: F wins AND, T wins OR, NOT U = U; WHERE keeps TRUE; IS NULL; NOT IN with NULL returns nothing. Aggregates: COUNT(*) counts rows, others skip NULL; empty → COUNT 0, others NULL.
Joins: NATURAL JOIN uses all common names; ON vs WHERE in outer joins; count the right table's column. Set ops: plain = sets; ALL: m + n, min, max(0, m − n). Subqueries: > ALL ∅ TRUE, > ANY ∅ FALSE, = ANY ≡ IN, <> ALL ≡ NOT IN; NULL blocks NOT IN/ALL; EXISTS never UNKNOWN; for all = double NOT EXISTS. Views store queries; WITH is per statement.
B · Merged rule card
3.3.1 SQL DDL: CREATE TABLE, data types, constraints, ON DELETE/ON UPDATE
- CREATE TABLE with types (INT, DECIMAL(p,s), CHAR(n), VARCHAR(n), DATE …).
- PRIMARY KEY = UNIQUE + NOT NULL, one per table; UNIQUE allows many NULLs.
- FOREIGN KEY … REFERENCES … ON DELETE/UPDATE CASCADE | SET NULL | SET DEFAULT | RESTRICT | NO ACTION (default).
- CHECK rejects only FALSE; DEFAULT fills omitted columns.
- ALTER TABLE ADD/DROP column/constraint; DROP TABLE removes schema + rows.
3.3.2 SQL DML: INSERT, UPDATE, DELETE
- INSERT INTO T [(cols)] VALUES (…) | SELECT …; missing columns get DEFAULT/NULL.
- UPDATE T SET … [WHERE …]: right-hand sides use old values; all qualifying rows change together.
- DELETE FROM T [WHERE …]: rows only; FK actions fire.
- No WHERE = all rows. DROP removes schema; DELETE does not.
3.3.3 SELECT–FROM–WHERE, DISTINCT, aliases, LIKE, BETWEEN, ORDER BY; bags vs sets
- SELECT [DISTINCT] list FROM tables WHERE cond ORDER BY … ≈ π(σ(×)) with bags.
- Order: FROM → WHERE → SELECT → DISTINCT → ORDER BY.
- SQL keeps duplicates; DISTINCT removes them.
- BETWEEN inclusive; IN list; LIKE with % (any, incl. empty) and _ (one char).
- Aliases needed for self-joins; ORDER BY ASC default, applied last.
3.3.4 NULL in SQL: three-valued logic, IS NULL, comparisons
- AND: F dominates; OR: T dominates; else U. NOT U = U.
- WHERE/HAVING keep only TRUE; CHECK rejects only FALSE.
- Test NULL with IS NULL / IS NOT NULL;
= NULLis always UNKNOWN. - x NOT IN (…, NULL) is never TRUE.
- DISTINCT/GROUP BY treat NULLs as one value; ORDER BY NULL position is system-dependent.
3.3.5 Aggregates, GROUP BY, HAVING; COUNT(*) vs COUNT(col); NULL handling
- COUNT(*) counts rows; COUNT(col), SUM, AVG, MIN, MAX ignore NULL; AVG = SUM/COUNT(col).
- Empty input: COUNT 0, others NULL; no GROUP BY → exactly one row.
- GROUP BY: one row per group; SELECT only grouping columns + aggregates; NULLs form one group.
- WHERE filters rows (no aggregates); HAVING filters groups.
- Order: FROM → WHERE → GROUP BY → HAVING → SELECT → DISTINCT → ORDER BY.
3.3.6 Joins in SQL: INNER, NATURAL, LEFT/RIGHT/FULL OUTER
- JOIN … ON (inner), USING (cols), NATURAL JOIN (all common names), LEFT/RIGHT/FULL OUTER JOIN, CROSS JOIN.
- ON conditions decide matching (outer rows still kept); WHERE filters afterwards (drops padded rows).
- After LEFT JOIN, count with COUNT(right.col).
- "No match": LEFT JOIN … WHERE right.key IS NULL.
3.3.7 UNION / INTERSECT / EXCEPT and their ALL variants
- UNION / INTERSECT / EXCEPT: set semantics (duplicates removed); need union compatibility.
- ALL: UNION m + n; INTERSECT min(m, n); EXCEPT max(0, m − n).
- Set operators treat NULL rows as equal to each other.
- Oracle's MINUS = EXCEPT.
3.3.8 Nested subqueries: IN, ANY/SOME, ALL, EXISTS, scalar subqueries
- IN: equals some value; NOT IN: differs from all, and no NULL present.
- op ANY: true for some value (empty → FALSE); op ALL: true for every value (empty → TRUE).
- > ANY ≈ > MIN; > ALL ≈ > MAX; = ANY ≡ IN; <> ALL ≡ NOT IN.
- NULL in the subquery: NOT IN and ALL can never be TRUE.
- EXISTS: at least one row; never UNKNOWN.
- Scalar subquery: one value; many rows → error; none → NULL.
3.3.9 Correlated subqueries; for-all in SQL; tracing a query on a table instance
- Correlated subquery references an outer column; evaluated per outer row.
- Some: EXISTS; none: NOT EXISTS; all: NOT EXISTS (… AND NOT EXISTS (…)).
- COUNT alternative: HAVING COUNT(DISTINCT x) = (SELECT COUNT(*) …); differs when the "all" set is empty.
- Trace with a table: outer row | inner result | condition | kept.
- NULL rows: comparisons inside COUNT give 0 matches.
3.3.10 Views and WITH
- View = stored query (virtual table); always current; used for security, simplicity, logical independence.
- Materialised view = stored rows, needs refresh.
- Updatable views: one table, no aggregates/GROUP BY/DISTINCT, key + required columns present.
- WITH CHECK OPTION: changes must keep rows visible.
- WITH name AS (…): temporary named result for one statement.
C · Interleaved GATE-style questions
Q1 · NAT · 1 mark(s) — Aggregates
SELECT COUNT(DISTINCT dept) FROM STUDENT; (5-row STUDENT)
Numerical answer: ________
Q2 · NAT · 2 mark(s) — Group by
How many rows: SELECT roll FROM ENROLL GROUP BY roll HAVING COUNT(*) >= 2;
Numerical answer: ________
Q3 · MCQ · 2 mark(s) — Three-valued logic
SELECT name FROM STUDENT WHERE cgpa < 7 OR cgpa >= 7; returns:
- (A) only Diya
- (B) the 4 students with a CGPA
- (C) no one
- (D) all 5 students
Q4 · NAT · 2 mark(s) — Outer join count
SELECT COUNT(E.grade) FROM STUDENT S LEFT JOIN ENROLL E ON S.roll = E.roll;
Numerical answer: ________
Q5 · NAT · 2 mark(s) — Set ops
How many rows: SELECT cid FROM ENROLL WHERE grade = 'A' UNION ALL SELECT cid FROM ENROLL WHERE grade = 'B';
Numerical answer: ________
Q6 · MCQ · 2 mark(s) — Subqueries
SELECT name FROM STUDENT WHERE cgpa > ALL (SELECT cgpa FROM STUDENT WHERE dept = 'ME'); returns:
- (A) Asha, Bhavin, Chirag
- (B) no rows
- (C) all 5
- (D) Asha, Chirag
Q7 · NAT · 2 mark(s) — Correlated
How many students satisfy EXISTS (SELECT * FROM ENROLL E JOIN COURSE C ON E.cid = C.cid WHERE E.roll = S.roll AND C.dept <> S.dept)?
Numerical answer: ________
Q8 · MCQ · 1 mark(s) — DDL
CHECK (credits BETWEEN 1 AND 5). Which insert is rejected?
- (A) credits = 5
- (B) credits = NULL
- (C) credits = 1
- (D) credits = 6
Q9 · MCQ · 1 mark(s) — DML
UPDATE COURSE SET credits = credits + 1; with no WHERE:
- (A) updates no rows
- (B) is an error
- (C) updates only the first row
- (D) updates every row
Q10 · MSQ · 2 mark(s) — Views
Which are true?
- (A) A materialised view stores rows.
- (B) A view with GROUP BY is usually updatable.
- (C) A view shows current base data.
- (D) WITH CHECK OPTION blocks changes that hide rows from the view.
D · Answer Vault
Reveal answers when you’re ready
Q1 Answer: 3 · 1 mark(s)
CS, EE, ME.
Re-read: Lesson 3.3.5 — Aggregates, GROUP BY, HAVING; COUNT(*) vs COUNT(col); NULL handling
Q2 Answer: 2 · 2 mark(s)
Roll 1 (3), roll 3 (2).
Re-read: Lesson 3.3.5 — Aggregates, GROUP BY, HAVING; COUNT(*) vs COUNT(col); NULL handling
Q3 Answer: (B) · 2 mark(s)
Esha: U OR U = U.
- (A) is wrong: Too few.
- (C) is wrong: Too few.
- (D) is wrong: Esha is dropped.
Re-read: Lesson 3.3.4 — NULL in SQL: three-valued logic, IS NULL, comparisons
Q4 Answer: 7 · 2 mark(s)
8 rows, Esha's grade is NULL.
Re-read: Lesson 3.3.6 — Joins in SQL: INNER, NATURAL, LEFT/RIGHT/FULL OUTER
Q5 Answer: 6 · 2 mark(s)
3 + 3.
Re-read: Lesson 3.3.7 — UNION / INTERSECT / EXCEPT and their ALL variants
Q6 Answer: (A) · 2 mark(s)
ME CGPAs {6.9}; cgpa > 6.9; Esha is UNKNOWN.
- (B) is wrong: The ME set has no NULL.
- (C) is wrong: Diya and Esha fail.
- (D) is wrong: Bhavin's 7.8 > 6.9.
Re-read: Lesson 3.3.8 — Nested subqueries: IN, ANY/SOME, ALL, EXISTS, scalar subqueries
Q7 Answer: 1 · 2 mark(s)
Only Asha (took EE's Circuits).
Re-read: Lesson 3.3.9 — Correlated subqueries; for-all in SQL; tracing a query on a table instance
Q8 Answer: (D) · 1 mark(s)
FALSE.
- (A) is wrong: Inclusive.
- (B) is wrong: UNKNOWN passes.
- (C) is wrong: Inclusive.
Re-read: Lesson 3.3.1 — SQL DDL: CREATE TABLE, data types, constraints, ON DELETE/ON UPDATE
Q9 Answer: (D) · 1 mark(s)
No WHERE = all rows.
- (A) is wrong: No.
- (B) is wrong: Valid.
- (C) is wrong: No.
Re-read: Lesson 3.3.2 — SQL DML: INSERT, UPDATE, DELETE
Q10 Answer: (A) (C) (D) · 2 mark(s)
GROUP BY views are not updatable.
- (B) is wrong: Not updatable.
Re-read: Lesson 3.3.10 — Views and WITH