SQL & Databases·Practice drills 4

Drill 7: Recall — Views and materialized views

Close the lesson and reproduce the example from memory.

Beginner 5 min read Lesson 116 of 480 Challenge included
What you will be able to do
  • Close the lesson and reproduce the example from memory.

Practice task

Close the lesson and reproduce the example from memory.

views.sqlSQL
CREATE TABLE products (
  id SERIAL PRIMARY KEY,
  name TEXT NOT NULL,
  price NUMERIC(10, 2) NOT NULL,
  stock INTEGER NOT NULL DEFAULT 0
);

CREATE VIEW low_stock AS
SELECT id, name, stock
FROM products
WHERE stock < 5;

CREATE MATERIALIZED VIEW product_count_by_name AS
SELECT name, COUNT(*) AS copies
FROM products
GROUP BY name;

REFRESH MATERIALIZED VIEW product_count_by_name;
Your turn

Write your answer

Easy

Close the lesson and reproduce the example from memory.

TEXT · starter
Your answer…
Study tools

Make it yours.

Keep a private note for this lesson, then turn the key ideas into flashcards you can revisit.

Sign in to save notes and flashcards.Both are private to your account and stay with your progress.
Sign in
Finished reading?

Lock in what you just learned.

Mark this lesson complete to save your place and move one step closer to the certificate.

Lesson 116 of 480 · 0% of course