Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to use of AND & OR operators in MySQL?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 528
    Comment on it

    Sometimes we need to filter records based on some conditions, for that we use the AND & OR operators in MySQL.

    AND operator - We use AND operator if we require both conditions in a query means the AND operator displays a record only if both the first condition AND the second condition are true in the query.

    OR operator - We use OR operator if we require any condition in a query means the OR operator displays a record only if either the first condition OR the second condition are true in the query.

    We have a table "employee" as below:

        employee
    
        id  first_name               salary       country
        .......................................................
        1   John                     10000         Canada
        2   Chris                    20000         London
        3   Max                      30000         Canada
        4   Jenny                    25000         Canada
        5   Jack                     25000         USA
        6   Clark                    35000         Canada
    

    AND Operator Example

    The below statement selects all employees who have country "Canada" and salary is greater than 10000:

    SELECT * FROM employee
    WHERE country="Canada"
    AND salary > 10000;
    

    Result

        id  first_name               salary       country
        .......................................................
        3   Max                      30000         Canada
        4   Jenny                    25000         Canada
        6   Clark                    35000         Canada
    

    OR Operator Example

    The below statement selects all employees with country "Canada" or "London":

    SELECT * FROM employee
    WHERE country="Canada"
    OR country="London";
    

    Result

        id  first_name               salary       country
        .......................................................
        1   John                     10000         Canada
        2   Chris                    20000         London
        3   Max                      30000         Canada
        4   Jenny                    25000         Canada
        6   Clark                    35000         Canada
    

    We can also use AND and OR operators together.

    The below statement selects all employees from the country "Canada" AND the salary greater than 1000 OR less then 35000, in the "employee" table:

    SELECT * FROM employee
    WHERE country='Canada'
    AND (salary > 10000 OR salary < 35000);
    

    Result

        id  first_name               salary       country
        .......................................................
        3   Max                      30000         Canada
        4   Jenny                    25000         Canada
    

    Hope this will help you :)

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: