Skip to lesson

learningsql.org / basics / 02-selecting-data · lesson 2 of 25

TL;DR

Master the SQL SELECT statement to retrieve specific data from your database. Learn column aliases, DISTINCT, LIMIT, and expressions.

Key concepts

  • SQL SELECT statement
  • SQL query basics
  • SELECT DISTINCT
  • SQL column aliases

Selecting Data

TL;DRSELECT column FROM table retrieves specific columns; SELECT * returns all of them. Use AS to rename columns in results, arithmetic expressions to compute values inline, DISTINCT to remove duplicates, and LIMIT n to cap the number of rows returned. Every SQL query you write starts with SELECT, so getting these basics right pays off in every lesson that follows.

In this lesson, we'll dive deep into the SELECT statement - the most fundamental and frequently used SQL command. You'll learn how to retrieve exactly the data you need from your database.

The SELECT Statement

The SELECT statement is used to query data from a database. At its simplest, it retrieves columns from a table.

Basic Syntax

SELECT column1, column2, column3
FROM table_name;

Selecting All Columns

Use the asterisk (*) to select all columns:

-- Select all columns from the employees table
SELECT * FROM employees;

Selecting Specific Columns

For better performance and clarity, select only the columns you need:

-- Select only name and email
SELECT name, email FROM employees;

-- Select with custom column order
SELECT salary, name, department FROM employees;

Column Aliases

Aliases let you rename columns in your result set, making output more readable:

-- Create a sample employees table
CREATE TABLE employees (
    id INTEGER PRIMARY KEY,
    first_name TEXT NOT NULL,
    last_name TEXT NOT NULL,
    salary INTEGER,
    department TEXT
);

-- Insert sample data
INSERT INTO employees (id, first_name, last_name, salary, department)
VALUES
    (1, 'John', 'Doe', 60000, 'Engineering'),
    (2, 'Jane', 'Smith', 65000, 'Marketing'),
    (3, 'Mike', 'Johnson', 55000, 'Engineering'),
    (4, 'Sarah', 'Williams', 70000, 'Sales');

-- Use aliases to create friendly column names
SELECT
    first_name || ' ' || last_name AS full_name,
    salary AS annual_salary,
    department AS dept
FROM employees;

Expressions and Calculations

SQL can perform calculations directly in your queries:

-- Create a products table
CREATE TABLE products (
    id INTEGER PRIMARY KEY,
    name TEXT NOT NULL,
    price DECIMAL(10, 2),
    quantity INTEGER
);

-- Insert products
INSERT INTO products (id, name, price, quantity)
VALUES
    (1, 'Laptop', 999.99, 10),
    (2, 'Mouse', 24.99, 50),
    (3, 'Keyboard', 79.99, 30),
    (4, 'Monitor', 299.99, 15);

-- Calculate total inventory value
SELECT
    name,
    price,
    quantity,
    price * quantity AS total_value
FROM products;

DISTINCT Keyword

Remove duplicate values from your results:

-- Create an orders table
CREATE TABLE orders (
    id INTEGER PRIMARY KEY,
    customer_name TEXT,
    product TEXT,
    amount DECIMAL(10, 2)
);

-- Insert sample orders
INSERT INTO orders (id, customer_name, product, amount)
VALUES
    (1, 'Alice', 'Laptop', 999.99),
    (2, 'Bob', 'Mouse', 24.99),
    (3, 'Alice', 'Keyboard', 79.99),
    (4, 'Charlie', 'Monitor', 299.99),
    (5, 'Bob', 'Laptop', 999.99);

-- Get unique customer names
SELECT DISTINCT customer_name FROM orders;

-- Get all orders (with duplicates)
SELECT customer_name FROM orders;

Limiting Results

Control how many rows are returned:

-- Create a sales table
CREATE TABLE sales (
    id INTEGER PRIMARY KEY,
    product TEXT,
    amount DECIMAL(10, 2),
    sale_date TEXT
);

-- Insert sales data
INSERT INTO sales (id, product, amount, sale_date)
VALUES
    (1, 'Laptop', 999.99, '2025-01-01'),
    (2, 'Mouse', 24.99, '2025-01-02'),
    (3, 'Keyboard', 79.99, '2025-01-03'),
    (4, 'Monitor', 299.99, '2025-01-04'),
    (5, 'Headphones', 149.99, '2025-01-05');

-- Get only the first 3 sales
SELECT * FROM sales LIMIT 3;

-- Skip the first 2 and get the next 2 (useful for pagination)
SELECT * FROM sales LIMIT 2 OFFSET 2;

Practice Exercises

Try these exercises in the playground above:

  1. Select only product names and their total values (price * quantity)
  2. Find all unique products that have been ordered
  3. Get the top 2 most expensive products
  4. Calculate a 10% discount on all product prices and display both original and discounted prices
  5. Combine first and last names with a comma separator (HINT: use ||)

Pro Tip: When working with large datasets, always use LIMIT during development to avoid accidentally retrieving millions of rows. You can remove it once you're confident your query is correct!

Key Takeaways

  • SELECT retrieves data from database tables
  • Use * to select all columns, or list specific columns for better performance
  • Aliases make your results more readable with the AS keyword
  • DISTINCT removes duplicate values from results
  • Calculations and expressions can be performed directly in SELECT
  • LIMIT controls the number of rows returned

Next Steps

Selecting all data from a table is useful, but real-world queries need precision. The next lesson introduces WHERE clauses for filtering rows and ORDER BY for sorting results. Together with SELECT, these tools let you answer specific questions like "which orders were placed last month, sorted by total?" instead of scanning entire tables manually.

Keep exploring:

Two-tier handoff: this document is the complete reading surface. Continue learning for stateful practice, progress, and real sandbox execution.