SQL & Databases·Practice drills 6

Drill 9: Make it smaller — Views and materialized views

Write a smaller version of the example that keeps the core idea.

Beginner 5 min read Lesson 148 of 480 Challenge included
What you will be able to do
  • Write a smaller version of the example that keeps the core idea.

Practice task

Write a smaller version of the example that keeps the core idea.

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

Write a smaller version of the example that keeps the core idea.

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 148 of 480 · 0% of course