Dataslope logoDataslope

SELECT Basics

Choosing columns, reading every row, and understanding that a query returns a brand-new result table without touching the original data.

SELECT is the workhorse of SQL — the statement you will write more than any other. We met it as a calculator; now we use it to read data from tables, which is what it was truly built for.

Choosing columns

The simplest read names the columns you want, FROM a table:

SQL
PostgreSQL 17

You asked for two columns, so you got two columns — for every row in the table. SELECT decides the columns; by default you get all the rows. Narrowing the rows is the next page's job (WHERE).

SELECT * — every column

* is shorthand for "all columns, in their defined order":

SQL
PostgreSQL 17

SELECT * is handy for exploring a table quickly. In real queries, though, it is usually better to name the exact columns you need — it is clearer, and you are not pulling back data you will not use.

A query produces a new result, not a changed table

This is a subtle but liberating idea: a SELECT never changes the underlying table. It computes a fresh result set — its own little table — and hands that back to you. The stored data sits untouched, ready for the next question.

Because of this, you can run as many SELECTs as you like, in any order, experimenting freely. You cannot "break" data by reading it.

Result sets are tables too

The output of a query has rows and columns — it is a table, just a temporary one. This is why you can build bigger queries by feeding one query's result into another, an idea we reach in the Composing Queries section.

Order of columns is up to you

The result's columns appear in the order you list them, regardless of how the table was defined. You are describing the shape of the answer, not echoing the table:

SQL
PostgreSQL 17

Removing duplicates with DISTINCT

Sometimes you only care about the distinct values in a column. SELECT DISTINCT collapses repeated rows into unique ones:

SQL
PostgreSQL 17

Five employees, but only three distinct departments. DISTINCT is perfect for answering "what are the possible values here?"

Check your understanding

QuestionSelect one

What does SELECT * return?

Only the first column of the table.

Every column of the table, in their defined order.

A count of how many rows exist.

The table's structure but none of its data.

QuestionSelect one

Running a SELECT query on a table does what to the stored data?

It permanently sorts the table.

It deletes the columns you did not select.

Nothing — SELECT reads the data and returns a new result set, leaving the stored table unchanged.

It locks the table so no one else can read it.

QuestionSelect one

A dept column contains "Sales" three times and "Eng" twice. What does SELECT DISTINCT dept return?

All five values, including the repeats.

Two rows: "Sales" and "Eng" — each distinct value once.

The single most common value only.

Nothing, because the values repeat.

Practice challenge

SQL Challenge
PostgreSQL 17
Select titles and authors

The books table has id, title, author, and year columns. Return just the title and author of every book.

On this page