SQL SELECT Statement & Retrieving Data
Now that you understand databases, tables, rows, columns, primary keys, and foreign keys, it's time to learn the most fundamental SQL command: SELECT.
Almost every SQL analysis starts with retrieving data.
1️⃣ What is SELECT?
SELECT is used to retrieve data from one or more columns in a table.
Basic syntax:
SELECT column_name
FROM table_name;
Example:
SELECT customer_name
FROM customers;
2️⃣ Select Multiple Columns
You can retrieve multiple columns by separating them with commas.
SELECT
customer_id,
customer_name,
city
FROM customers;
Result:
customer_id | customer_name | city
1 | Rahul | Mumbai
2 | Priya | Delhi
3 | Amit | Pune
3️⃣ Select All Columns Using *
If you want every column:
SELECT *
FROM customers;
* means all columns.⚠️ Interview Tip: Although
SELECT * is convenient while exploring data, avoid relying on it in production queries. Prefer selecting only needed columns.4️⃣ Column Aliases
Use AS to give a column a different name in the result.
SELECT
customer_name AS name,
city AS location
FROM customers;
The original table is not changed.
5️⃣ Aliases Without AS
SELECT
customer_name name,
city location
FROM customers;
However, using AS is generally clearer for beginners.
6️⃣ Calculations Inside SELECT
SELECT
product_name,
price,
price * 0.90 AS discounted_price
FROM products;
7️⃣ Arithmetic Operators
+ Addition, - Subtraction, * Multiplication, / DivisionSELECT
product_name,
selling_price,
cost_price,
selling_price - cost_price AS profit
FROM products;
8️⃣ Using Expressions
SELECT
product_name,
quantity,
unit_price,
quantity * unit_price AS total_value
FROM order_items;
9️⃣ DISTINCT
Removes duplicate values.
SELECT DISTINCT city
FROM customers;
🔟 DISTINCT Across Multiple Columns
SELECT DISTINCT
city,
customer_segment
FROM customers;
1️⃣1️⃣ Using SELECT With Text
SELECT
customer_name,
'Active Customer' AS status
FROM customers;
1️⃣2️⃣ Combining Columns
SELECT
first_name,
last_name,
CONCAT(first_name, ' ', last_name) AS full_name
FROM employees;
1️⃣3️⃣ SELECT With a Condition
SELECT
customer_name,
city
FROM customers
WHERE city = 'Mumbai';
1️⃣5️⃣ SQL Query Structure
At this stage, learn this basic pattern:
SELECT column1, column2
FROM table_name;
SELECT column1, column2
FROM table_name
WHERE condition;
1️⃣6️⃣ A Real-World Example
Manager asks: "Show me product name, selling price, cost price, and profit"
SELECT
product_name,
selling_price,
cost_price,
selling_price - cost_price AS profit
FROM products;
