SELECT Statement & Column Aliases: The Core of Data Querying (DQL)
Over 80% of all SQL code written by software engineers, data analysts, and BI specialists consists of Data Query Language (DQL). The cornerstone of DQL is the SELECT statement, used to retrieve rows and columns from one or more database tables.
1. The Core SELECT Syntax
1SELECT[DISTINCT] column_1, column_2,...
2FROM table_name;
Querying All Columns (SELECT *) vs Specific Columns
1-- Retrieve all columns across all rows (Wildcard):
2SELECT*FROM employees;
3
4-- Retrieve only specific columns (Recommended Best Practice):
5SELECT employee_id, first_name, salary FROM employees;
Why SELECT * is an Anti-Pattern in Production:
1
Network Overhead: Fetching 30 columns when your web app only needs id and name wastes bandwidth and slows down API response times.
2
Memory Exhaustion: If a table contains wide TEXT or BLOB columns, SELECT * pulls massive data payloads into memory buffers.
3
Breaks Covering Indexes: Queries that select specific indexed columns can be answered entirely from RAM indexes without touching the disk tablespace!
2. Renaming Columns with Column Aliases (AS)
A Column Alias temporarily renames a column or calculated expression in the query result set, making outputs readable for human users and API serializers:
1SELECT
2 customer_id AS id,
3 first_name AS'First Name',
4 account_balance AS current_balance,
5 account_balance *0.18AS estimated_gst_tax -- Computed column alias
6FROM customers;
Alias Rules:
The AS keyword is optional (e.g., first_name fname), but including AS makes queries much easier to read and maintain.
If your alias contains spaces or special characters, enclose it in quotes (e.g., 'First Name' or `First Name`).
3. Removing Duplicates with DISTINCT
When querying columns that contain duplicate values (e.g., cities, departments, job titles), use DISTINCT to return only unique values:
1-- View all unique cities where customers reside:
2SELECTDISTINCT city FROM customers;
3
4-- DISTINCT across multiple columns (unique combinations):
5SELECTDISTINCT department_id, job_title FROM employees;
4. Literal Values & Arithmetic Expressions
SELECT can be used without a table to evaluate scalar expressions or inspect server variables:
Aliases Cannot Be Used in WHERE Clauses: In SQL, the WHERE clause is evaluated before the SELECT clause. Therefore, referring to a column alias inside WHERE triggers an error!
1-- WRONG (Syntax Error):
2SELECT salary *12AS annual_salary FROM employees WHERE annual_salary >500000;
3
4-- CORRECT:
5SELECT salary *12AS annual_salary FROM employees WHERE(salary *12)>500000;
Interactive Quiz• Knowledge Check
Multiple Choice Questions
Question 1
What does the AS keyword accomplish in a SELECT statement?
Question 2
Why is using SELECT * considered an anti-pattern in high-performance production APIs?
Question 3
Which keyword eliminates duplicate rows from the result set of a SELECT query?
Question 4
Why does the query SELECT price * 2 AS double_price FROM items WHERE double_price > 100; fail in standard SQL?
Question 5
Can a SELECT query be executed in MySQL without specifying a FROM table_name clause?
Next Lesson
Filtering Rows with WHERE Clause
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.