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

  • Understand what the SELECT statement does and how it fits in the SQL language.

  • Retrieve specific columns or all data from a table.

  • Use expressions, functions, and computed columns in SELECT.

  • Apply aliases to rename columns for clarity.

  • Order, filter, and limit results effectively.

What is the SELECT statement?

The SELECT statement is the core of SQL. It’s used to query (read) data from one or more tables in a database.

Think of it as asking a question to your database — and the database answers with a result table (called a result set).

SELECT column1, column2, ...
FROM table_name;

Retrieving all data

If you want to get every column and every row, use an asterisk (*):

SELECT *
FROM customers;
  • SELECT * means “select all columns”.

  • While useful for quick exploration, it’s not recommended in production queries — always specify the columns you need.

Example output

customer_idfirst_namelast_namecountry
1MayaChenFR
2LeoMartinFR
3SamSinghUS

Selecting specific columns

  • You can choose which columns to retrieve:

SELECT first_name, last_name
FROM customers;

This returns only the first and last names.

Output:

first_namelast_name
MayaChen
LeoMartin
SamSingh

Tip: Always specify column names for better performance and clarity.

Using expressions inside

You can create computed or derived columns using arithmetic, concatenation, or functions.

Examples

1. Combine first and last name:

 
SELECT first_name || ' ' || last_name AS full_name
FROM customers;

In MySQL or BigQuery: use CONCAT(first_name, ' ', last_name).

2. Add a calculated column:

SELECT order_id, amount, amount * 1.2 AS amount_with_vat
FROM orders;

Here we add 20% VAT to the amount column.

Aliases: Renaming columns for readability

Aliases make results easier to read, especially when you compute or combine values.

SELECT
first_name || ' ' || last_name AS full_name,
country AS customer_country
FROM customers;

You can also use quotes if your alias includes spaces:

SELECT
amount AS "Order Amount (€)"
FROM orders;

Why use aliases?

  • To make reports or dashboards clearer.

  • To simplify column names after joins or calculations.

Removing duplicates with DISTINCT

If your data contains duplicates and you want only unique results:

SELECT DISTINCT country
FROM customers;

Returns each country once:

country
FR
US

Sorting results with ORDER BY

To organize the result in ascending or descending order:

1- Ascending 

SELECT first_name, last_name, country
FROM customers
ORDER BY last_name ASC; -- ascending (default)

2- Descending

SELECT first_name, amount
FROM orders
ORDER BY amount DESC; -- descending

✅ Tip:
You can sort by column position (not recommended for clarity):

SELECT first_name, amount
FROM orders
ORDER BY 2 DESC; -- sorts by the 2nd column in SELECT

Limiting results with LIMIT (or TOP)

To see just a few rows, use LIMIT (or TOP in SQL Server):

SELECT *
FROM orders
LIMIT 5;
👉 Useful for checking sample data or testing queries.
In SQL Server:
SELECT TOP 5 * FROM orders;

Filtering with WHERE

Combine SELECT with WHERE to return only rows matching a condition.

Sorting + Filtering + Aliasing (Full Example)

SELECT
order_id,
customer_id,
amount AS total_eur,
status
FROM orders
WHERE status = 'paid' AND amount >= 50
ORDER BY amount DESC
LIMIT 10;
 

✅ This gives you the 10 largest paid orders over €50 — renamed and sorted.

How SQL executes a SELECT query (order of operations)

Even though you write:

 
SELECT ...
FROM ...
WHERE ...
GROUP BY ...
HAVING ...
ORDER BY ...
LIMIT ...
 

SQL actually executes it in this order:

  1. FROM — choose table(s)
  2. WHERE — filter rows
  3. GROUP BY — aggregate rows
  4. HAVING — filter groups
  5. SELECT — choose columns
  6. ORDER BY — sort results
  7. LIMIT — return final subset

This explains why you can’t use an alias defined in SELECT inside your WHERE — it hasn’t been created yet!

Summary

ConceptSyntaxExample
Select allSELECT * FROM table;SELECT * FROM orders;
Select specific columnsSELECT col1, col2 FROM table;SELECT first_name, country FROM customers;
Alias columncol AS aliasSELECT amount AS total;
Remove duplicatesSELECT DISTINCT col FROM table;SELECT DISTINCT country FROM customers;
FilterWHERE conditionWHERE status='paid';
SortORDER BY col DESCORDER BY amount DESC;
Limit rowsLIMIT nLIMIT 10;

Key Takeaways

  • SELECT is the foundation of SQL — all analysis starts here.
  • Always specify columns instead of using * for clarity and performance
  • Use aliases to make query results readable.
  • Combine WHERE, ORDER BY, and LIMIT to refine results.
  • Learn the logical order of SQL execution to understand why some expressions work only in certain clauses.