Section outline
-
-
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
WHEREclause 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
DISTINCTkeyword 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, col2Specific columns SELECT Name, Salary FROM Employees;WHEREFilter rows WHERE Salary > 60000;ASRename column Name AS EmployeeNameDISTINCTRemove duplicates SELECT DISTINCT Department;ORDER BYSort results ORDER BY Salary DESC;LIMIT / TOPRestrict 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
-
-
1. 🔹 What is WHERE?
-
The WHERE clause is used to filter rows based on specific conditions.
-
It tells SQL: “Only show the rows that match this rule.”
👉 General Syntax:
2. 🔹 Example Table (Employees)
EmployeeID Name Department Salary Age 101 John HR 50000 30 102 Alice IT 70000 28 103 David Finance 65000 40 104 Neha IT 80000 35 105 Ramesh HR 45000 25
3. 🔹 Basic Filtering
Equality (=)
📌 Result:
Name Department Alice IT Neha IT
Greater Than (>)
📌 Result:
Name Salary Alice 70000 David 65000 Neha 80000
Less Than (<)
📌 Result:
Name Age Alice 28 Ramesh 25
4. 🔹 Combining Conditions
AND (Both Conditions Must Be True)
📌 Result:
Name Salary Department Neha 80000 IT
OR (At Least One Condition Must Be True)
📌 Result:
Name Department John HR David Finance Ramesh HR
NOT (Exclude Results)
📌 Result:
Name Department Alice IT David Finance Neha IT
5. 🔹 Special Filtering Operators
BETWEEN (Range)
📌 Result:
Name Salary John 50000 Alice 70000 David 65000 Ramesh 45000
IN (Multiple Values)
📌 Result:
Name Department Alice IT David Finance Neha IT
LIKE (Pattern Matching)
📌 Result:
Name Alice 👉 Patterns:
-
A%→ starts with A -
%a→ ends with a -
%am%→ contains "am"
IS NULL / IS NOT NULL
👉 Finds employees with no department assigned.
6. 📊 Summary
Operator Example Meaning =Salary = 50000Equal to >/<Age > 30Greater / Less than ANDDept = 'IT' AND Salary > 70000Both must be true ORDept = 'HR' OR Dept = 'Finance'At least one true NOTNOT Dept = 'HR'Excludes HR BETWEENSalary BETWEEN 40000 AND 60000Within a range INDept IN ('IT','Finance')Matches multiple values LIKEName LIKE 'A%'Pattern search IS NULLDept IS NULLMissing values
✅ You now know how to filter data with WHERE using:
-
Basic operators (
=,<,>) -
Logical operators (
AND,OR,NOT) -
Special filters (
BETWEEN,IN,LIKE,NULL)
-
-