SQL SELECT Basics

    Atul Kabra5 min readUpdated

    The SELECT statement is how you read data out of a SQL database. It is the single most-used statement in SQL — you will write it thousands of times. At its simplest it says "give me these columns from this table, matching these conditions, in this order." Master SELECT and you have unlocked most of day-to-day database work.

    The basic shape

    A SELECT query is built from clauses, written in this order:

    SELECT   column_list      -- which columns you want
    FROM     table_name       -- where the data lives
    WHERE    condition         -- which rows to keep
    ORDER BY column_list       -- how to sort the result
    LIMIT    n;                -- how many rows to return
    

    Let us use a student table for examples:

    -- PostgreSQL / MySQL compatible
    
    -- 1. Get every column, every row (the * means "all columns")
    SELECT * FROM student;
    
    -- 2. Get only the columns you care about (better practice than *)
    SELECT full_name, city FROM student;
    
    -- 3. Filter rows with WHERE: only students from Jalgaon
    SELECT full_name, city
    FROM student
    WHERE city = 'Jalgaon';
    
    -- 4. Sort the result: alphabetical by name
    SELECT full_name, city
    FROM student
    WHERE city = 'Jalgaon'
    ORDER BY full_name ASC;   -- ASC = ascending (default); DESC = descending
    
    -- 5. Limit how many rows come back: top 5 only
    SELECT full_name, city
    FROM student
    ORDER BY full_name
    LIMIT 5;
    

    Note on LIMIT: this works in PostgreSQL and MySQL. SQL Server uses TOP, and the SQL standard form is FETCH FIRST 5 ROWS ONLY (also supported by PostgreSQL). When portability matters, the FETCH FIRST form is the standard one.

    Filtering with WHERE

    The WHERE clause is where queries get useful. Operators you will use constantly:

    -- Comparison
    SELECT * FROM student WHERE age >= 18;
    
    -- Multiple conditions with AND / OR
    SELECT * FROM student WHERE city = 'Jalgaon' AND age >= 18;
    
    -- Range with BETWEEN (inclusive on both ends)
    SELECT * FROM student WHERE age BETWEEN 18 AND 25;
    
    -- Membership with IN
    SELECT * FROM student WHERE city IN ('Jalgaon', 'Pune', 'Nashik');
    
    -- Pattern matching with LIKE (% = any characters, _ = one character)
    SELECT * FROM student WHERE full_name LIKE 'A%';   -- names starting with A
    
    -- NULL must use IS NULL, never = NULL
    SELECT * FROM student WHERE phone IS NULL;
    

    That last one matters: NULL means "unknown," and column = NULL is never true. You must use IS NULL or IS NOT NULL.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses

    DISTINCT: removing duplicates

    DISTINCT collapses duplicate rows in the result:

    -- List each city once, even if many students live there
    SELECT DISTINCT city FROM student;
    

    Aliasing columns and expressions

    You can compute values and rename output columns with AS:

    -- Combine columns and rename the output
    SELECT
        full_name AS name,
        age,
        age + 1 AS age_next_year   -- a computed column
    FROM student;
    

    The clause that surprises everyone: execution order

    You write a query starting with SELECT, but the database executes the clauses in a different order:

    1. FROM — find the source table(s)
    2. WHERE — filter rows
    3. GROUP BY — group them (covered in GROUP BY & aggregates)
    4. HAVING — filter the groups
    5. SELECT — pick/compute the output columns
    6. ORDER BY — sort
    7. LIMIT — cut to size

    This order explains a common error: you cannot use a column alias defined in SELECT inside the WHERE clause, because WHERE runs before SELECT. You can use it in ORDER BY, which runs after.

    -- WORKS: alias used in ORDER BY (runs after SELECT)
    SELECT full_name, age + 1 AS age_next_year
    FROM student
    ORDER BY age_next_year;
    
    -- FAILS in standard SQL: alias used in WHERE (runs before SELECT)
    -- SELECT full_name, age + 1 AS age_next_year
    -- FROM student
    -- WHERE age_next_year > 20;   -- error: age_next_year unknown here
    

    Common mistakes

    • Using = NULL. It silently matches nothing. Always use IS NULL / IS NOT NULL.
    • Selecting * in production code. It pulls every column (slower, fragile if columns change). Name the columns you actually need.
    • Expecting a stable row order without ORDER BY. The database may return rows in any order, and that order can change. If order matters, say so explicitly.
    • Referencing a SELECT alias in WHERE. Because of execution order, the alias does not exist yet. Repeat the expression or use a subquery / CTE.
    • Confusing LIKE wildcards. % matches many characters, _ matches exactly one. Forgetting which is which leads to wrong matches.

    FAQ

    Is SQL case-sensitive? Keywords are not (SELECT = select). String comparisons depend on the database's collation — MySQL is often case-insensitive by default, PostgreSQL is case-sensitive. Identifiers vary too. When in doubt, be consistent and test.

    What is the difference between WHERE and HAVING? WHERE filters individual rows before grouping; HAVING filters groups after GROUP BY. See GROUP BY & aggregate functions.

    Why use column names instead of *? Naming columns is faster, makes intent clear, and protects you if someone adds or reorders columns later.

    Keep learning

    Once you can read data, learn to change it with INSERT, UPDATE & DELETE, combine tables with SQL joins, and summarise it with GROUP BY & aggregate functions.

    Want to practise writing queries against real datasets? Join the waitlist for the DBMS & SQL course at Infoplanet, Jalgaon.

    Want to learn this properly?

    Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.

    Browse courses
    Atul Kabra

    Founder, Infoplanet

    Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.

    Related guides