Topic 7 of 52
SELECT Basics
Overview
The SELECT statement is the most frequently used command in SQL. It reads data from the database. You use it to specify exactly which columns you want to extract, and from which table. By explicitly naming columns instead of fetching everything, you drastically reduce memory usage on your server and drastically speed up query times.
Syntax
sql
-- 1. Select specific columns (Best Practice)
SELECT first_name, email, age
FROM users;
-- 2. Select EVERYTHING (Great for testing, terrible for production)
SELECT *
FROM users;
-- 3. Aliasing (Renaming columns in the output result set)
-- The 'AS' keyword is optional but highly recommended for readability.
SELECT first_name AS "First Name",
age AS user_age
FROM users;
-- 4. Basic Math in SELECT
SELECT product_name, price, (price * 1.20) AS price_with_tax
FROM products;Common Pitfalls
- Using
SELECT *in production code. If your table has 50 columns, including massive text blobs, fetching*will download gigabytes of unnecessary data across the network to your backend, severely lagging the app. Always specify only the columns you actually need. - Forgetting quotes around string aliases with spaces.
SELECT first_name AS First Namewill cause a syntax error. You must use double quotes:AS "First Name".
Interview Questions
Q:
Does
SELECT * guarantee that the columns will always return in the exact same order every time you run the query?A:
Usually yes, based on the physical table schema definition. However, if an engineer runs an ALTER TABLE to add or move a column, your backend code expecting data at a specific array index will instantly break. This is another major reason to explicitly list column names.
Real-World Example
Formatting API responses directly in the database layer.
example
sql
-- Instead of fetching raw data and fixing it in Python/JS,
-- format it instantly using string concatenation in SQL!
SELECT
CONCAT(first_name, ' ', last_name) AS full_name,
LOWER(email) AS standardized_email
FROM employees;Check Your Knowledge
Test your understanding of SELECT Basics with these quick questions.