What You Need to Know to Judge SQL
Tables: What Does One Row Mean?
You're the Judge, Not the Typist
AI writes SQL faster than you ever will. It also writes wrong SQL with the same confidence as right SQL, and wrong SQL usually runs fine. No error, no red text. Just a number that's off.
So the skill isn't typing queries. It's looking at one and knowing whether to believe it. This course uses SQL Server (T-SQL) and Entity Framework Core 10, and one small shop database:
| Table | One row is | Points to |
|---|---|---|
Customers |
one customer | nothing |
Orders |
one order | Customers, by CustomerId |
OrderLines |
one product on one order | Orders, by OrderId |
Payments |
one payment on one order | Orders, by OrderId |
Read the middle column again. "What is one row?" is the most important question in SQL. Ask it about every table going in and about the result coming out. A lot of wrong reports are just somebody losing track of the answer.
Let the Database Say No
A table can refuse bad data, so no app bug and no AI can put it there:
CREATE TABLE dbo.OrderLines (
OrderLineId int IDENTITY(1,1) NOT NULL
CONSTRAINT PK_OrderLines PRIMARY KEY,
OrderId int NOT NULL
CONSTRAINT FK_OrderLines_Orders
REFERENCES dbo.Orders (OrderId),
ProductId int NOT NULL,
Quantity int NOT NULL
CONSTRAINT CK_OrderLines_Quantity CHECK (Quantity > 0),
UnitPrice decimal(10,2) NOT NULL
);
- Primary key: what makes a row unique.
- Foreign key: no line can belong to an order that doesn't exist.
- NOT NULL and CHECK: no blanks, no negative quantities.
- Types: money is
decimal. Neverfloat. In afloat, 0.1 + 0.2 does not equal 0.3.
When AI hands you a table, check those four first.
NULL means "unknown," and it isn't equal or unequal to anything. WHERE Status <> 'Shipped' silently skips every row where Status is NULL. Any column that allows NULL needs a decision from you: do those rows count or not?
What's the first question to ask about any table or query result?