Data Query Language(DQL) examples in sql

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.

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;

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;

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.



  Tutorials

Popular Links

Contact Us

address Plot No-N6/530,2ND Floor
Nayapalli,Jayadev Vihar
      Bhubaneswar-751015
      Email: info@silantechnology.com
      Phone: 0674-2361252
address
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved