SELECT Statement

SQL FundamentalsDMLFree Lesson

Advertisement

The SELECT Statement

The SELECT statement retrieves data from a database. It's the most common SQL command.

💡 SELECT is how you ask your database questions. You specify WHAT columns and WHERE to find them.

Basic Syntax

SELECT column1, column2
FROM table_name;

Select All Columns

SELECT * FROM customers;
idfirst_namelast_nameemailcity
1AliceJohnsonalice@email.comNew York
2BobSmithbob@email.comLos Angeles

⚠️ SELECT * is convenient but avoid it in production. It returns ALL columns, which is slower and breaks if the schema changes.

Select Specific Columns

SELECT first_name, last_name, email FROM customers;
first_namelast_nameemail
AliceJohnsonalice@email.com
BobSmithbob@email.com

Expressions in SELECT

SELECT
    name,
    price,
    price * 1.08 AS price_with_tax,
    price * stock AS inventory_value
FROM products;
namepriceprice_with_taxinventory_value
Mouse29.9932.394498.50
Keyboard89.9997.196749.25

SELECT Without a Table

SELECT 1 + 1 AS result;                    -- 2
SELECT CURRENT_DATE AS today;              -- 2024-01-15
SELECT 'Hello, World!' AS greeting;        -- Hello, World!

Column Aliases

SELECT
    first_name AS "First Name",
    last_name AS "Last Name",
    salary * 12 AS annual_salary
FROM employees;

SQL Execution Order

SELECT ...      -- 3. Return these columns
FROM ...        -- 1. Get data from these tables
WHERE ...       -- 2. Filter rows
ORDER BY ...    -- 4. Sort the results

✏️ Exercise: Write a query to select 'name' and 'price' from 'products', and calculate a 10% discount as 'discounted_price'

See Solution


SELECT
    name,
    price,
    price * 0.9 AS discounted_price
FROM products;

✅ Key Takeaways

  1. SELECT retrieves data from a database
  2. Use specific column names instead of * in production
  3. Expressions can compute new values in your query
  4. Aliases (AS) make your output cleaner
  5. SQL executes in a specific order: FROM → WHERE → SELECT → ORDER BY

Advertisement

Need Expert SQL Help?

Get personalized SQL training or database consulting.

Advertisement