SELECT
Retrieve specific columns, calculate expressions, eliminate duplicates, and query tables using the SQL SELECT statement.
Imagine opening a massive spreadsheet containing 100,000 customers. Each customer has 35 columns: address, phone number, shipping notes, credit limit, internal IDs, and signup timestamps.
If your marketing team only needs a list of customer names and email addresses to send a newsletter, downloading all 35 columns wastes memory, clogs network bandwidth, and slows down your application.
In SQL (Structured Query Language), SELECT is your surgical tool for reading data. It allows you to specify exactly which columns you want to pull out of a table.
The Anatomy of a Basic Query
A database table is organized into columns (attributes) and rows (individual records).
TABLE: customers
โโโโโโฌโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโ
โ id โ name โ email โ city โ
โโโโโโผโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโค
โ 1 โ Alice Johnson โ alice@example.com โ New York โ
โ 2 โ Bob Smith โ bob@example.com โ London โ
โ 3 โ Carlos Ruiz โ carlos@example.com โ Madrid โ
โโโโโโดโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโ
The fundamental SQL statement specifies two things:
SELECT: Which columns do you want?FROM: Which table holds those columns?
SELECT name, city
FROM customers;
Result:
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโ
โ name โ city โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโค
โ Alice Johnson โ New York โ
โ Bob Smith โ London โ
โ Carlos Ruiz โ Madrid โ
โโโโโโโโโโโโโโโโโดโโโโโโโโโโโ
Notice that SQL returns only the requested two columns, ignoring id and email.
Fetching All Columns: SELECT *
If you want to inspect every single column in a table, SQL provides the asterisk wildcard (*):
SELECT *
FROM customers;
[!TIP]
SELECT *is great for quick debugging in your database terminal. However, never useSELECT *in production backend code. If someone later adds a 50 MBprofile_picture_blobcolumn to thecustomerstable, every query in your application will suddenly become drastically slower! Always list the exact columns your application needs.
Renaming Columns with Aliases (AS)
Sometimes raw database column names are cryptic (like usr_fname or tx_dt), or you want to format them nicely for an API response. You can rename columns in the output using the AS keyword:
SELECT
name AS customer_name,
city AS location
FROM customers;
Result:
โโโโโโโโโโโโโโโโโฌโโโโโโโโโโโ
โ customer_name โ location โ
โโโโโโโโโโโโโโโโโผโโโโโโโโโโโค
โ Alice Johnson โ New York โ
โ Bob Smith โ London โ
โ Carlos Ruiz โ Madrid โ
โโโโโโโโโโโโโโโโโดโโโโโโโโโโโ
Calculating Values on the Fly
SELECT is not limited to reading raw columnsโit can also perform calculations, mathematical operations, and string transformations directly inside the database engine:
SELECT
name,
salary,
salary * 12 AS annual_income,
salary * 0.10 AS estimated_bonus
FROM employees;
The database executes the multiplication for each row in microseconds and returns annual_income as a computed column, saving your application from having to compute it in memory.
Removing Duplicates with DISTINCT
If five customers live in London and ten live in New York, running SELECT city FROM customers; prints โLondonโ five times and โNew Yorkโ ten times.
To extract only the unique values, add the DISTINCT keyword immediately after SELECT:
SELECT DISTINCT city
FROM customers;
Result:
โโโโโโโโโโโโ
โ city โ
โโโโโโโโโโโโค
โ New York โ
โ London โ
โ Madrid โ
โโโโโโโโโโโโ
Common Traps and Gotchas
1. Missing Commas Between Columns
Each column in your SELECT list must be separated by a comma:
-- WRONG: Causes a syntax error or accidentally aliases 'name' as 'city'!
SELECT name city FROM customers;
-- CORRECT:
SELECT name, city FROM customers;
2. Dangling Trailing Commas
Unlike Python, SQL does not allow a comma after the final column before FROM:
-- WRONG: Syntax error near 'FROM'
SELECT name, city, FROM customers;
-- CORRECT:
SELECT name, city FROM customers;
3. Forgetting the Semicolon
In database command lines and migration scripts, SQL statements require a terminating semicolon (;). Without it, the client waits indefinitely for more input.
Next Steps
Master this concept to build a rock-solid foundation. Next, proceed to select-all-columns.