Guide/Beginner/Your First SELECT

Your First SELECT

The anatomy of a SELECT statement and how to start reading data.

Every SQL query that reads data starts with SELECT. It is the most important word in the language, and also the most forgiving one to learn. The basic form is simple enough to write in thirty seconds, yet the same structure supports queries of enormous complexity.

The basic form

SELECT column1, column2
FROM table_name;

You name the columns you want. You name the table they live in. The database returns every row from that table, with only those columns included. The order of execution matters here: the database finds the table first, then figures out which columns to include in the output.

To return every column without naming them, use *:

SELECT *
FROM employees;

The asterisk is useful for exploration when you are not sure what columns a table has. In production code, though, you should name your columns explicitly. This makes your intent clear, avoids surprises when the table gains new columns, and helps the database avoid fetching data you do not need.

Aliases

You can rename any column in the output using AS. The alias exists only in the result set and does not change anything about the underlying table.

SELECT
  first_name AS name,
  salary * 12 AS annual_salary
FROM employees;

Aliases become especially useful when your SELECT includes calculations. Without an alias, the column header might be something like ?column? or salary * 12, which is unhelpful. With an alias, the output is self-explanatory.

You can also alias tables, which becomes important with joins. More on that later.

Expressions in SELECT

You are not limited to column names in a SELECT clause. You can include any valid expression: arithmetic, string operations, function calls, and more.

SELECT
  first_name,
  last_name,
  first_name || ' ' || last_name AS full_name,
  salary * 1.10 AS salary_with_raise,
  UPPER(department) AS department
FROM employees;

The || operator concatenates strings in PostgreSQL. If you come from MySQL, you may be used to CONCAT() instead. Both work in practice, though CONCAT() is more portable across database systems.

The key point is that expressions are evaluated for every row. If you multiply salary * 1.10, the database computes that for each employee row independently.

DISTINCT

Adding DISTINCT after SELECT removes duplicate rows from the result:

SELECT DISTINCT department
FROM employees;

Without DISTINCT, this query would return one row per employee, with many repeated department names. With DISTINCT, each department appears exactly once. This is useful for exploring what values a column actually contains.

Note that DISTINCT applies to the combination of all selected columns, not just the first one:

SELECT DISTINCT department, title
FROM employees;

This returns each unique (department, title) pair, not each unique department alone.

Column order and semicolons

The columns in your result appear in the order you list them in SELECT, which may differ from the order they appear in the table. This is purely cosmetic but worth knowing.

Every SQL statement ends with a semicolon. In most clients, the semicolon is what tells the parser where one statement ends and the next begins. It is technically optional when you are running a single statement, but developing the habit early prevents a confusing class of errors later.

From here, everything else in SQL is about adding clauses to this basic shape: WHERE to filter rows, ORDER BY to sort, GROUP BY to summarize, and JOIN to combine tables. The structure of SELECT ... FROM ... never changes.