SELECT retrieves columns from one or more tables. It is read-only and the most common statement you will write—whether in analytics, ORM debug logs, or admin consoles.
Basic SELECT
SELECT name, email
FROM customers;
SELECT *
FROM customers;List specific columns for clarity; use * only when exploring unknown schemas.
Practice: Run in SQLite, DB Fiddle, or Postgres. Create the customers/orders sample from the relational-model lesson if needed.
Column aliases
SELECT name AS customer_name,
email AS contact_email
FROM customers;AS renames output columns—useful in reports and when joining tables with duplicate column names.
Distinct rows
SELECT DISTINCT customer_id
FROM orders;DISTINCT removes duplicate values from the result set—handy before counting unique entities.
Literal expressions
SELECT name,
total,
total * 0.1 AS tax_estimate
FROM orders;SELECT can compute expressions per row without storing them—similar to spreadsheet formulas.
Important interview questions and answers
- Q: SELECT * risks?
A: Returns every column—can hide performance costs and leak sensitive fields in production logs. - Q: DISTINCT vs GROUP BY?
A: DISTINCT deduplicates projected rows; GROUP BY aggregates—often used together with COUNT.
Self-check
- Which keyword removes duplicate values from results?
- Why prefer listing column names over * in application code?
Pitfall: SELECT * in production logs may expose password hash columns—list columns explicitly.
Interview prep
- SELECT * risk?
Over-fetching columns including sensitive or large fields.
- DISTINCT purpose?
Removes duplicate rows from the result set.