Module 01 · lesson 2

Selecting only the columns you need

The founder needs a price list — just the name and price of each product. Dates and stock levels are unnecessary here. Pulling the whole table for two columns is wasteful.

List the columns you need

Instead of *, name the columns you need, separated by commas — and in exactly the order you want to see them:

SELECT category, stock
FROM products;

This query returns only two columns — category and stock.

Projection

Selecting specific columns is called projection. In real queries you almost always do this — it's:

  • faster — the database doesn't drag along extra data;
  • more readable — it's immediately clear what's needed;
  • more reliable — the query won't "drift" if new columns are added to the table.

So an explicit list is almost always better than *.

Your turn

Put together a price list: output the name and price of each product.

Summary

  • A comma-separated list of columns returns only what you need — in the order you give. This is projection.
  • The column order in SELECT = the order in the result.
  • An explicit list beats * (faster, more readable, more reliable).