SELECT Statement
Retrieve data from database tables
The SELECT statement is the foundation of SQL. It retrieves data from one or more tables and is by far the most commonly used SQL command.
Basic Syntax
SELECT column1, column2, ... FROM table_name;
Select All Columns
Use * to select all columns:
-- Get everything from the users table SELECT * FROM users; -- Result: all columns and all rows
Select Specific Columns
List the columns you need, separated by commas:
-- Get only name and email SELECT name, email FROM users; -- Column order in results matches your SELECT SELECT email, name FROM users;
Column Aliases
Rename columns in the output using AS:
-- Rename columns in output
SELECT
name AS full_name,
email AS contact_email,
created_at AS signup_date
FROM users;
-- AS is optional (but recommended for clarity)
SELECT name full_name FROM users;
-- Use quotes for aliases with spaces
SELECT name AS "Full Name" FROM users;Expressions & Calculations
SELECT can do more than just retrieve columns:
-- Math operations
SELECT
product_name,
price,
quantity,
price * quantity AS total
FROM order_items;
-- String concatenation (SQLite/PostgreSQL)
SELECT first_name || ' ' || last_name AS full_name FROM users;
-- String concatenation (MySQL)
SELECT CONCAT(first_name, ' ', last_name) AS full_name FROM users;Literal Values
-- Add constant values to results
SELECT
name,
email,
'Active' AS status,
2024 AS year
FROM users;Best Practice: Avoid SELECT * in production code. It's slower and can break if table structure changes. Always specify the columns you need.