CodingNic

SQL Basics

The SELECT Statement

SQL Basics 12 min read

The SELECT Statement

Objectives

By the end of this lesson, you should be able to:

  • Retrieve every column from a table
  • Retrieve specific columns, in a chosen order
  • Explain what a result set is

💡 Why this matters: SELECT is the query you’ll write more than every other kind of SQL statement combined. Everything else in this course is about controlling what a SELECT returns, precisely.

⚠️ A note on verification: every statement and result in this lesson was run against a real, live PostgreSQL 18 database.

A Sample Table to Practice On

The rest of this module needs an actual table to query. CREATE TABLE and INSERT are covered properly in Modules 3 and 4, for now, run this once to set up a small products table:

sql
CREATE TABLE products (
    id SERIAL PRIMARY KEY,
    product_name VARCHAR(100) NOT NULL,
    category VARCHAR(50) NOT NULL,
    price NUMERIC(8,2) NOT NULL,
    in_stock BOOLEAN NOT NULL DEFAULT true
);

INSERT INTO products (product_name, category, price, in_stock) VALUES
    ('Wireless Mouse', 'Electronics', 24.99, true),
    ('Mechanical Keyboard', 'Electronics', 89.99, true),
    ('USB-C Cable', 'Electronics', 9.99, true),
    ('Desk Lamp', 'Home', 34.50, true),
    ('Standing Desk', 'Home', 349.00, false),
    ('Notebook', 'Office', 4.99, true),
    ('Ballpoint Pen Pack', 'Office', 6.50, true),
    ('Water Bottle', 'Home', 15.00, true),
    ('Webcam', 'Electronics', 49.99, false),
    ('Desk Organizer', 'Office', 12.25, true);

Don’t worry about understanding every line yet, that’s exactly what the next two modules cover in full.

Selecting Every Column

sql
SELECT * FROM products;

* means “every column.” This returns all 10 rows, every column, exactly as stored:

text
 id |    product_name     |  category   | price  | in_stock
----+----------------------+-------------+--------+----------
  1 | Wireless Mouse       | Electronics |  24.99 | t
  2 | Mechanical Keyboard  | Electronics |  89.99 | t
  3 | USB-C Cable          | Electronics |   9.99 | t
  4 | Desk Lamp            | Home        |  34.50 | t
  5 | Standing Desk        | Home        | 349.00 | f
  6 | Notebook             | Office      |   4.99 | t
  7 | Ballpoint Pen Pack   | Office      |   6.50 | t
  8 | Water Bottle         | Home        |  15.00 | t
  9 | Webcam               | Electronics |  49.99 | f
 10 | Desk Organizer       | Office      |  12.25 | t

This set of returned rows and columns is called a result set. It looks like a table, but it isn’t one, it’s freshly generated by the query, and disappears once you’re done looking at it. The actual products table is completely unaffected by running a SELECT against it.

Selecting Specific Columns

Usually you only care about a few columns. List exactly the ones you want, separated by commas, instead of *:

sql
SELECT product_name, price FROM products;
text
    product_name      | price
-----------------------+--------
 Wireless Mouse        |  24.99
 Mechanical Keyboard   |  89.99
 USB-C Cable           |   9.99
 Desk Lamp             |  34.50
 Standing Desk         | 349.00
 Notebook              |   4.99
 Ballpoint Pen Pack    |   6.50
 Water Bottle          |  15.00
 Webcam                |  49.99
 Desk Organizer        |  12.25

The order of columns in the query is the order they come back in. SELECT price, product_name FROM products; would return the same data with the two columns swapped.

Try It

  1. Run the setup script above to create and populate products.
  2. Write a query that selects every column from products.
  3. Write a query that selects only product_name and category.
  4. Write a query that selects price, product_name, and in_stock, in that column order.

Recap

  • SELECT * FROM table_name; returns every column of every row.
  • Listing specific column names instead of * returns only those columns, in the order listed.
  • The rows and columns a query returns are a result set, generated fresh each time, the underlying table is never changed by a SELECT.

Next lesson: DISTINCT, for removing duplicate values from a result set.