Stored Procudure - ORDER BY Clause

It is used to display the table data in stored order. The order may be either ascending (or) descending order depending on the specified column. It always appears at the end of select statement.

Syntax:

SELECT {*/Column List} FROM TABLENAME [WHERE CONDITION] ORDER BY COLUMNNAME {ASC/DESC}

The default order is ascending order. It means if we does not specify any specific order then it automatically takes ascending order.

Example1: Write a query to display employ details in ascending order based on their department numbers(DEPTNO).

SELECT * FROM EMP ORDER BY DEPTNO ASC

Example2: Write a query to display employ details in descending order based on their employ numbers(EMPNO).

SELECT * FROM EMP ORDER BY EMPNO DESC

Example3: Write a query to display employ details in descending order based on their salaries(SAL)

SELECT * FROM EMP ORDER BY SAL DESC

Exampe4: Write a querey to display employ names and their salaries in descending order based on their salaries(SAL).

SELECT ENAME,SAL FROM ORDER BY SAL DESC

Example5: Write a query to display 10th department employs in ascending order based on their employ names(ENAME).

SELECT * FROM EMP WHERE DEPTNO=10 ORDER BY ENAME
Share/Bookmark

No comments: