SELECT
Overview
The SELECT statement is the most fundamental SQL command. It is used to retrieve data from one or more tables in a database. Almost every SQL query begins with SELECT.
SELECT allows you to specify which columns you want to retrieve, from which tables, and optionally filter and sort the results.
Basic Syntax
The basic syntax of the SELECT statement is:
SELECT column1, column2, ...
FROM table_name;
Selecting All Columns
To retrieve all columns from a table, use the asterisk (*) wildcard:
SELECT * FROM Customers;
Result: Returns all columns and all rows from the Customers table.
Selecting Specific Columns
To retrieve only specific columns, list them separated by commas:
SELECT customer_id, name, email
FROM Customers;
Result: Returns only the customer_id, name, and email columns from all rows.
Examples with Sample Data
Let's see how SELECT works with a sample Customers table:
Customers Table:
┌─────────────┬─────────────┬──────────────────┬────────────┐
│ customer_id │ name │ email │ city │
├─────────────┼─────────────┼──────────────────┼────────────┤
│ 1 │ John Doe │ john@example.com │ New York │
│ 2 │ Jane Smith │ jane@example.com │ Los Angeles│
│ 3 │ Bob Johnson │ bob@example.com │ Chicago │
└─────────────┴─────────────┴──────────────────┴────────────┘
Example 1: Select All Columns
SELECT * FROM Customers;
┌─────────────┬─────────────┬──────────────────┬────────────┐
│ customer_id │ name │ email │ city │
├─────────────┼─────────────┼──────────────────┼────────────┤
│ 1 │ John Doe │ john@example.com │ New York │
│ 2 │ Jane Smith │ jane@example.com │ Los Angeles│
│ 3 │ Bob Johnson │ bob@example.com │ Chicago │
└─────────────┴─────────────┴──────────────────┴────────────┘
Example 2: Select Specific Columns
SELECT name, email FROM Customers;
┌─────────────┬──────────────────┐
│ name │ email │
├─────────────┼──────────────────┤
│ John Doe │ john@example.com │
│ Jane Smith │ jane@example.com │
│ Bob Johnson │ bob@example.com │
└─────────────┴──────────────────┘
SELECT with WHERE Clause
You can filter results using the WHERE clause (covered in detail in the WHERE tutorial):
SELECT name, email
FROM Customers
WHERE city = 'New York';
SELECT with ORDER BY
You can sort results using ORDER BY (covered in detail in the ORDER BY tutorial):
SELECT name, email
FROM Customers
ORDER BY name ASC;
Calculated Columns
You can perform calculations in SELECT statements:
-- Calculate total price (quantity * price)
SELECT product_name, quantity, price, quantity * price AS total
FROM OrderItems;
-- String concatenation (syntax varies by database)
SELECT first_name || ' ' || last_name AS full_name
FROM Customers;
Column Aliases
Use aliases (with the AS keyword) to rename columns in the result set:
SELECT
customer_id AS id,
name AS customer_name,
email AS email_address
FROM Customers;
SELECT with Aggregate Functions
You can use aggregate functions like COUNT, SUM, AVG, MIN, MAX:
-- Count total customers
SELECT COUNT(*) AS total_customers FROM Customers;
-- Average price
SELECT AVG(price) AS average_price FROM Products;
-- Maximum and minimum values
SELECT MAX(price) AS max_price, MIN(price) AS min_price
FROM Products;
SELECT from Multiple Tables
You can select data from multiple tables using JOINs (covered in detail in the Joins tutorials):
SELECT
c.name AS customer_name,
o.order_date,
o.total_amount
FROM Customers c
INNER JOIN Orders o ON c.customer_id = o.customer_id;
SELECT DISTINCT
Use DISTINCT to eliminate duplicate rows from results (covered in detail in SELECT DISTINCT):
SELECT DISTINCT city FROM Customers;
Best Practices
- Select only needed columns: Avoid
SELECT *in production code for better performance - Use meaningful aliases: Make result columns more readable
- Test with small datasets first: Verify logic before running on large tables
- Use WHERE to filter: Reduce data transfer by filtering at the database level
- Format for readability: Use proper indentation for complex queries
Common Use Cases
- Retrieving customer information for a report
- Listing products in a catalog
- Generating summaries and statistics
- Creating data exports
- Displaying data in applications
Next Steps
Now that you understand SELECT, learn how to filter results with WHERE clause, eliminate duplicates with SELECT DISTINCT, or sort results with ORDER BY.