Skip to main content

    Lesson 3 • Beginner Track

    SELECT Statement

    By the end of this lesson you'll be able to pull exactly the data you want out of any table — choosing columns, renaming them, removing duplicates, paginating, and calculating new values on the fly. SELECT is the command you'll use more than any other in SQL.

    What You'll Learn

    • Select all columns vs. just the ones you need
    • Rename columns in the output with AS
    • Remove duplicate rows with DISTINCT
    • Page through results with LIMIT and OFFSET
    • Build calculated columns with maths
    • Read a query result like a database does

    Our Sample Table: products

    Every query in this lesson runs against this little products table. Keep it in mind as you read — knowing the data is half of writing good SQL.

    Result:

    idproduct_namecategorypricestock
    1Wireless MouseElectronics24.99120
    2Coffee MugKitchen9.5300
    3Mechanical KeyboardElectronics7945
    4NotebookStationery3.25500
    5Desk LampHome3280
    6USB-C CableElectronics12.99200

    1. The Basic SELECT

    A SELECT statement answers the question "show me some data". The simplest version asks for everything:

    🛒 Real-world analogy

    SELECT is like ordering from a menu. SELECT * means "bring me one of everything"; SELECT product_name, price means "just the dish name and the price, please".

    SELECT * — every column

    Return all columns and all rows.

    Try it Yourself »
    SQL
    -- Return EVERY column for EVERY row in the table
    SELECT * FROM products;
    
    -- The * is a wildcard meaning "all columns".
    -- Great for exploring, but name your columns in real apps (see below).

    Result — 6 rows:

    idproduct_namecategorypricestock
    1Wireless MouseElectronics24.99120
    2Coffee MugKitchen9.5300

    In real applications, name the columns you want instead of using * — it's faster, clearer, and won't silently change when the table gains new columns.

    Specific columns

    Ask only for product_name and price.

    Try it Yourself »
    SQL
    -- Ask for only the columns you actually need
    SELECT product_name, price
    FROM products;
    
    -- Cleaner output, faster query, and it won't break
    -- if someone adds new columns to the table later.

    Result:

    product_nameprice
    Wireless Mouse24.99
    Coffee Mug9.5
    Mechanical Keyboard79

    Your Turn: pick the columns

    Fill in the blanks to list the category and stock of every product. The expected result is in the comments so you can check yourself.

    🎯 Your Turn: category & stock

    Replace the ___ blanks with the column names.

    Try it Yourself »
    SQL
    -- 🎯 YOUR TURN — fill in the two blanks, then press "Try it Yourself"
    -- Goal: list the category and stock level of every product.
    
    SELECT ___, ___      -- 👉 put the column names here: category, stock
    FROM products;
    
    -- ✅ Expected result: 6 rows, two columns (category, stock),
    --    e.g. Electronics | 120 ,  Kitchen | 300 , ...

    2. Renaming Columns with AS

    The AS keyword gives a column a friendly name in the result. The underlying table is never changed — the alias only exists for this one query. It becomes essential the moment you start calculating columns.

    Column aliases

    Rename columns for readable output.

    Try it Yourself »
    SQL
    -- AS renames a column just for this result (the table is unchanged)
    SELECT
        product_name AS "Product",
        price        AS "Price (USD)"
    FROM products;
    
    -- Aliases make headings readable and are essential
    -- once you start using calculated columns.

    Result:

    ProductPrice (USD)
    Wireless Mouse24.99
    Coffee Mug9.5

    3. DISTINCT — Remove Duplicates

    DISTINCT collapses duplicate rows into one. Our table has three Electronics products, but asking for distinct categories returns each category only once.

    DISTINCT categories

    Collapse duplicates to unique values.

    Try it Yourself »
    SQL
    -- DISTINCT removes duplicate rows from the result
    SELECT DISTINCT category
    FROM products;
    
    -- A row counts as a duplicate only if EVERY selected
    -- column matches, so DISTINCT works on the whole row.

    Result — 4 unique rows:

    category
    Electronics
    Kitchen
    Stationery
    Home

    Your Turn: unique categories

    One blank this time — add the keyword that removes duplicates.

    🎯 Your Turn: DISTINCT

    Return each category exactly once.

    Try it Yourself »
    SQL
    -- 🎯 YOUR TURN — fill in the blank to list each category only once.
    
    SELECT ___ category   -- 👉 the keyword that removes duplicates
    FROM products;
    
    -- ✅ Expected result (4 rows): Electronics, Kitchen, Stationery, Home

    4. LIMIT & OFFSET — Control How Many Rows

    LIMIT caps the number of rows returned; OFFSET skips rows first. Together they power "page 1, page 2, page 3" pagination you see on every website.

    LIMIT 3 OFFSET 3 is "skip the first 3 results, then give me the next 3" — exactly page 2 of a 3-per-page list.

    LIMIT & pagination

    Cap and page through rows.

    Try it Yourself »
    SQL
    -- LIMIT caps how many rows come back
    SELECT product_name, price
    FROM products
    LIMIT 3;
    
    -- OFFSET skips rows first — this is how pagination works:
    SELECT product_name, price
    FROM products
    LIMIT 3 OFFSET 3;   -- skip the first 3, return the next 3

    5. Calculated Columns

    A SELECT can compute brand-new columns from existing ones using maths or functions. The original data stays untouched — the new column lives only in the result.

    Calculated columns

    Add a price_with_tax column on the fly.

    Try it Yourself »
    SQL
    -- You can compute new columns on the fly
    SELECT
        product_name,
        price,
        price * 1.20 AS price_with_tax   -- add 20% tax
    FROM products;
    
    -- The products table is never changed — the calculation
    -- only exists in this query's result.

    Result:

    product_namepriceprice_with_tax
    Wireless Mouse24.9929.988
    Coffee Mug9.511.4

    Common Errors (and the fix)

    • "no such column: productname" — column names must match exactly (and SQL has no spaces in names). It's product_name, not productname or "product name".
    • Forgetting FROM: SELECT product_name; errors — every column query needs a table: SELECT product_name FROM products;
    • SELECT * in real apps: it works, but fetches columns you don't need and breaks code when the table changes. Name your columns.
    • Quotes mix-up: use single quotes for text values ('Electronics'); double quotes are for identifiers/aliases in most databases. '".
    • Missing semicolon: most tools need ; to end a statement, especially when running several at once.

    📘 Quick Reference

    SyntaxPurpose
    SELECT *Every column
    SELECT col1, col2Only these columns
    AS aliasRename a column in the output
    DISTINCTRemove duplicate rows
    LIMIT nReturn at most n rows
    OFFSET nSkip the first n rows
    price * 1.2 AS xA calculated column

    Frequently Asked Questions

    Q: Does the order of columns in SELECT matter?

    Yes — the result shows columns in the exact order you list them. SELECT price, product_name puts price first.

    Q: Is SQL case-sensitive?

    Keywords (SELECT, FROM) are not — but it's convention to UPPERCASE them. Table and column names can be case-sensitive depending on the database, so match them exactly.

    Q: Why use AS if the table doesn't change?

    Readability and necessity: calculated columns have no name until you give them one, and clear headings make results far easier to read.

    Q: Will DISTINCT change my data?

    No. SELECT only reads — it never modifies the table. DISTINCT just de-duplicates the result.

    Mini-Challenge: Half-Price Preview

    Put it all together — a brief, a blank canvas, and the expected result in the comments. Write it, then copy it into a playground to confirm.

    🎯 Mini-Challenge

    product_name + a half_price calculated column, first 4 rows.

    Try it Yourself »
    SQL
    -- 🎯 MINI-CHALLENGE
    -- Using ONLY what this lesson covered (SELECT, AS, calculated columns, LIMIT):
    --   1. Show product_name and a column called half_price (price * 0.5)
    --   2. Return just the first 4 products
    --
    -- ✅ Expected: 4 rows, columns product_name and half_price
    --    e.g. Wireless Mouse | 12.495 , Coffee Mug | 4.75 , ...
    
    -- your query here

    🎉 Lesson Complete

    • SELECT * grabs everything; naming columns is better in real apps
    • AS renames columns in the output only
    • DISTINCT removes duplicate rows from the result
    • LIMIT + OFFSET cap and paginate results
    • ✅ You can compute calculated columns without changing the table
    • Next: the WHERE clause — return only the rows that match a condition

    Sign up for free to track which lessons you've completed and get learning reminders.

    Previous

    Cookie & Privacy Settings

    We use cookies to improve your experience, analyze traffic, and show personalized ads. You can manage your preferences below.

    By clicking "Accept All", you consent to our use of cookies for analytics and personalized advertising. You can customize your preferences or reject non-essential cookies.

    Privacy PolicyTerms of Service