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:

Basic SELECT Syntax
SELECT column1, column2, ...
FROM table_name;

Selecting All Columns

To retrieve all columns from a table, use the asterisk (*) wildcard:

Select All Columns
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 Specific Columns
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:

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

Query
SELECT * FROM Customers;
Result
┌─────────────┬─────────────┬──────────────────┬────────────┐
│ 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

Query
SELECT name, email FROM Customers;
Result
┌─────────────┬──────────────────┐
│ 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 with Filtering
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 with Sorting
SELECT name, email
FROM Customers
ORDER BY name ASC;

Calculated Columns

You can perform calculations in SELECT statements:

Calculated Columns
-- 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:

Using Aliases
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:

Aggregate Functions
-- 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 from Multiple Tables
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 Values
SELECT DISTINCT city FROM Customers;

Best Practices

Common Use Cases

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.