SELECT Queries
The SELECT statement is the heart of SQL. Almost every interaction with a database starts with a SELECT query. It allows us to retrieve data from tables in a clear and controlled way.
What is a SELECT Query?
A SELECT query tells the database:
- What data you want
- From which table you want it
The simplest SELECT query looks like this:
SELECT * FROM table_name;
Selecting All Columns
The asterisk symbol (*) means all columns.
When you use it, SQL returns every column from the table.
Example:
SELECT * FROM employees;
This query returns the complete employees table.
Selecting Specific Columns
In real-world projects, selecting all columns is not always efficient. Instead, we usually select only the columns we need.
Example:
SELECT name, department FROM employees;
This query returns only the name and
department columns.
Why Selecting Specific Columns is Better
| Reason | Explanation |
|---|---|
| Performance | Less data is transferred from the database |
| Clarity | Results are easier to read and understand |
| Security | Sensitive columns can be excluded |
Understanding Query Execution Order
Although we write SELECT queries in this order:
SELECT columns FROM table;
SQL internally processes them in this logical order:
FROM– identify the tableSELECT– retrieve columns
Understanding this order will help you later when working with filters and joins.
Using Column Aliases
Column aliases allow us to rename columns in the result output. They make query results more readable.
Example:
SELECT name AS employee_name, salary AS monthly_salary FROM employees;
Aliases only affect the output — the actual table structure remains unchanged.
SELECT with Calculated Columns
We can also perform calculations inside SELECT queries.
Example:
SELECT name, salary, salary * 12 AS annual_salary FROM employees;
This query calculates the annual salary without modifying the table data.
Common Beginner Mistakes
- Forgetting commas between column names
- Using incorrect table names
- Confusing column aliases with real column names
Always double-check spelling and table structure when writing queries.
What’s Next?
In the next lesson, we will learn how to filter data
using the WHERE clause, allowing us to retrieve only the
rows that match specific conditions.