SELECT, DISTINCT, Column Aliasing (AS) & Backtick Identifiers in MySQL
Overview & Learning Objectives
The `SELECT` statement is the cornerstone of Data Query Language (DQL). Production analytics requires selective column projection rather than wasteful `SELECT *` queries that overwhelm network bandwidth. Analysts leverage `DISTINCT` to uncover unique values and apply column aliases (`AS`) to label computed metrics with clear business nomenclature. In MySQL, identifiers containing spaces or reserved keywords are wrapped in backticks (``` `order` ```).
- Retrieve specific column subsets and compute derived expressions.
- Deduplicate records across single and multiple columns using `DISTINCT`.
- Assign readable column aliases (`AS`) and safely escape reserved words with backticks.
Industry Standard
Standard: Top engineering teams at Netflix and Amazon prohibit `SELECT *` in production services to minimize memory footprint, avoid unnecessary I/O, and prevent schema mismatch bugs.
Real-World Example: ScoopCraft Dark-Store Delivery Performance
ScoopCraft analytics extracts distinct dark-store hub locations and formats order ticket totals with currency naming.
| order_id | dark_store | subtotal | tax_amount | Gross Sales Formula |
|---|---|---|---|---|
| ORD-201 | Koramangala Hub | 350.00 | 17.50 | subtotal + tax_amount AS gross_ticket |
| ORD-202 | Indiranagar Hub | 280.00 | 14.00 | subtotal + tax_amount AS gross_ticket |
| ORD-203 | Koramangala Hub | 510.00 | 25.50 | subtotal + tax_amount AS gross_ticket |
Syntax & Core SQL Implementation
SELECT DISTINCT
dark_store AS fulfillment_hub,
subtotal + tax_amount AS gross_revenue
FROM `orders`
WHERE is_completed = TRUE;
Parameters & Arguments Breakdown
SELECT col1, col2: Projects specific target columns from the table.DISTINCT: Eliminates duplicate rows across the combination of selected columns.AS alias_name: Renames output column header for presentation and downstream reporting.`identifier` (Backticks): MySQL escaping syntax allowing table or column names that match reserved keywords.
Sample Walkthrough (Q&A)
Question: How do you find all distinct combinations of dark store hubs and payment modes?
SQL Pattern: SELECT DISTINCT dark_store, payment_mode FROM orders;
Result: Returns unique pairs (e.g. Koramangala - UPI, Koramangala - Card, Indiranagar - UPI)
Explanation: DISTINCT evaluates the entire row tuple across all projected columns, not just the first column.
Common Mistakes & Limitations
- Using `SELECT *` in analytics reports on 50-column tables when only 3 columns are needed, causing massive query performance penalties.
- Using double quotes `"` or single quotes `'` for column aliases in MySQL instead of standard identifiers or backticks.
Keyboard Shortcuts (MySQL Workbench)
| Action | Windows | Mac |
|---|---|---|
| Execute Query | Ctrl + Enter | Cmd + Enter |
Pro Tips & Modern Best Practices
- In MySQL, backticks (``` `table` ```) escape table and column names; single quotes (`"string"`) are strictly for text string literals.
- Use `SELECT COUNT(DISTINCT customer_id)` to quickly check unique active buyer counts.