1. 🔹 What is SELECT?
-
The SELECT statement is used to retrieve data from a database table.
-
It is the most common SQL command and forms the foundation of querying.
👉 General Syntax:
-
SELECT→ specifies which columns to show. -
FROM→ tells SQL which table to get data from.
2. 🔹 Selecting All Columns
If you want to get all the data from a table, use * (wildcard).
👉 Example Table: Employees
| EmployeeID | Name | Department | Salary |
|---|---|---|---|
| 101 | John | HR | 50000 |
| 102 | Alice | IT | 70000 |
| 103 | David | Finance | 65000 |
📌 Result:
Shows all rows and columns of the Employees table.
3. 🔹 Selecting Specific Columns
You can choose only the columns you want.
📌 Result:
| Name | Salary |
|---|---|
| John | 50000 |
| Alice | 70000 |
| David | 65000 |
4. 🔹 Using WHERE Clause (Filtering Data)
The WHERE clause filters rows based on conditions.
📌 Result:
| Name | Department |
|---|---|
| Alice | IT |
| David | Finance |
5. 🔹 Using Aliases (Renaming Columns)
Aliases help make results more readable.
📌 Result:
| EmployeeName | MonthlySalary |
|---|---|
| John | 50000 |
| Alice | 70000 |
| David | 65000 |
6. 🔹 Removing Duplicate Results (DISTINCT)
The DISTINCT keyword removes duplicates.
📌 Result:
| Department |
|---|
| HR |
| IT |
| Finance |
7. 🔹 Sorting Results (ORDER BY)
You can sort rows in ascending (ASC) or descending (DESC) order.
📌 Result:
| Name | Salary |
|---|---|
| Alice | 70000 |
| David | 65000 |
| John | 50000 |
8. 🔹 Limiting Results (LIMIT / TOP)
-
In MySQL & PostgreSQL →
LIMIT -
In SQL Server →
TOP
📌 Result:
Shows only the first 2 rows.
9. 📊 Summary
| Keyword | Purpose | Example |
|---|---|---|
SELECT * |
All columns | SELECT * FROM Employees; |
SELECT col1, col2 |
Specific columns | SELECT Name, Salary FROM Employees; |
WHERE |
Filter rows | WHERE Salary > 60000; |
AS |
Rename column | Name AS EmployeeName |
DISTINCT |
Remove duplicates | SELECT DISTINCT Department; |
ORDER BY |
Sort results | ORDER BY Salary DESC; |
LIMIT / TOP |
Restrict rows | LIMIT 5 / TOP 5 |
✅ You now know how to:
-
Select all or specific columns
-
Filter results
-
Rename columns
-
Remove duplicates
-
Sort and limit results