What you will build
You will turn a product question into a focused SQL query, inspect the exact result and understand why AI applications still need structured data.
Priya’s AI assistant needs facts, not guesses
Priya asks a shopping assistant, “Show two active headphones, highest price first.” A language model can understand the sentence, but the latest product name, status and price live in a database. SQL retrieves those facts in a controlled way.
Tables give business data a dependable shape
| Table | One row represents | Important columns |
|---|---|---|
products | one product | product_id, name, category, price, status |
customers | one customer | customer_id, name, city |
orders | one order | order_id, customer_id, ordered_at, total_amount |
A primary key identifies one row. A foreign key links it to another table. orders.customer_id, for example, points to the customer who placed the order.
How the database answers the request
FROMchooses the table.WHEREkeeps matching rows.SELECTreturns approved columns.ORDER BYarranges the rows.LIMITbounds the response.
Walk through the SQL
01SELECT product_id, name, price02FROM products03WHERE category = 'Headphones'04AND status = 'ACTIVE'05ORDER BY price DESC06LIMIT 2;
Exact result
| product_id | name | price |
|---|---|---|
| 101 | Wireless Headphones | ₹4,999 |
| 104 | Studio Headphones | ₹3,499 |
The output has a predictable three-column contract. Avoid SELECT * in an application: a later schema change could expose unnecessary data or silently change the response shape.
Connect customers to orders
01SELECT o.order_id, c.name, o.total_amount02FROM orders AS o03JOIN customers AS c ON c.customer_id = o.customer_id04WHERE o.ordered_at >= DATE '2026-09-01'05ORDER BY o.ordered_at DESC;
The JOIN condition connects matching keys. Without it, every order could be paired with every customer, creating incorrect duplicate combinations.
Real-world application: support answer
An AI support assistant may understand “Where is Meena’s latest order?” but it should not invent the status. The application extracts the authorised customer ID, runs a parameterised SQL query and gives the returned status to the model for explanation.
SQL and the AI model have different jobs
| Capability | SQL | Language model |
|---|---|---|
| Retrieve exact approved rows | Strong | Should use a tool |
| Apply deterministic filters | Strong | May be inconsistent |
| Explain conversationally | Limited | Strong |
| Enforce database permission | Database responsibility | Not a substitute |
Common mistakes to catch early
Common mistakes
- Using
SELECT *when the application needs only three columns. - Writing
price = NULL; useprice IS NULLinstead. - Using
LIMITwithoutORDER BYwhen “top” or “latest” matters. - Joining on names instead of stable IDs.
- Building SQL by concatenating user text instead of using parameters.
Practice before moving on
Write a query that returns the three lowest-priced active products in the Keyboard category. Return only product_id, name and price.
Choose an answer, inspect the explanation and explain the idea in your own words.
Which clause decides whether a product row qualifies for the result?
Interview-ready explanation
Why does an AI application still need SQL?
A language model can interpret a natural-language request, but SQL retrieves current structured facts deterministically. The application combines language understanding with parameterised queries, explicit permissions and a predictable result contract.
Questions beginners usually ask
Does SQL execute in the order it is written? Not exactly. A useful beginner mental model starts with FROM and WHERE, then considers SELECT, ORDER BY and LIMIT.
Should the model connect directly to production? No. An application layer should enforce identity, permissions, validation, parameters and logs.
What to remember
Key takeaways
- SQL retrieves structured facts that an AI application should not guess.
- Tables, primary keys and foreign keys create reliable relationships.
- Select explicit columns, filter intentionally, sort deterministically and bound the response.
- Permissions and parameterised queries remain necessary when AI is involved.
Next Byte: turn many order rows into business totals, rankings and trends with aggregations and window functions.