thecodex.expert · The Codex Family of Knowledge
SQL

SELECT Basics

Every SQL query, however complex, starts from the same two words: SELECT what you want, FROM where it lives.

SQL:2016 / ANSI SELECT ... FROM ... Last verified:
Canonical Definition

A SELECT statement names the columns to return, and FROM names the table to read them from. SELECT * returns every column, though naming columns explicitly is generally preferred in real code since it's clearer and more resilient to schema changes. Column and table aliases (AS) rename output columns or shorten long table references for readability.

SELECT specific columns, or everything with *

Naming columns explicitly is the idiomatic choice for real applications — SELECT * is convenient for quick exploration but can silently break code if the table's columns change later.

SQLselect_basics.sql
SELECT name, email FROM users;    -- explicit columns, preferred

SELECT * FROM users;               -- every column, convenient but fragile

Aliasing with AS

AS renames a column or table in the output, useful for computed columns, clarity, or shortening a long table name used repeatedly in a query.

SQLaliasing.sql
SELECT
    first_name AS name,
    price * quantity AS total_cost
FROM orders AS o;

DISTINCT: unique values only

DISTINCT removes duplicate rows from the result — applied across all selected columns together, not per-column.

SQLdistinct.sql
SELECT DISTINCT country FROM customers;
-- returns each country name once, no matter how many customers share it

Sources

1
PostgreSQL Global Development Group. PostgreSQL Documentation, "SELECT", postgresql.org/docs/current/sql-select.html.