Chapter 5: SQL SELECT Statement
The SELECT statement is the foundational command of relational databases. Learn how to query tables, choose specific columns, use wildcards, calculate arithmetic expressions on the fly, and inspect output datasets without altering underlying data.
Read-Only Query
Safe Data InspectionSELECT reads and projects records from storage without modifying, locking, or mutating disk records.
Column Selection
Targeted Projection
Specify the exact attributes needed in any custom sequence to reduce network overhead and memory consumption.
Wildcard Asterisk
Rapid Exploration
Use SELECT * for quick ad-hoc table inspection, and list columns explicitly in production systems.
Inline Math
Computed Columns
Calculate discounts, tax amounts, unit conversions, and tableless math expressions in real-time.
5.1 What Does the SELECT Statement Do?
The SELECT statement is the primary query tool in SQL. It falls into the DQL (Data Query Language) category. Its single responsibility is to read stored records from one or more database tables and project them as a two-dimensional result dataset.
You can safely run SELECT queries as many times as you like. It will never overwrite, alter, insert, or delete any record stored in your database.
SELECT * FROM table_name;
For example, to retrieve all records and columns from the Customers table:
SELECT * FROM Customers;
5.2 Visual: Which Columns Does SELECT Pick?
Step through the visual lifecycle below to observe how the SQL query engine filters and projects columns from the Customers table:
5.3 Selecting Specific Columns
In production software and professional data analysis, you should specify only the exact columns you intend to use. Separate each column name with a comma:
SELECT CustomerName, City
FROM Customers;
| CustomerName | City |
|---|---|
| Aarav Sharma | Mumbai |
| Priya Nair | Kochi |
| Liam Carter | Toronto |
| Sofia Rossi | Milan |
| ... 4 more rows (8 total) | |
SELECT ProductName, Price
FROM Products;
| ProductName | Price |
|---|---|
| Wireless Mouse | ₹799 |
| Mechanical Keyboard | ₹3,499 |
| USB-C Hub | ₹1,999 |
| 27-inch Monitor | ₹15,999 |
| ... 4 more rows (8 total) | |
Notice: The column order in your output table matches the exact sequence specified in your SELECT clause. Requesting SELECT City, CustomerName yields City first!
5.4 Selecting All Columns with the Wildcard (*)
The asterisk (*) is the universal SQL wildcard symbol meaning "all columns in their physical table schema order".
SELECT * FROM Customers;
- Quick schema exploration and inspecting new database tables.
- Debugging query filters in local development sandboxes.
- One-off ad-hoc data analysis scripts and interactive notebooks.
- Transfers unneeded columns across network connections, slowing APIs.
- Cannot leverage index-only scans (covering indexes) efficiently.
- Adding or dropping columns later can break client code positional bindings.
| Attribute | SELECT * (Wildcard) |
SELECT col1, col2 (Explicit Columns) |
|---|---|---|
| Network Transfer | Transfers every column in table (high bandwidth) | Transfers only requested attributes (lightweight & fast) |
| Schema Change Resilience | Fragile; column additions alter positional mapping | Robust; application code only receives expected fields |
| Database Index Utilization | Rarely uses index-only covering scans | Can utilize high-speed covering indexes directly |
| Best Use Case | Interactive exploratory data analysis & debugging | Production APIs, ETL pipelines & reporting dashboards |
5.5 Expressions and Calculations in SELECT
SQL can compute arithmetic calculations and string expressions directly within the SELECT clause. You can perform calculations using numeric columns, constants, and math operators (+, -, *, /):
-- Calculate a 10% promotional discount on all products
SELECT ProductName, Price, Price * 0.9
FROM Products;
Preview: Column Aliases with AS (Chapter 16)
Notice that the computed column displays the header Price * 0.9. In Chapter 16, you will learn how to assign a friendly label using the AS keyword, such as SELECT Price * 0.9 AS DiscountedPrice.
You can also execute tableless calculations where no database table is queried:
SELECT 2 + 3;
SELECT 2 + 3; runs natively across SQLite, MySQL, PostgreSQL, and Microsoft SQL Server. Older Oracle Database systems require specifying the dummy table FROM DUAL (e.g. SELECT 2 + 3 FROM DUAL;).
5.6 Upcoming Capabilities: You Will Learn These Soon
Eliminate duplicate values: SELECT DISTINCT City FROM Customers;
Filter rows matching conditions: WHERE Country = 'India'
Sort resulting records: ORDER BY Price DESC
Transform text in queries: SELECT UPPER(CustomerName)
5.7 Interactive Practice: Try It Yourself
Execute real queries against our in-browser SQLite database (Customers, Products, Orders):
Customers (CustomerID, CustomerName, City, Country) •
Products (ProductID, ProductName, Category, Price) •
Orders (OrderID, CustomerID, ProductID, Quantity, OrderDate)
5.8 Common SELECT Mistakes & Real Error Messages
Below are 6 real SQLite errors beginners encounter with SELECT and how to correct them:
-- ❌ Error: no such column: CustomerNam SELECT CustomerNam FROM Customers;
-- ✅ Fixed: SELECT CustomerName FROM Customers;
-- ❌ Error: no such table: Costumers SELECT * FROM Costumers;
-- ✅ Fixed: SELECT * FROM Customers;
Omitted commas are silently interpreted as column aliases!
-- ❌ Mistake: Returns names under column header "City" SELECT CustomerName City FROM Customers;
-- ✅ Fixed: SELECT CustomerName, City FROM Customers;
-- ❌ Error: near "FROM": syntax error SELECT CustomerName, City, FROM Customers;
-- ✅ Fixed: SELECT CustomerName, City FROM Customers;
-- ❌ Error: no such column: CustomerName SELECT CustomerName;
-- ✅ Fixed: SELECT CustomerName FROM Customers;
Single quotes produce a literal string repeated for every row!
-- ❌ Mistake: Prints the text 'CustomerName' 8 times SELECT 'CustomerName' FROM Customers;
-- ✅ Fixed: SELECT CustomerName FROM Customers;
5.9 Hands-On Practice Exercises
Solve each challenge using the sandbox above, or click "Insert into Sandbox":
Write a query to retrieve every column and row from the Products catalog.
SELECT * FROM Products;
Open in Playground »
Retrieve only the ProductName and Category for all items in the Products table.
SELECT ProductName, Category FROM Products;
Open in Playground »
Select OrderID, Quantity, and double the quantity (Quantity * 2) from the Orders table.
SELECT OrderID, Quantity, Quantity * 2 FROM Orders;
Open in Playground »
5.11 Key Takeaways
SELECTreads data from tables into a result set without altering or deleting any underlying records.- List specific column names separated by commas (
SELECT CustomerName, City) to return only necessary data. - Output column order strictly reflects the order written in the
SELECTclause. - The asterisk (
*) returns every column; use it for exploring tables, but prefer named columns in production. - SQL can compute arithmetic calculations (
Price * 0.9) and tableless expressions (SELECT 2 + 3;).