SELECT
The SELECT command is the foundation of data retrieval in SQL.
While beginners often start with SELECT *, real world applications require precise and efficient data selection.
In this chapter, we explore:
- How to select specific columns
- Why selecting only required columns is important
- How to use Aliases to rename columns temporarily for better readability
Why Selecting Specific Columns Matters
Using SELECT * retrieves all columns from a table, which may:
- Fetch unnecessary data
- Reduce query performance
- Make results harder to read
Selecting only required columns:
- Improves performance
- Enhances clarity
- Follows professional database practices
Example: employees table
| emp_id | emp_name | department | salary |
|---|---|---|---|
| 101 | Rahul Kumar | Marketing | 18,000 |
Syntax: Selecting Specific Columns
SELECT column1, column2FROM table_name;Example
SELECT emp_name, salaryFROM employees;Result
Only the emp_name and salary columns are displayed.
Selecting Columns in a Preferred Order
The order of columns in the SELECT statement determines the order of output, regardless of the table structure.
SELECT salary, emp_nameFROM employees;When working with databases, column names are often created for technical clarity, not for presentation.
For example, column names may be:
- Too long
- Not user-friendly
- Written using abbreviations
- Difficult to understand in reports
SELECT emp_name, salary * 12FROM employees;Although the query works correctly, the output column name for the calculated value may appear unclear or system-generated.
In real-world applications, SQL query results are often:
- Shown to end users
- Used in reports and dashboards
- Exported to Excel or PDF files
In such cases, readable and meaningful column names become important.
An Alias is a temporary name assigned to a column or table only for the result set.
Aliases:
- Improve readability
- Make column names user-friendly
- Are commonly used in reports and dashboards
Syntax
SELECT column_name AS alias_nameFROM table_name;The keyword AS is optional but recommended for clarity.
Example: Using Column Alias
SELECT emp_name AS Employee_Name, salary AS Monthly_SalaryFROM employees;Result
The column names in the output appear as:
- Employee_Name
- Monthly_Salary
Example: Alias Without AS Keyword
SELECT emp_name Employee_Name, salary Monthly_SalaryFROM employees;This works, but using AS improves readability and is considered a best practice.
Example: Using Aliases with Expressions
Aliases are especially useful when working with calculated values.
SELECT emp_name, salary * 12 AS Annual_SalaryFROM employees;Important Points to Remember
- Aliases do not change the actual column names
- They exist only during query execution
- Useful for reporting and analysis
- Improve query readability
Two Minute Drill
- Avoid SELECT* in professional queries
- Select only required columns
- Column order depends on SELECT clasue
- Aliases rename columns temporarily
- AS keyword improves clarity
- Aliases are comonly used with expressions