E-Learning Platform
0 of 139 builtAlways freeSign in
← Database Foundations I
2.02

SELECT and FROM

On this stop
  • SELECT and FROM
  • Choosing columns

Every request to a librarian, in any library on earth, has to answer two questions before it can be acted on: which drawer, and which parts of each card do you want copied out? SQL agrees, which is why nearly every query you will ever read begins with the same two clauses. Time to learn them properly.

The anatomy of the ask

Up closeSQL
SELECT name, age
FROM students;

Two clauses, one job each:

  • FROM students — which drawer. One table, named exactly. The librarian walks to the students drawer and nowhere else.
  • SELECT name, age — which facts off each card. Columns, named in a comma-separated list. Off every card in the drawer, copy out the name and the age; leave everything else in the drawer.

The result: every student, two columns wide.

Up closeText
name   | age
-------+----
Sara   | 14
Marcus | 15
Amara  | 14

A small oddity worth noticing once: you say SELECT first, but the librarian does FROM first — find the drawer, then copy the columns. Reading queries in that order (FROM, then SELECT) makes even big ones easier to unpick.

Two bits of housekeeping from last lesson, now official: keywords like SELECT and FROM are capitalized by convention (the database doesn't insist; readers appreciate it), and the semicolon marks the end of the request — SQL's full stop.

Choosing columns is the first kindness

Why name columns at all? Because answers are for reading. The students drawer might carry a dozen columns; the person who asked "who's in the class and how old?" wants two. Naming exactly what you need keeps the tray small, the answer readable, and — on real systems shoveling millions of rows — the librarian's cart mercifully light. Ask for what you'll read. It's the difference between requesting two lines off each card and photocopying the entire drawer.

Order is yours too: SELECT age, name hands the columns back in that order. The drawer's layout doesn't change — only the tray's.

The asterisk

Sometimes you genuinely want the whole card — usually when you're exploring a table you've never met. SQL has a shortcut:

Up closeSQL
SELECT *
FROM students;

The * means "every column, in the drawer's own order." Wonderful for a first look; lazy for everything after. Habitual SELECT * is the mark of someone photocopying whole drawers to read one line — and when a table gains a new column next year, every * query quietly changes shape. Explorers use *; askers name their columns.

When the ask goes wrong

Ask for a column the table doesn't have — SELECT nme — and the librarian doesn't guess, doesn't shrug, doesn't hand you something close. You get an error naming the column it couldn't find. First time this happens it stings; the tenth time you'll be grateful. A librarian who guessed would be far more dangerous than one who asks you to spell the label correctly.

So far, though, every card in the drawer comes back. Every student, every time. The real power move is saying which rows deserve a spot on the tray — and that's the next word in your vocabulary: WHERE.