400 questions in four levels, all on RetailMart, the practice database of this course. Write every query yourself, get it wrong, read the error, fix it. That is how it sticks.
Q72Insert one row into employees_with_fk: full_name 'Test User', tier_id 2 (must exist).
Q73Try inserting a duplicate email into customers (after UNIQUE added). What error do you expect?
Q74Insert via SELECT - populate practice.brands_copy (you'll need to create it first) from brands.
Q75Insert a transaction that violates the UNIQUE (from_account, txn_time) constraint. What error?
UPDATE / DELETE / TRUNCATE / TRANSACTIONS
Q76Update practice.customers SET is_active = FALSE WHERE cust_id = 1.
Q77Update all products SET in_stock = TRUE.
Q78Update practice.products SET price = price * 1.10 - give every product a 10% price hike.
Q79Update practice.employees SET salary = 50000 WHERE salary IS NULL.
Q80Update practice.customers SET phone = '+91 9999999999' WHERE email = '[email protected]'.
Q81Update practice.bank_accounts SET balance = balance - 1000 WHERE account_id = 1.
Q82Update practice.bank_accounts SET balance = balance + 1000 WHERE account_id = 2.
Q83Delete one row: DELETE FROM customers WHERE cust_id = 1.
Q84Delete all products where price < 50.
Q85Delete all feedback with rating < 3.
Q86Delete a customer WITH cascade - verify their orders_demo rows are also deleted (because of ON DELETE CASCADE you added earlier).
Q87TRUNCATE practice.feedback - fast wipe.
Q88TRUNCATE practice.events_log RESTART IDENTITY - wipe AND reset the BIGSERIAL counter.
Q89Run a transaction: BEGIN; UPDATE bank_accounts SET balance = balance - 5000 WHERE account_id = 1; UPDATE bank_accounts SET balance = balance + 5000 WHERE account_id = 2; COMMIT.
Q90Run a transaction that you ROLLBACK to verify rollback works: BEGIN; UPDATE bank_accounts SET balance = 0; ROLLBACK. Then SELECT to verify balances unchanged.
Q91Update product names: SET name = INITCAP(name) on practice.products.
Q92Update practice.employees SET dept_name = 'Engineering' WHERE dept_name IS NULL.
Q93Delete from practice.notifications where sent_at < CURRENT_DATE - INTERVAL '30 days' (purge old notifications).
Q94Update practice.subscriptions SET auto_renew = FALSE WHERE ends < CURRENT_DATE.
Q95Delete from practice.login_log where login_time < CURRENT_DATE - INTERVAL '90 days'.
Q96TRUNCATE practice.notifications.
Q97Delete every row from sessions where ended IS NOT NULL (already closed).
Q98Update orders_demo SET total = 0 WHERE total IS NULL.
Q99Inside a transaction, INSERT a customer, then ROLLBACK. After ROLLBACK, query - should the customer exist?
Q100Wipe ALL practice tables by using TRUNCATE ... CASCADE on practice.customers (cascades to children).
Combined ideas, multi-step thinking
CONSTRAINTS & DML DEEPER CONCEPTUAL
Q1Compare ON DELETE CASCADE vs ON DELETE SET NULL vs ON DELETE RESTRICT - give a real-world example where each is correct.
Q2What does "DEFERRABLE INITIALLY DEFERRED" mean on a FK - when do you need it?
Q3Difference between a UNIQUE constraint and a UNIQUE INDEX - which is more flexible?
Q4What is a PARTIAL UNIQUE index - give a use case from a multi-tenant SaaS.
Q5Why can't a CHECK constraint reference ANOTHER table? What do you use instead?
Q6Explain how PostgreSQL validates CHECK constraints when you ALTER TABLE ADD CHECK on existing data.
Q7What is INSERT ... ON CONFLICT (upsert) - and what TWO patterns does it support?
Q8What does the RETURNING clause do - give a use case where it saves a round trip.
Q9Compare DELETE vs TRUNCATE vs DROP TABLE - when is each appropriate?
Q10Why does PostgreSQL allow you to UPDATE multiple tables in one transaction but NOT in one UPDATE statement?
Q11Explain the "lost update" problem and how SELECT FOR UPDATE solves it.
Q12Compare optimistic vs pessimistic locking - which is which and when do you use each?
Q13What is a SAVEPOINT - and when is it more useful than just COMMIT/ROLLBACK?
Q14Why is INSERT ... SELECT often used in ETL pipelines?
Q15Compare UPDATE ... SET col = (SELECT ...) (correlated) vs UPDATE ... FROM (joined). Which is generally faster?
Q16Explain what "tuple visibility" means in PostgreSQL - and why DELETE doesn't actually free disk space.
Q17Why is VACUUM needed in PostgreSQL - what does it actually do?
Q18What is autovacuum - and when does it run?
Q19Compare INSERT INTO ... VALUES (...) vs prepared statements - when does each win?
Q20What happens if you INSERT 1000 rows in a single INSERT vs 1000 separate INSERT statements (no transaction)?
Q21Why do experienced engineers wrap bulk DML in transactions even for INSERTs?
Q22Explain what "phantom reads" are and which isolation level prevents them.
Q23Compare READ COMMITTED vs SERIALIZABLE isolation levels - which is the PostgreSQL default?
Q24What is a TRIGGER - give one example of when it's the right tool vs when it's an anti-pattern.
Q25Why do many teams BAN triggers in production code - even though PostgreSQL supports them?
ADVANCED CONSTRAINT SCENARIOS
Q26Add a multi-column UNIQUE on practice.transactions: (from_account, to_account, txn_time) - prevent exact-duplicate transfers at the same moment.
Q27Add a CHECK on practice.bank_accounts that balance must be either 0 OR positive when account_type is 'Savings'.
Q28Add a DEFERRABLE FK between two tables so you can insert children before parents in a single transaction.
Q29Add a PARTIAL UNIQUE INDEX to practice.addresses ensuring only ONE is_default = TRUE per customer.
Q30Add a CHECK to practice.subscriptions ensuring ends > starts.
Q31Add a CHECK to practice.products ensuring discount_percent BETWEEN 0 AND 100.
Q32Add a CHECK on practice.employees ensuring joining_date <= CURRENT_DATE (no future-dated joins).
Q33Add a CHECK on practice.product_reviews ensuring rating BETWEEN 1 AND 5 AND comment IS NOT NULL when rating <= 2 (force feedback on low ratings).
Q34Add a CHECK on practice.feedback ensuring (rating = 5 AND freeform IS NOT NULL) OR rating < 5 (i.e., 5-star reviews must have a comment).
Q35Add a composite CHECK on practice.coupons (if you created it from Topic 3): valid_until > CURRENT_DATE OR uses_count >= max_uses.
Q36Add an ON DELETE SET NULL between practice.employees_with_fk(tier_id) and practice.tier_master(tier_id).
Q37Add an ON DELETE CASCADE between practice.transactions(from_account) and practice.bank_accounts(account_id) - what's the risk?
Q38Drop a CHECK constraint by looking it up in pg_constraint first.
Q39Add a CHECK using a complex expression: ON practice.products, expensive products (price > 10000) must have a sku_code.
Q40Add an EXCLUSION constraint on practice.subscriptions preventing two overlapping date ranges for the same user_id (advanced - needs btree_gist).
Q41Add a NOT NULL on practice.customers.created_at with a DEFAULT NOW() in one statement.
Q42Drop the NOT NULL on practice.customers.is_active (allow NULL again).
Q43Set the DEFAULT of practice.products.in_stock to FALSE - without affecting existing rows.
Q44Use NOT VALID to add a constraint that only checks new rows, then VALIDATE later.
Q45Add a UNIQUE constraint on (LOWER(email)) using a unique index expression.
Q46Create a constraint that ensures email contains '@' - use a CHECK with the regex operator.
Q47Create a constraint that ensures phone is exactly 10 digits if not NULL (use CHECK with LENGTH).
Q48Add a CHECK ensuring pincode is a 6-digit number stored as VARCHAR.
Q49Add a CHECK ensuring discount_percent < tax_rate doesn't lead to negative net (cross-column CHECK).
Q50List all constraints on practice.products using pg_constraint.
BULK DML PATTERNS
Q51INSERT 1000 rows into practice.notifications using INSERT ... SELECT from generate_series(1, 1000).
Q52INSERT INTO practice.archive_orders SELECT * FROM practice.orders_demo WHERE order_date < '2024-01-01'.
Q53UPDATE all products in practice.products that have NULL discount_percent -> SET to 0.
Q54UPDATE practice.products SET price = price * 1.10 WHERE brand_id IN (1, 2, 3).
Q55UPDATE FROM pattern: update practice.customers SET tier = m.tier_id FROM practice.tier_master m WHERE m.tier_id = practice.customers.tier_id.
Q56DELETE FROM practice.notifications USING practice.customers WHERE notifications.user_id = customers.cust_id AND customers.is_active = FALSE.
Q57INSERT INTO practice.products (name, price, in_stock) VALUES (...), (...), (...) - 10 rows in one statement.
Q58Upsert: INSERT INTO practice.tier_master VALUES (4, 'Diamond', 1000) ON CONFLICT (tier_id) DO UPDATE SET tier_name = EXCLUDED.tier_name.
Q59Upsert with DO NOTHING: INSERT INTO practice.customers (email) VALUES ('[email protected]') ON CONFLICT (email) DO NOTHING.
Q60Use RETURNING to get the newly inserted customer's auto-generated cust_id.