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 column1, column2, ... FROM table_name;
      • 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).

       
      SELECT * FROM Employees;

      👉 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.

       
      SELECT Name, Salary FROM Employees;

      📌 Result:

      Name Salary
      John 50000
      Alice 70000
      David 65000

      4. 🔹 Using WHERE Clause (Filtering Data)

      The WHERE clause filters rows based on conditions.

       
      SELECT Name, Department FROM Employees WHERE Salary > 60000;

      📌 Result:

      Name Department
      Alice IT
      David Finance

      5. 🔹 Using Aliases (Renaming Columns)

      Aliases help make results more readable.

       
      SELECT Name AS EmployeeName, Salary AS MonthlySalary FROM Employees;

      📌 Result:

      EmployeeName MonthlySalary
      John 50000
      Alice 70000
      David 65000

      6. 🔹 Removing Duplicate Results (DISTINCT)

      The DISTINCT keyword removes duplicates.

       
      SELECT DISTINCT Department FROM Employees;

      📌 Result:

      Department
      HR
      IT
      Finance

      7. 🔹 Sorting Results (ORDER BY)

      You can sort rows in ascending (ASC) or descending (DESC) order.

       
      SELECT Name, Salary FROM Employees ORDER BY Salary DESC;

      📌 Result:

      Name Salary
      Alice 70000
      David 65000
      John 50000

      8. 🔹 Limiting Results (LIMIT / TOP)

      • In MySQL & PostgreSQLLIMIT

      • In SQL ServerTOP

       
      -- MySQL / PostgreSQL SELECT * FROM Employees LIMIT 2; -- SQL Server SELECT TOP 2 * FROM Employees;

      📌 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

    • 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:

       
      SELECT column1, column2, ... FROM table_name WHERE condition;

      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 (=)

       
      SELECT Name, Department FROM Employees WHERE Department = 'IT';

      📌 Result:

      Name Department
      Alice IT
      Neha IT

      Greater Than (>)

       
      SELECT Name, Salary FROM Employees WHERE Salary > 60000;

      📌 Result:

      Name Salary
      Alice 70000
      David 65000
      Neha 80000

      Less Than (<)

       
      SELECT Name, Age FROM Employees WHERE Age < 30;

      📌 Result:

      Name Age
      Alice 28
      Ramesh 25

      4. 🔹 Combining Conditions

      AND (Both Conditions Must Be True)

       
      SELECT Name, Salary, Department FROM Employees WHERE Department = 'IT' AND Salary > 75000;

      📌 Result:

      Name Salary Department
      Neha 80000 IT

      OR (At Least One Condition Must Be True)

       
      SELECT Name, Department FROM Employees WHERE Department = 'Finance' OR Department = 'HR';

      📌 Result:

      Name Department
      John HR
      David Finance
      Ramesh HR

      NOT (Exclude Results)

       
      SELECT Name, Department FROM Employees WHERE NOT Department = 'HR';

      📌 Result:

      Name Department
      Alice IT
      David Finance
      Neha IT

      5. 🔹 Special Filtering Operators

      BETWEEN (Range)

       
      SELECT Name, Salary FROM Employees WHERE Salary BETWEEN 45000 AND 70000;

      📌 Result:

      Name Salary
      John 50000
      Alice 70000
      David 65000
      Ramesh 45000

      IN (Multiple Values)

       
      SELECT Name, Department FROM Employees WHERE Department IN ('IT', 'Finance');

      📌 Result:

      Name Department
      Alice IT
      David Finance
      Neha IT

      LIKE (Pattern Matching)

       
      SELECT Name FROM Employees WHERE Name LIKE 'A%';

      📌 Result:

      Name
      Alice

      👉 Patterns:

      • A% → starts with A

      • %a → ends with a

      • %am% → contains "am"


      IS NULL / IS NOT NULL

       
      SELECT Name FROM Employees WHERE Department IS NULL;

      👉 Finds employees with no department assigned.


      6. 📊 Summary

      Operator Example Meaning
      = Salary = 50000 Equal to
      > / < Age > 30 Greater / Less than
      AND Dept = 'IT' AND Salary > 70000 Both must be true
      OR Dept = 'HR' OR Dept = 'Finance' At least one true
      NOT NOT Dept = 'HR' Excludes HR
      BETWEEN Salary BETWEEN 40000 AND 60000 Within a range
      IN Dept IN ('IT','Finance') Matches multiple values
      LIKE Name LIKE 'A%' Pattern search
      IS NULL Dept IS NULL Missing values

      ✅ You now know how to filter data with WHERE using:

      • Basic operators (=, <, >)

      • Logical operators (AND, OR, NOT)

      • Special filters (BETWEEN, IN, LIKE, NULL)