Course contents
On this page
Lesson 03 of 59

Ask for a row, then change it

Read SELECT and UPDATE as requests against the product table.

We have a table called products. Its columns include product_id, name, price, and stock. The application needs the name and price of product 27.

SQL is a language for expressing requests like that. A statement is one instruction written in the language. Read this statement from the table and condition toward the result:

SELECT name, price
FROM products
WHERE product_id = 27;

FROM products chooses the table. WHERE product_id = 27 keeps rows whose product number equals 27. SELECT name, price chooses which columns to return. The semicolon ends the statement.

With our example data, the result is one row:

nameprice
Canvas tote20

Reading the row does not change it. SQL describes the result we want; the engine chooses how to find it. That choice will become the subject of our indexing and query-execution lessons.

Change one fact

The seller raises the price to 25 credits:

UPDATE products
SET price = 25
WHERE product_id = 27;

UPDATE identifies the table to change. SET supplies the new value. The condition selects which rows to change. If the condition were omitted, this statement would set the price of every product to 25.

  1. Choose the row

    Its product_id must equal 27.

  2. Replace one value

    The price changes from 20 to 25. The name and stock remain the same.

  3. Read it again

    The same SELECT now returns Canvas tote and 25.

This trace assumes one caller, a successful committed update, and no other changes between the statements.

An INSERT adds a row. A DELETE removes selected rows. Those operations must also respect the schema’s rules. We will examine their implementation after we can follow a read.

A condition is a test, not a position

WHERE stock = 0 means “keep rows whose stock equals zero.” It does not mean “go to storage location zero.” Without a structure that guides the search, the engine can inspect each row and test the condition. This is called a scan.

SQL also lets us request an order explicitly. Without an ORDER BY clause, we must not rely on a particular result order, even if repeated runs happen to return the same sequence. Storage and execution choices may change it.

Predict the result

After the price change, consider:

SELECT product_id
FROM products
WHERE price < 30
ORDER BY product_id;

Notebook 12 costs 8, tote 27 costs 25, and lamp 46 costs 35. What comes back?

Apply the condition to each row

Products 12 and 27 satisfy “price below 30.” Product 46 does not. The result contains one column, product_id, with 12 followed by 27 because we explicitly requested increasing product-number order.

Neither the returned column nor its order is determined by how the products happened to be stored.

Next we will connect a product to an order stored in another table. That connection motivates a join.

Further reading

PostgreSQL’s query tutorial explains selection and explicit result ordering. The examples here use a deliberately small SQL subset.