• Home
  • SQL Operator Examples

    Select emp-name, emp-salary
    From employee
    Where dept-id in(10, 20);

    In the above example it will display all those emp-name and emp-salary where the dept-id is either 10 or 20.

    Note: if character or dates are used in the list they must be enclosed in single quotes.

    Like operator:

    It is quite logical that you may not always know the exact value to search for. You can select records that match a character pattern(wildcard pattern) by using the like operator. You can use like operator to perform wildcard search of valid search string values. The search condition may contain either literal characters or numbers.

    • % denotes either 0 or many characters.
    • _ denotes one character

    Select emp-name, emp-salary
    From employee
    Where emp-name like 's%' ;

    In the above example it will display all the emp-name and emp-salary from the employee table where the emp-name begins with s.

    The null operator:

    The null operator includes the is null condition and is not null condition.

    Example:

    Select emp-name, emp-salary
    From employee
    Where dept-id is null ;

    In the above example it will display all those emp-name and emp-salary where the dept-id is null.

    The logical operators:

    A logical operator helps us to combine two or more conditions in a where clause to produce a single result based on them or inverts the result of a single condition. A list of logical operators are as follows:

    Operator Meaning
    And Returns true if both components conditions are true
    Or Returns true if either component condition is true
    Not Returns true if the following condition is false

    Example:
    • Select * from employee
    • Where emp-name='sunil' or emp-name='rakesh'

    • Select * from employee where not dept-id='101';

    Sorting records with the help of order by clause:

    The order of rows return by a query is undefined. The order by clause can be used to sort the rows. The default order of order by clause is ascending order. To change the order, we use the DESC keyword. It is noted that the order by clause is the last clause of the SQL statement.

    Syntax:
    Select expr
    From table
    [where condition(s)]
    [order by{column, expr} [asc|desc] ;

    In the above syntax:

    Order by specifies the order in which the retrieved rows are displayed
    Asc orders the rows in ascending order (this is the default order)
    Desc orders the rows in descending order

    Example:

    Select emp-name, emp-salary, hire-date
    From employee
    Order by hire-date DESC ;

    The above query will display the rows in the descending order of hire-date value.

    Note:- the column name that we are using in the order by clause should be available in the select column list.


    About the Author



    Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

    We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc





     PreviousNext