Basic SQL for Data Engineers
By the end of this lesson, you’ll be able to:
Understand what the
SELECTstatement 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_id | first_name | last_name | country |
|---|---|---|---|
| 1 | Maya | Chen | FR |
| 2 | Leo | Martin | FR |
| 3 | Sam | Singh | US |
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_name | last_name |
|---|---|
| Maya | Chen |
| Leo | Martin |
| Sam | Singh |
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;
SELECT TOP 5 * FROM orders;
Filtering with WHERE
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:
FROM— choose table(s)WHERE— filter rowsGROUP BY— aggregate rowsHAVING— filter groupsSELECT— choose columnsORDER BY— sort resultsLIMIT— 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
| Concept | Syntax | Example |
|---|---|---|
| Select all | SELECT * FROM table; | SELECT * FROM orders; |
| Select specific columns | SELECT col1, col2 FROM table; | SELECT first_name, country FROM customers; |
| Alias column | col AS alias | SELECT amount AS total; |
| Remove duplicates | SELECT DISTINCT col FROM table; | SELECT DISTINCT country FROM customers; |
| Filter | WHERE condition | WHERE status='paid'; |
| Sort | ORDER BY col DESC | ORDER BY amount DESC; |
| Limit rows | LIMIT n | LIMIT 10; |
Key Takeaways
SELECTis 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, andLIMITto refine results. - Learn the logical order of SQL execution to understand why some expressions work only in certain clauses.
Finish Course Early?
You have not completed all required lessons and assessments.