Setup: Run setup.sql first. All questions reference the ecommerce_db database.
Difficulty scale: [E] Easy · [M] Medium · [H] Hard
A BI report queries vw_customer_summary and returns a column called loyalty_tier.
- Which base table and column does
loyalty_tieractually map to? - Write a query that retrieves the same value directly from the base table for customer
alice@example.com. - Bonus: What column does
tier_discountcome from, and which table owns it?
The vw_customer_summary view exposes a column called full_name.
- How is
full_nameconstructed? There is nofull_namecolumn anywhere in the base tables — explain where the value comes from. - Write a query to find any customer rows where
full_nameas returned by the view would not equal a simplefirst_name || ' ' || last_nameconcatenation. - What MySQL function could you use instead of
CONCATto handle the NULL case gracefully, and how would it change the output?
You've inherited a codebase and someone mentions "the discount_pct column." You don't know which table it lives in.
- Write a query against
INFORMATION_SCHEMAthat lists every table and column inecommerce_dbwhose name contains the stringdiscount. - Extend that query to also show the column's data type and whether it allows NULLs.
- Write a second query that searches view definitions in
INFORMATION_SCHEMAfor any view that references the stringdiscount_pct.
The view vw_high_value_customers exposes a column called tier_discount.
- Trace
tier_discountall the way back to the physical base table and original column name. How many hops does it take? - Write the chain as:
vw_high_value_customers.tier_discount→???→table.column. - Write a single query using
INFORMATION_SCHEMA.VIEWSto retrieve the definition of each view in the chain and identify the hops programmatically (rather than reading the DDL by hand).
You're reviewing the application code and notice that every INSERT into order_items omits the line_total column entirely — yet when you SELECT * FROM order_items, every row has a non-NULL line_total.
- Explain how this is possible. Where is
line_totalbeing set? - Write the SQL you would run to discover the trigger(s) responsible — without looking at the application code or knowing the trigger name in advance.
- Verify by running:
What do you expect
INSERT INTO order_items (order_id, product_id, quantity, unit_price) VALUES (1, 2, 3, 9.99);
line_totalto be? Why? - T-SQL note: Write the equivalent metadata query for SQL Server instead of MySQL.
A downstream report shows order_status = 'Delivered' for some orders. You search every table in the database and find no column named order_status and no row containing the string 'Delivered'.
- How would you find where
order_statuscomes from? - Identify the view responsible and explain the transformation it applies.
- The
orderstable storesstatus_code. List all the valid codes and their human-readable meanings as defined in this database. - A new status
'BO'(Backordered) needs to be added. What exactly needs to change, and in how many places?
Orders with source_system = 'IMPORT' appear in the orders table, but no application INSERT directly into orders for those rows. The orders_staging table holds raw data from an external feed.
- How would you find the stored procedure responsible for moving data from
orders_stagingintoorders? - The external feed sends
raw_status = 'confirmed'. What value ends up inorders.status_code? Trace the mapping. - The external feed sends
raw_total = '149.98'as a VARCHAR. What happens to the data type during the load? Is there a risk here? - Carol has a row in
orders_stagingwithraw_status = 'confirmed'. Write the call to execute the load procedure and then verify the result.
Focus on the customers table, specifically the columns customer_tier and last_modified_by.
- Enumerate every code path (triggers, procedures, direct SQL) through which
customer_tiercan be changed. For each path, describe what drives the change. - Do the same for
last_modified_by. Notice that different paths stamp different values — describe each one. - After running the setup script, query
customersto see the current values ofcustomer_tier,lifetime_orders, andlast_modified_byfor all three customers. Explain why each customer has the values they do. - You find a row where
last_modified_by = 'TRIGGER:trg_update_customer_tier'. What does that tell you about the most recent action that modified that customer? - You find a row where
last_modified_by = 'root@localhost'(or your connection'sUSER()). What does that tell you — and why might it be less informative than the trigger value?
Q9 — Ghost Writes: Finding Hidden INSERTs [M]
Rows are appearing in order_status_log but nobody can find any INSERT INTO order_status_log in the application code or stored procedures.
- Write the query to find all triggers in
ecommerce_dband show their event, timing, and the table they fire on. - Identify which trigger is writing to
order_status_log. What condition must be true for a row to be written? - Demonstrate the trigger by running an UPDATE that changes
status_codeon an order, and then verify the log entry was created. - How would you write this same discovery query in T-SQL (SQL Server)?
These don't have SQL answers but are worth thinking through:
-
D1: You're given a column
processed_byin theorderstable. It's sometimesNULL, sometimes'sp_process_staged_orders', and sometimes an employee ID. How would you approach understanding all the paths that can write to this field in a large production system you've never seen before? -
D2: A colleague says "the
total_amountcolumn is alwayssubtotal - discount_amount." How would you verify or disprove this claim? Write the query. -
D3: You need to understand data lineage for an audit.
order_status_logtracks status changes — but what doesn't it track? What gaps would you flag to the engineering team?