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.
ALTER TABLE PRACTICE (25) Topics: Add, drop, rename columns; change types
Q51Add a new column 'department' VARCHAR(50) to practice.employees.
Q52Add a new column 'last_login' TIMESTAMP to practice.customers.
Q53Add a column 'discount_pct' INT to practice.products.
Q54Add a column 'is_verified' BOOLEAN to practice.customers.
Q55Add a column 'updated_at' TIMESTAMPTZ to practice.products.
Q56Rename the column 'department' in employees to 'dept_name'.
Q57Rename the column 'discount_pct' in products to 'discount_percent'.
Q58Rename the table 'reviews' to 'product_reviews'.
Q59Drop the column 'last_login' from customers.
Q60Drop the column 'is_active' from customers.
Q61Change the type of products.price from NUMERIC(12,2) to NUMERIC(14,2) - to allow larger prices.
Q62Change the type of employees.salary to NUMERIC(12,2).
Q63Change customers.phone from VARCHAR(20) to VARCHAR(25).
Q64Add a column 'tags' of type TEXT[] (array of text) to practice.products.
Q65Add a column 'metadata' of type JSONB to practice.products.
Q66Add a column 'rating_avg' NUMERIC(3,2) to practice.products.
Q67Drop the column 'metadata' you just added to products.
Q68Add a column 'created_at' TIMESTAMP with DEFAULT CURRENT_TIMESTAMP to practice.products.
Q69Add a column 'is_archived' BOOLEAN with DEFAULT FALSE to practice.products.
Q70Set a DEFAULT of 0 on the discount_percent column in products.
Q71Drop the DEFAULT from discount_percent.
Q72Rename the schema archive to cold_storage.
Q73Drop the table practice.notifications.
Q74Drop the entire cold_storage schema with CASCADE (removes everything inside).
Q75Drop the practice.subscriptions table only if it exists (use IF EXISTS).
PICK THE RIGHT DATA TYPE (25) For each scenario, the question asks you to write the column declaration with the type you'd choose. Run nothing - just declare the type.
Q76Storing an Indian phone number ("+91 9876543210"). What type?
Q77Storing a customer's lifetime spend (Rs with paisa precision). What type?
Q78Storing a product description that could be a few sentences. What type?
Q79Storing whether a customer has opted in to marketing emails. What type?
Q80Storing the timestamp a user clicked a button (millisecond precision, with timezone). What type?
Q81Storing just the date someone joined the company. What type?
Q82Storing a UPI VPA like "priya@axis". What type and length?
Q83Storing a pincode (always 6 digits). What type and constraint?
Q84Storing the latitude of a delivery address. What type and precision?
Q85Storing a primary key for a small lookup table (~50 rows). What type?
Q86Storing a primary key for a high-volume events table (1B+ rows). What type?
Q87Storing event payload from an API webhook (nested JSON). What type?
Q88Storing a session token that should be globally unique. What type?
Q89Storing an order quantity (small integer, 1-1000). What type?
Q90Storing a percentage value like 12.50% with 2 decimal precision. What type?
Q91Storing a long product review that might be 5,000 characters. What type?
Q92Storing a yes/no flag for is_email_verified. What type?
Q93Storing a date range start (just a date, no time). What type?
Q94Storing a website URL (could be 2,000 chars). What type?
Q95Storing tax_rate as a percentage with 4 decimal precision (e.g., 18.0000). What type?
Q96Storing tags for a blog post (multiple tags per post). What type or design?
Q97Storing IPv4 / IPv6 addresses. What type?
Q98Storing a hash (SHA-256, 64 hex chars). What type?
Q99Storing a price in USD that may need precision up to 4 decimals (currency conversion). What type?
Q100Storing a one-letter status code ('A', 'I', 'D'). What type?
Combined ideas, multi-step thinking
DDL & DATA-TYPE DEEPER CONCEPTUAL
Q1Compare NUMERIC(10,2), NUMERIC, and FLOAT - when do you pick each?
Q2Defend why a 4-byte INT is enough for most SERIAL columns but not for high-volume events.
Q3What is a GENERATED COLUMN in PostgreSQL - give a real RetailMart-like use case.
Q4Compare CHAR(64) vs VARCHAR(64) vs TEXT for storing a SHA-256 hex hash. Which is best?
Q5When does TIMESTAMPTZ matter MORE than TIMESTAMP - give a concrete example.
Q6Why is INTERVAL its own type? When have you actually needed it?
Q7Compare TEXT[] (array of text) vs a separate child table. Defend each design with one use case.
Q8When would you use ENUM in PostgreSQL - and what's the migration tax?
Q9Defend the choice of UUID vs SERIAL for a "tracking event" primary key.
Q10What's the difference between SMALLINT, INT, BIGINT - and how much disk do they each use?
Q11Why do experienced engineers ALMOST NEVER use FLOAT/REAL columns?
Q12Compare DOUBLE PRECISION vs NUMERIC(20,4) for a "GPS latitude" column.
Q13What's the difference between DEFAULT NOW() and DEFAULT CURRENT_TIMESTAMP?
Q14Why is "ON UPDATE CURRENT_TIMESTAMP" not native to PostgreSQL - what alternative do PG users use?
Q15Defend the choice to use BOOLEAN vs a 1-character CHAR(1) for a yes/no flag.
Q16What is BYTEA used for - and when would you NOT store binary in the database?
Q17Compare TIMESTAMP(3) vs TIMESTAMP - what does the (3) mean?
Q18A junior types JSONB-everything. Argue when JSONB is OVERKILL vs RIGHT.
Q19Why is "INET" type useful for storing IP addresses (over VARCHAR)?
Q20Compare GENERATED ALWAYS AS IDENTITY vs SERIAL - which does modern PostgreSQL prefer and why?
Q21Why is choosing the SMALLEST appropriate integer type still important even with cheap disk?
Q22Defend the use of CITEXT for case-insensitive comparisons over LOWER()-everywhere.
Q23Explain the design tradeoff between storing a calculated field vs computing on-the-fly.
Q24When designing a new table, what FIVE columns should almost every business table have?
Q25Compare schema-per-tenant vs row-per-tenant (multi-tenant SaaS) - when do you pick each?
Q27Create practice.order_items with FK to a (hypothetical) practice.orders table - on CREATE.
Q28Create a table with a generated column: full_name = first_name || ' ' || last_name.
Q29Create a table that uses GENERATED ALWAYS AS IDENTITY for the primary key (instead of SERIAL).
Q30Create a table with an inline UNIQUE constraint on (email, tenant_id).
Q31Create a table with three CHECK constraints inline: price > 0, quantity >= 0, name NOT NULL.
Q32Create a table that uses TEXT[] for a 'tags' column.
Q33Create a table that uses JSONB for 'metadata' with a CHECK that requires 'source' key (use jsonb_exists).
Q34Create a table that uses NUMERIC(20,4) for a 'exchange_rate' column.
Q35Create a table with a DEFAULT NOW() on created_at AND a separate updated_at with DEFAULT NOW().
Q36Create a table with a CHECK that validates email contains '@' (regex check).
Q37Create a table partitioned by RANGE on created_at by month - for events.
Q38Create a table that has BOTH a SERIAL surrogate PK and a UNIQUE business key (e.g., sku_code).
Q39Create a self-referencing FK table: categories(category_id PK, parent_category_id INT REFERENCES same table).
Q40Create a table with a multi-column FK: order_line(order_id INT, line_no INT, product_id INT) -> orders(order_id) AND constraint on (order_id, line_no) being unique.
Q41Create a junction table for products and tags with composite PK (product_id, tag_id) and both FKs.
Q42Create a table with column-level COMMENTS using COMMENT ON COLUMN.
Q43Create a table with a CHECK using BETWEEN and IN combined.
Q44Create a table that uses INET for an IP address column.
Q45Create a table using CITEXT for case-insensitive email column (must enable citext extension first).
Q46Create a table where one column is generated stored: total = quantity * unit_price (STORED).
Q47Create a table with TWO surrogate keys (one INT SERIAL for legacy, one UUID for new system).
Q48Create a table with a DEFERRABLE INITIALLY DEFERRED FK (advanced - for chicken-and-egg insert order).
Q49Create a temporary table using CREATE TEMP TABLE - what's the lifetime?
Q50Create an UNLOGGED table - when is this appropriate and what do you give up?
COMPLEX ALTER PATTERNS
Q51Add THREE columns to practice.products in a single ALTER TABLE statement.
Q52Drop TWO columns from practice.products in a single ALTER.
Q53Add a column with a DEFAULT and NOT NULL in one shot - what's the migration risk?
Q54Change the data type of products.price from NUMERIC(12,2) to NUMERIC(14,2) USING a cast.
Q55Migrate an INT column 'status_id' to a VARCHAR 'status' - show the multi-step migration.
Q56Rename a table AND its index in two statements.
Q57ALTER TABLE to add a CHECK constraint on existing data - what if some rows violate?
Q58Add a NOT NULL to a column that has existing NULLs - what's the safe two-step approach?
Q59Use ALTER TABLE ... ATTACH PARTITION to add a partition to a partitioned table.
Q60Use ALTER TABLE ... SET TABLESPACE to move a table to a different tablespace.
Q61Add a UNIQUE constraint to an existing column with duplicate values - what happens?
Q62Combine ADD COLUMN + ALTER TYPE + SET NOT NULL in one ALTER statement.
Q63Use ALTER TABLE ... DISABLE TRIGGER to temporarily silence a trigger during bulk load.
Q64Use ALTER TABLE ... INHERIT to make one table inherit from a parent (legacy partitioning).
Q65Drop a column that's referenced by a view - what error do you get and how do you resolve?
Q66Rename a column referenced by an FK constraint - does the FK still work?
Q67ALTER COLUMN to change a column's collation.
Q68ALTER COLUMN ... SET STATISTICS to increase planner accuracy on a heavily-queried column.
Q69ALTER COLUMN ... SET STORAGE EXTENDED - what is column storage and when does it matter?
Q70Add an INDEX to an existing column via CREATE INDEX (not ALTER TABLE).
Q71Add a PARTIAL INDEX (WHERE clause-bound) on an active rows only.
Q72Add a UNIQUE INDEX (not a UNIQUE constraint) - what's the difference?
Q73Drop an existing index that's no longer used.
Q74Rename an index.
Q75Use CREATE INDEX CONCURRENTLY to add an index without locking the table.
DESIGN EXERCISES
Q76Design a 'feature_flags' table for a SaaS app: feature_name, percent_rollout, active_from. List columns + types + constraints.
Q77Design a 'user_sessions' table that tracks login activity. List columns + types + indexes.
Q78Design a 'price_history' table - every time a product's price changes, log a row.
Q79Design a 'subscription' table for monthly billing - list columns including next_billing_date.
Q80Design a 'product_categories_tree' table that can store a hierarchy of unlimited depth.
Q81Design a 'two-factor_auth_codes' table - temporary codes with expiry.
Q82Design a 'cron_jobs' table that schedules background tasks.
Q83Design a 'webhook_deliveries' table that tracks attempt/retry/success.
Q84Design a 'shopping_cart_items' table (cart_id, product_id, qty, price_at_add).
Q85Design a 'addresses' table that supports MULTIPLE addresses per customer with one default.
Q86Design a 'coupons' table with code, discount_pct, max_uses, valid_until.
Q87Design a 'banking_transactions' table with idempotency_key + ACID guarantees.
Q88Design a 'audit_record_changes' table that logs every UPDATE on a target table.
Q89Design a 'inventory_movements' table that tracks every stock-in / stock-out event.
Q90Design a 'kyc_documents' table for storing customer document uploads.
Q91Design a 'leaderboard' table for a game (user_id, score, season_id).
Q92Design a 'support_ticket_messages' table - many messages per ticket.
Q93Design an 'email_outbox' table that the application reads to send emails.
Q94Design a 'rate_limit_buckets' table for an API rate limiter.
Q95Design a 'tenants' table for multi-tenant SaaS.
Q96Design a 'product_variants' table (one product, many variants like size/color).
Q97Design a 'survey_responses' table where each survey has dynamic questions (JSONB or normalized?).
Q98Design a 'gdpr_data_requests' table - track delete-my-data requests with status.
Q99Design a 'ml_predictions' table to store model output: input_id, prediction, confidence, model_version, timestamp.
Q100Design a 'a_b_experiments' table to track which user got which variant.
Interview grade, edge cases
ADVANCED DDL - CONCEPTUAL
Q1What is a GENERATED ALWAYS AS column - give 3 use cases.
Q2Compare STORED vs VIRTUAL generated columns (Postgres only supports one).
Q3Why is PARTITION BY RANGE on order_date the standard pattern for time-series?
Q4When do you choose PARTITION BY LIST vs RANGE vs HASH?
Q5What is "partition pruning" - and why does it require the planner to see the partition key in WHERE?
Q6Compare table INHERITANCE (legacy) vs declarative PARTITIONING.
Q7What is a DOMAIN type - and when is it cleaner than a CHECK constraint?
Q8What is a COMPOSITE type - give a use case (address: street/city/zip).
Q9When do you use ENUM types vs a lookup table?
Q10Why is ENUM hard to extend in production (ADD VALUE limitations)?
Q11Walk through CHECK with regex (e.g., email format) - and when to NOT do this in DDL.
Q12What is an EXCLUSION constraint - give a booking-overlap example.
Q13ON DELETE CASCADE vs ON DELETE SET NULL vs ON DELETE RESTRICT - when each.
Q14What is a DEFERRABLE constraint - when is INITIALLY DEFERRED useful?
Q15Why does Postgres require UNIQUE constraints on the partition key for partitioned tables?
Q16Walk through how attach/detach partition works without downtime.
Q17What is sub-partitioning - and why do most setups avoid it?
Q18Compare GENERATED column vs trigger-based computed column - tradeoffs.
Q19What is a "default partition" - what happens when a row matches no defined partition?
Q20Why is HASH partitioning bad for time-range queries?
Q21Explain how foreign keys interact with partitioned parents (PG12+).
Q22What is "constraint exclusion" (legacy) vs "partition pruning"?