SELECT — Retrieving Data from a Table
7 min read
The SELECT Statement
SELECT is the most common SQL statement. It reads data from one or more tables and returns a result set. Every SELECT query must name at least one table using FROM.
sql
-- Retrieve every column for every student
SELECT * FROM students;
-- Retrieve only specific columns
SELECT id, name, city FROM students;
-- Column aliases make output easier to read
SELECT name AS student_name, age AS student_age FROM students;Choosing Columns Wisely
Using SELECT * is convenient for exploration but avoid it in production code. Listing columns explicitly makes queries self-documenting, prevents breakage when columns are added, and reduces the amount of data transferred from the database.
SQL keywords are case-insensitive. SELECT, select, and Select all work. The convention is to write keywords in uppercase and table/column names in lowercase for readability.
Ready to test yourself?
Practice SQL— quiz & coding exercises