Lesson 3 โ€ข Beginner

    SELECT Statement

    Master the most important SQL command โ€” retrieving data from tables with SELECT, DISTINCT, aliases, and LIMIT.

    ๐ŸŽฏ What You'll Learn

    • Select all columns vs. specific columns
    • Create readable aliases with AS
    • Remove duplicates with DISTINCT
    • Limit results with LIMIT and OFFSET
    • Create calculated columns in your output

    Basic SELECT Syntax

    The SELECT statement retrieves data from a table. It's the command you'll use 90% of the time.

    ๐Ÿ›’ Real-World Analogy

    SELECT is like ordering from a menu. SELECT * means "give me everything." SELECT name, price means "just tell me the dish name and how much it costs."

    SELECT * returns all columns. It's great for exploring, but in production code, always specify the columns you need โ€” it's faster and clearer.

    โš ๏ธ Common Mistake

    Using SELECT * in production applications. It fetches unnecessary data, slows queries, and breaks when columns are added or removed.

    SELECT Basics

    Try selecting all columns, specific columns, and using aliases

    Try it Yourself ยป
    SQL
    -- Select ALL columns from a table
    SELECT * FROM products;
    
    -- Select specific columns only
    SELECT product_name, price FROM products;
    
    -- Use column aliases for cleaner output
    SELECT 
        product_name AS "Product",
        price AS "Price ($)",
        category AS "Category"
    FROM products;

    DISTINCT โ€” Remove Duplicates

    DISTINCT removes duplicate rows from your results. It compares all selected columns โ€” a row is only a duplicate if every selected column matches.

    ๐Ÿ’ก Pro Tip

    DISTINCT can be slow on large tables because the database must compare every row. Use it only when you actually need unique values.

    DISTINCT in Action

    Remove duplicate values from your query results

    Try it Yourself ยป
    SQL
    -- Remove duplicate rows with DISTINCT
    SELECT DISTINCT category FROM products;
    
    -- Count unique values
    SELECT DISTINCT department, job_title
    FROM employees;
    
    -- DISTINCT applies to the entire row
    -- Both columns must match for a row to be "duplicate"

    LIMIT & OFFSET โ€” Control Result Size

    LIMIT restricts how many rows are returned. OFFSET skips a number of rows โ€” perfect for pagination (page 1, page 2, etc.).

    ๐Ÿ“– Analogy

    LIMIT 10 OFFSET 20 is like saying: "Skip the first 20 search results and show me results 21โ€“30."

    LIMIT & Pagination

    Control how many rows you get back

    Try it Yourself ยป
    SQL
    -- Limit results to first N rows
    SELECT * FROM products LIMIT 10;
    
    -- Pagination: skip first 10, get next 10
    SELECT * FROM products LIMIT 10 OFFSET 10;
    
    -- Combine with column selection
    SELECT product_name, price
    FROM products
    LIMIT 5;
    
    -- Useful for previewing large tables
    -- Always use LIMIT when exploring unknown data!

    Calculated Columns

    Create new columns with math and string operations

    Try it Yourself ยป
    SQL
    -- Calculated columns
    SELECT 
        product_name,
        price,
        price * 0.9 AS discounted_price,
        price * 1.08 AS price_with_tax
    FROM products;
    
    -- String concatenation (varies by database)
    -- MySQL:
    SELECT CONCAT(first_name, ' ', last_name) AS full_name
    FROM employees;
    
    -- PostgreSQL:
    -- SELECT first_name || ' ' || last_name AS full_name
    -- FROM employees;

    ๐Ÿ“˜ Quick Reference

    SyntaxPurpose
    SELECT *All columns
    SELECT col1, col2Specific columns
    AS aliasRename column in output
    DISTINCTRemove duplicates
    LIMIT nReturn only n rows
    OFFSET nSkip first n rows

    ๐ŸŽ‰ Lesson Complete!

    You can now retrieve data with SELECT, remove duplicates, use aliases, and paginate results. Next, you'll learn to filter data with the powerful WHERE clause!

    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 Policy โ€ข Terms of Service