bhylu.com

Advertisements

SQL Select

Choose only the columns your page or report needs. Returning every column is convenient while exploring, but an explicit list makes application code easier to understand and reduces unnecessary data transfer.

Understand the idea

A SELECT list can contain column names, calculations, and aliases. It does not need to match the table column order. This query calculates a display amount from integer cents. Dividing by 100.0 requests a fractional result in SQLite; dividing two integers can discard the fractional part. The stored price remains unchanged.

Where to try it

Run this example in the SQL playground. The lesson link loads the shop tables and sample rows before this query. Select Run when you are ready. Every run starts fresh, so your changes do not affect the website or a previous run.

SQLite sample data · MySQL / MariaDB sample data · See the sample tables

A worked example

SQLite

SELECT name, price_cents / 100.0 AS price
FROM products
ORDER BY product_id;

Read the result

The price column contains 2.5, 18.0, 9.0, and 4.5 in product order. A client can format these as currency. Formatting for display and storing a precise monetary amount are different jobs.

Try one change

Add stock to the result and calculate stock * price_cents AS inventory_cents. Keep the original stored columns unchanged.