0 · Recall Warm-up
Answer from memory first. Answers are in the Answer Vault at the bottom.
- How is "for all" written in SQL? from Lesson 3.3.9
- What is an external schema in the three-schema architecture? from Lesson 0.1.3
- Which clause filters groups? from Lesson 3.3.5
1 · The Hook
The exam cell should see only names and CGPAs, never phone numbers. A long query is used in ten reports. In both cases you want to give a query a name and use it like a table. SQL offers views (stored, reusable) and WITH (temporary, inside one query).
2 · Predict First
A view CS_STUDENTS is defined as "SELECT roll, name FROM STUDENT WHERE dept = 'CS'". A new CS student is inserted into STUDENT. Does the view show her?
Make a guess in your head. The answer opens Section 3.
3 · Core Concept
Predict First — the answer
Yes. A (normal) view stores the query, not the rows. Every time the view is used, the query runs on the current data.
Views
CREATE VIEW CS_STUDENTS AS
SELECT roll, name, cgpa FROM STUDENT WHERE dept = 'CS';
SELECT name FROM CS_STUDENTS WHERE cgpa > 9; -- used like a table: Asha
DROP VIEW CS_STUDENTS;
- A view is a virtual table: only its definition is stored in the catalog.
- Uses: security (show only some columns or rows), simplicity (hide joins), logical data independence (Lesson 0.1.3: external schemas are views).
- A materialised view stores the result physically and must be refreshed when base tables change. It is faster to read but can be stale.
Updating through a view
An INSERT, UPDATE or DELETE on a view must be translated into a change of base tables. Standard SQL allows it only for simple views, typically:
- defined on one base table,
- no aggregates, GROUP BY, HAVING or DISTINCT,
- the select list includes enough columns (the key and all NOT NULL columns without defaults) to build a base row.
A view with a join or with AVG cannot be updated: AVG(cgpa) = 9 has no single meaning in base rows.
WITH CHECK OPTION: rejects inserts or updates through the view that would produce rows the view cannot see (inserting a non-CS student through CS_STUDENTS).
WITH (common table expressions)
WITH dept_avg AS (
SELECT dept, AVG(cgpa) AS a FROM STUDENT GROUP BY dept
)
SELECT S.name FROM STUDENT S JOIN dept_avg D ON S.dept = D.dept
WHERE S.cgpa > D.a; -- Asha
A WITH table exists only for that one query. It makes long queries readable and replaces repeated subqueries.
4 · Visual
CREATE VIEW CS_STUDENTS AS SELECT roll, name, cgpa FROM STUDENT WHERE dept='CS'
catalog stores: the query text (normal view: no rows stored)
on each use: query runs on STUDENT -> rows of the moment
materialised view: rows stored, refresh needed
WITH x AS (...): name lives only inside one statement
5 · Worked Examples
Example 1 Easy
Is a view's data stored on disk?
Solution
For a normal view, no: only its definition. A materialised view does store its rows.
Example 2 Medium
Which view is updatable? V1 = SELECT roll, name FROM STUDENT WHERE cgpa > 8; V2 = SELECT dept, COUNT(*) FROM STUDENT GROUP BY dept.
Solution
V1 (one table, no aggregation, includes the key). V2 uses GROUP BY and COUNT: not updatable.
Example 3 GATE
Through CS_STUDENTS (defined WITH CHECK OPTION) someone runs INSERT INTO CS_STUDENTS VALUES (9, 'Kunal', 8.0). What dept does the base row get, and is the insert accepted?
Solution
dept is not in the view, so the base row gets dept = NULL (or its default). The new row has dept ≠ 'CS', so the view could not see it: WITH CHECK OPTION rejects it. Without the option it would be accepted and silently invisible through the view.
6 · GATE Traps
- A normal view stores the query, not the data; it always shows current data.
- Views with joins, aggregates, GROUP BY or DISTINCT are generally not updatable.
- WITH CHECK OPTION blocks changes that move rows out of the view.
- A WITH table lasts for one statement only.
7 · Memory Hook
"A view is a window, not a photo." (A materialised view is the photo.)
8 · Connect the Dots
Chapter 3.3 is complete: DDL, DML, SELECT, NULLs, aggregates, joins, set operators, subqueries, views. Next: the Chapter 3.3 Lock-in, then one drill lesson writing the same query in RA, TRC and SQL.
9 · Self-Test
GATE format. Try every question before opening the vault.
Q1 · MCQ · 1 mark(s) — Views
A normal (non-materialised) view stores:
- (A) an index on its base table
- (B) its defining query
- (C) a copy of its rows
- (D) nothing at all
Q2 · MCQ · 1 mark(s) — Updatability
Which view is generally not updatable?
- (A) SELECT cid, title FROM COURSE
- (B) SELECT roll, name FROM STUDENT
- (C) SELECT dept, AVG(cgpa) FROM STUDENT GROUP BY dept
- (D) SELECT * FROM COURSE WHERE credits = 4
Q3 · MCQ · 1 mark(s) — CHECK OPTION
WITH CHECK OPTION on a view:
- (A) refreshes a materialised view
- (B) rejects changes through the view that create rows the view would not show
- (C) makes the view read-only
- (D) checks the base table's CHECK constraints only
Q4 · MSQ · 2 mark(s) — WITH
Which are true of WITH (common table expressions)?
- (A) It can be joined with base tables in the main query.
- (B) It permanently creates a table.
- (C) The named result exists only for one statement.
- (D) It can make a long query more readable.
Q5 · NAT · 1 mark(s) — View query
CS_STUDENTS = SELECT roll, name, cgpa FROM STUDENT WHERE dept = 'CS'. How many rows does SELECT * FROM CS_STUDENTS return on the 5-row STUDENT table?
Numerical answer: ________
10 · Rule Card
- 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.
11 · Concept Ledger
Unlocked in this lesson. From here on, these terms may be used.
view (virtual table)CREATE VIEWDROP VIEWmaterialised viewupdatable viewWITH CHECK OPTIONWITH (common table expression)
12 · Answer Vault
Reveal answers when you’re ready
Recall Warm-up
- NOT EXISTS (… AND NOT EXISTS (…)).
- A view: the part of the database one user group needs.
- HAVING.
Self-Test
Q1 Answer: (B) · 1 mark(s)
Definition only.
- (A) is wrong: No.
- (C) is wrong: That is a materialised view.
- (D) is wrong: The definition is stored in the catalog.
Re-read: Lesson 3.3.10 — Views and WITH
Q2 Answer: (C) · 1 mark(s)
Aggregation.
- (A) is wrong: Simple single-table view.
- (B) is wrong: Simple single-table view.
- (D) is wrong: Simple single-table view.
Re-read: Lesson 3.3.10 — Views and WITH
Q3 Answer: (B) · 1 mark(s)
Rows must remain visible.
- (A) is wrong: No.
- (C) is wrong: Changes are still allowed.
- (D) is wrong: More than that.
Re-read: Lesson 3.3.10 — Views and WITH
Q4 Answer: (A) (C) (D) · 2 mark(s)
Temporary and joinable.
- (B) is wrong: Not permanent.
Re-read: Lesson 3.3.10 — Views and WITH
Q5 Answer: 3 · 1 mark(s)
Asha, Chirag, Esha.
Re-read: Lesson 3.3.10 — Views and WITH