Model relationships and query at the intended grain
Use keys, normalization, SQL statements and database objects to preserve meaning as rows change.
The doubled order total
One order has three lines and two payments. A report joins both child tables directly to the order before summing the line amounts. Why does it inflate revenue?
Run the DP-900 SQLite join lab to check your prediction with five rows of data.
Keys and relationships
A row represents one entity or event at a chosen grain. A primary key identifies it. A foreign key links it to a valid parent. A customer can have many orders. A junction table can represent a many-to-many relationship between orders and products.
Normalization
Separate facts according to what they describe. Keeping each customer’s current contact data in one customer row avoids updating every order. A historical price paid is a fact of the order line and should survive a later change to the product’s list price.
SQL statement families
SELECT reads rows. INSERT, UPDATE and DELETE change data. CREATE and ALTER change definitions. GRANT controls permissions on engines that support it. Use transactions to group changes and verify a WHERE condition before applying an update.
Common database objects
Tables store rows. A view stores a query definition and can provide a consistent interface to joined data. An index can accelerate suitable access paths while adding write overhead. Stored procedures encapsulate server-side logic where the engine supports them.
Work through the decision
Compare your reasoning
The join can produce six rows and repeat each line per payment. Aggregate lines and payments separately to order grain before joining. SUM(DISTINCT amount) can remove legitimate equal-priced lines and is not a general repair.
Change one constraint
Where would you store the actual unit price paid if next month’s catalog price will change?
Check your answer
Store the agreed unit price on the order line and maintain the current list price in the product catalog. They describe different business facts.
DP-900 lab · Reproduce SQL join fan-out
Version 1.0 · Python 3 with built-in SQLite. Use this five-row exercise to show why a report doubles an order total and how grouping each table by order fixes it. Copy the code into a Python file and run it locally, or trace the rows on paper.
import sqlite3
db = sqlite3.connect(':memory:')
db.executescript('''
CREATE TABLE line (order_id INTEGER, amount INTEGER);
CREATE TABLE payment (order_id INTEGER, received INTEGER);
INSERT INTO line VALUES (1, 10), (1, 10), (1, 30);
INSERT INTO payment VALUES (1, 20), (1, 30);
''')
bad = 'SELECT SUM(amount) FROM line JOIN payment USING (order_id)'
good = '''WITH lines AS (SELECT order_id, SUM(amount) AS total FROM line GROUP BY order_id),
payments AS (SELECT order_id, SUM(received) AS paid FROM payment GROUP BY order_id)
SELECT total, paid FROM lines JOIN payments USING (order_id)'''
print(db.execute(bad).fetchone())
print(db.execute(good).fetchone())
db.close()
Check your result
Expected output, in order:
(100,) (50, 50)
The first query counts each line once per payment. The second groups the lines and payments separately, giving 50 in sales and 50 received. Summing distinct amounts would drop one legitimate line of 10. In T-SQL, express the join condition with ON.
Revisit the recording
The T-SQL section distinguishes defining a table, changing its rows and reading a result. Identify which statement would change the persisted data. T-SQL · 2:14:12.
The SSMS demonstration runs a query against Azure SQL. Verify the target database and read the row grain before aggregating its results. Use SSMS to perform a query on Azure SQL · 3:44:20.
Sources and further practice
Microsoft exam objectives · Microsoft documentation for this case.
Keep the right rows in a join · SQL querying with SQLite · Return to the study map.
Model relationships and query at the intended grain quiz
Answer 6 questions. Check each answer for an explanation.