TechnoLearnAcademy

TECHNOLEARN

SQL WHERE Clause

The SQL WHERE Clause is used to filter rows in a table based on specific conditions.
In the where clause, condition refers to the expression that determines whether each row in the table is true or false.
If the condition is true, the row will be shown.
if the condition is false, the row will not be displayed.

Syntax:      

                SELECT  column1 , column2 , column3  FROM WHERE tablename ; 
Let’s understand the where clause with an example.
         Table Emp

Example 1:

   Objective: In this example, we will retrieve all the records from the Emp table where the location is Texas.
                      SELECT  * FROM Tbl_Emp WHERE location = ‘Texas’;
    Output:
Note: In this example, ‘Texas’ is a text argument. Text arguments always enclose in single quotes.

Example 2:

   Objective: In this example, we will retrieve all the records from the Emp table where ecode is 1025. 
                      SELECT  * FROM Tbl_Emp WHERE = 1025;
    Output:
Note: In this example, ecode is a numeric argument. We do not enclose numeric arguments in single quotes.

Example 3:

   Objective: In this example, we will retrieve all the records from the Emp table where the salary is greater than 2500. 
                      SELECT  * FROM Tbl_Emp WHERE Salary > 25000;
    Output:

Example 4:

   Objective: In this example, we will retrieve all the records from the Emp table where the salary is less than 2500. 
                      SELECT  * FROM Tbl_Emp WHERE Salary < 25000;
    Output:

AND OPERATOR

Example 5:

   Objective: In this example, we will retrieve all the records from the Emp table where the salary is greater than 25000 and                        less than 2500. 
                      SELECT  * FROM Tbl_Emp WHERE Salary Between 25000 and 45000;
    Output:

Example 6:

   Objective: In this example, we will use the IN Operator to retrieve all the records from the Emp table where ecode is                               1025. 
                      SELECT  * FROM Tbl_Emp ECODE IN (1026) ;
    Output:

Example 7:

   Objective: For this example, we will use the NOT IN Operator to retrieve all records from the Emp table, except for those                               where the ecode is 1025.
                      SELECT  * FROM Tbl_Emp WHERE ECODE NOT IN (1026);
    Output:

Example 8:

   Objective: In this example, we will use the AND Operator to retrieve all the records from the Emp table where                                          Deparment  is IT and Location is WASHINGTON. 
                      SELECT  * FROM Tbl_Emp WHERE department = ‘IT’ AND location = ‘Washington’;
    Output:

Example 9:

   Objective: In this example, we will use OR Operator to retrieve all the records from the Emp table where Location is                                Washington OR Location is Florida. 
                      SELECT  * FROM Tbl_Emp WHERE Location = ‘Washington’ OR Location = ‘Florida’;
    Output:

Example 10:

   Objective: In this example, we will use the AND Operator in combination with the IN Operator to retrieve all records                              from the Emp. 
                      SELECT * FROM Tbl_Emp WHERE Department = ‘IT’ and (Location = ‘Washington’ OR Location = ‘Florida’);
    Output:
Scroll to Top