Arranging output in SQL

Remember ORDER BY is the last clause in the SELECT statement, which arranges the order after the SQL data are fetched

The ORDER BY clause arranges the  output of a SELECT statement in a particular order. It can be ascending or descending.

SELECT EMPNO,ENAME,SAL FROM EMP ORDER BY SAL;
 

The above SQL will display the result in the ascending order of SAL.

We can achieve the similar output by slightly reforming the statement, like

SELECT EMPNO,ENAME,SAL FROM EMP ORDER BY 3;

Notice SAL is the 3rd column to be displayed

SELECT EMPNO,ENAME,SAL FROM EMP ORDER BY SAL DESC;

will display the output in descending order.

You can use where condition in the sqame statement, but the ORDER BY clause should be the last one in the SELECT statement

Between, In Operators in SQL

This lesson will describe the usage of IN, Between Operators in Oracle

At times usage of OR, AND becomes cumbersome. Oracle has facilitated some other operators

BETWEEN

SELECT ENAME,JOB,SAL FROM EMP

WHERE SAL BETWEEN 1500 AND 3000;

is exactly equivalent to

SELECT ENAME,JOB,SAL FROM EMP

WHERE SAL> 1500 AND SAL < 3000;

Between can replace complicated AND operators

IN

SELECT EMPNO,ENAME,JOB,DEPTNO FROM EMP

WHERE JOB IN (‘PRESIDENT’,’MANAGER’,’ANALYST’);
 

This is exactly equivalent to

SELECT EMPNO,ENAME,JOB,DEPTNO FROM EMP

WHERE JOB =‘PRESIDENT’ OR JO’MANAGER’,’ANALYST’;

IN can replace complicated OR operators

And & OR Operators in SQL

How to and where to use AND and OR Operators in Select Statement

We should remember a fundamental concept. AND restricts further and OR loosens the restrictions in the result set

SELECT ENAME, SAL FROM EMP WHERE DEPTNO=10 AND SAL>1500;

The above statement will display the restricted set of rows whose department no is 10 and SAL is greater than 1500.

If we use OR instead of AND

it will display the rows, where ever there is a department number 10 as well as SAL is greater than 1500

embed a message in SQL

How to embed a message in SQL

SELECT ENAME||’ ‘||’is in the position of ’||’ ‘||JOB||’ ‘||’and his earning is || ‘ ‘||SAL “Employee Details” FROM EMP;

Look at the usage of quodes (‘) and the concatination symbols ||

The blank spaces have been mentioned within quodes to display the required additional blanks between words.