SELECT and FROM: Asking Questions
The foundation of every SQL query — what you want and where to find it
SQL is a question-answering language. Every query is a question you ask the database. SELECT defines what you want to know. FROM defines where to look. That's 80% of SQL right there.
Your first query
The simplest possible SQL queries
-- Get everything from the users table (* means "all columns") SELECT * FROM users; -- Get only specific columns SELECT name, email FROM users; -- Get just the name column SELECT name FROM users;
SELECT is the question. FROM is the filing drawer.
You walk to the filing cabinet and pull open the "customers" drawer (FROM customers). Then you say: I want to see the name and email from each file, not all the other details (SELECT name, email). SQL reads exactly like this natural language question — just with a strict word order.
Reading query results
Every SELECT query returns a result set — a temporary table with the data you asked for. Nothing is changed. SELECT never modifies data. It only reads.
SELECT name, plan FROM users — result:
name | plan --------+------ Sarah | pro Alex | free Priya | pro
SQL is case-insensitive for keywords
SELECT, select, Select — all the same. By convention, keywords are UPPERCASE and column/table names are lowercase. This makes queries easier to read at a glance. Always end a query with a semicolon (;) — it tells the database the query is complete.
Useful SELECT tricks
More SELECT patterns
-- Rename a column in the result (alias) SELECT name AS customer_name, email AS contact FROM users; -- Arithmetic in SELECT SELECT name, price, price * 1.2 AS price_with_vat FROM products; -- Combine text columns SELECT first_name || ' ' || last_name AS full_name FROM users; -- Count how many rows (returns one number) SELECT COUNT(*) FROM users;
▶ Build your first query step by step
Start with SELECT *
SELECT * FROM users;
Result
Returns all columns and all rows — every piece of data in the table.
Pick specific columns
SELECT id, name, email FROM users;
Result
Returns only those 3 columns for every row.
Count the rows
SELECT COUNT(*) FROM users;
Result
3 (or however many users you have)
Add an alias
SELECT COUNT(*) AS total_users FROM users;
Result
Same result but column is labelled "total_users" in the output.
Try this
Write a query to get the name and plan of every user in your database. Then modify it to also include the created_at column. Then write a query that just counts how many users you have.