SELECT is the most used SQL command, used to retrieve data from a database.
Think of SQL like asking questions to a database. SELECT = asking what data you want.
โ What is SELECT in SQL?
SELECT statement retrieves data from one or more tables in a database.
๐ Basic Syntax
SELECT column_name
FROM table_name;
How SQL executes:
1. Finds table (FROM)
2. Applies filter (WHERE)
3. Returns selected columns (SELECT)
4. Sorts results (ORDER BY)
5. Limits rows (LIMIT)
๐น 1. SELECT All Columns (SELECT *)
Used to retrieve every column from a table.
SELECT *
FROM employees;
๐ Returns complete table data.
๐ When to use:
โ Exploring new dataset
โ Checking table structure
โ Quick testing
โ ๏ธ Avoid in production: Slow on large tables, fetches unnecessary data.
๐น 2. SELECT Specific Columns
Best practice โ retrieve only required data.
SELECT name, salary
FROM employees;
๐ Returns only selected columns.
๐ก Why important:
โ Faster queries
โ Better performance
โ Cleaner results
๐น 3. FROM Clause (Data Source)
Specifies where data comes from.
SELECT name
FROM customers;
๐ SQL reads data from customers table.
๐น 4. WHERE Clause (Filtering Data)
Used to filter rows based on conditions.
SELECT column
FROM table
WHERE condition;
Examples:
- Filter by value:
SELECT * FROM employees WHERE salary > 50000;- Filter by text:
SELECT * FROM employees WHERE city = 'Mumbai';๐น 5. ORDER BY (Sorting Results)
Sorts query results.
SELECT column
FROM table
ORDER BY column ASC | DESC;
Examples:
- Ascending:
SELECT name, salary FROM employees ORDER BY salary ASC;- Descending:
SELECT name, salary FROM employees ORDER BY salary DESC;๐น 6. LIMIT (Control Output Rows)
Restricts number of returned rows.
SELECT *
FROM employees
LIMIT 5;
๐ Returns first 5 records.
โญ SQL Query Execution Order
1. FROM
2. WHERE
3. SELECT
4. ORDER BY
5. LIMIT
๐ง Real-World Example
Business question: "Show top 10 highest paid employees."
SELECT name, salary
FROM employees
ORDER BY salary DESC
LIMIT 10;
๐ Mini Practice Tasks
โ Task 1: Get all records from customers.
โ Task 2: Show only customer name and city.
โ Task 3: Find employees with salary > 40000.
โ Task 4: Show top 3 highest priced products.
Double Tap โฅ๏ธ For Part-2