Memra

Choosing columns (projection)

◈ 2 cards

Ask for specific columns and rename them.

Pick your columns

* is handy for a peek, but real queries name the columns they want — this is called projection. List them comma-separated in the order you want them:

SELECT title, price FROM books;

You can rename a column in the output with AS (an alias). This doesn't change the table — only the result's heading:

SELECT title AS book, price AS cost FROM books;

You can also compute new columns from existing ones. price * stock gives the value of the stock on hand.

titleauthor_idgenrepriceThe Glass Forest1fiction14.99Northern Light4poetry9.99SELECT title, price — same rows, fewer columns.
Projection chooses columns and keeps every row. AS renames only the heading in the result, and price * stock adds a column that exists in no table.
NORMAL ~/memra/learn/postgresql/projection-choosing-columns utf-8 LF