GGistDev

SELECT Basics

Compose result sets from tables with SELECT, FROM, and optional clauses.

Minimal pattern

SELECT col1, col2
FROM   table_name
WHERE  condition
ORDER BY col1 ASC
LIMIT  10 OFFSET 0;

Clause order is significant: SELECTFROMWHEREGROUP BYHAVINGORDER BYLIMIT/OFFSET.

Selecting columns and aliases

SELECT u.id AS user_id, u.name, u.email
FROM users AS u;

Aliases shorten references and label columns.

Filtering rows

SELECT * FROM users WHERE email LIKE '%@example.com';

Use WHERE to limit rows early for performance.

DISTINCT and LIMIT

SELECT DISTINCT country FROM customers ORDER BY country;
SELECT * FROM orders ORDER BY created_at DESC LIMIT 20 OFFSET 0;

DISTINCT removes duplicates; LIMIT/OFFSET paginates.

Expressions and functions

SELECT id, LOWER(name) AS name_lower FROM users;

Safety tips

  • Always specify ORDER BY for deterministic ordering
  • Project only needed columns to reduce I/O

Summary

  • Learn the core SELECT shape; use aliases, filtering, and ordering consciously