Presentation is loading. Please wait.

Presentation is loading. Please wait.

ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific.

Similar presentations


Presentation on theme: "ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific."— Presentation transcript:

1 ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific ordered format, then the ORDER BY clause would be used. The result can be in - Ascending order (Default) - Descending order - By multiple columns Ex:1) To sort all employees based on name of the employee. SQL> select * from emp ORDER BY ename ASC; or SQL> select * from emp ORDER BY 2 ASC ; or SQL> select * from emp ORDER BY ename ;

2 Ex 2) To sort all employees based on maximum salary wise SQL> select * from emp ORDER BY basic DESC; or SQL> select * from emp ORDER BY 5 DESC; Ex : 3 ) To sort all employees based on maximum salary wise in each department SQL> select * from emp ORDER BY dept ASC, basic DESC;

3 GROUP BY clause in SELECT command: The GROUP BY clause divides a table into groups of rows, so that the rows in each group have the same value in a specified column. Syntax: SELECT FROM WHERE GROUP BY Ex: Consider the following table “SALES” Name Null? Type ------------------------------- -------- ---- STATION CHAR(15) MONTH CHAR(15) SEED_NAME CHAR(15) QUANTITY NUMBER(10)

4 STATION MONTH SEED_NAME QUANTITY --------------- --------------- --------------- --------- Killikulam January ASD16 10 Coimbatore January IR20 20 Trichy January ASD16 25 Killikulam February ASD16 8 Killikulam February ASD18 20 Coimbatore February ASD16 30 Trichy February IR20 30 Trichy February ASD18 10 Ex: To find total seeds quantity in station wise SQL> SELECT SUM(QUANTY) FROM SALES WHERE STATION = ‘Trichy'; SUM(QUANTY) ----------- 65

5 SQL> SELECT station, 'Total Sales = ', sum(quanty) FROM sales GROUP BY station; STATION 'TOTALSALES=' SUM(QUANTY) ----------------------------- ----------- Coimbatore Total Sales = 50 Killikulam Total Sales = 38 Trichy Total Sales = 65 Ex: 2 To find total seeds quantity in seed wise

6 SQL> select sum(quanty) from sales group by seed_name; SUM(QUANTY) ----------- 73 30 50 HAVING clause in SELECT command: We can select specific rows with the help of a WHERE clause As well as we select specific groups with the help of a HAVING clause. Syntax: SELECT FROM WHERE GROUP BY

7 Ex: To find total seeds quantity in Trichy station SQL> SELECT sum(quanty) from sales GROUP BY station HAVING station = ' Trichy '; SUM(QUANTY) ----------- 65 Ex:2 To list all stations its total seed sales is >= 50; SQL> select station, 'Total Sales = ', sum(quanty) from sales group by station HAVING sum(quanty) >= 50; STATION 'TOTALSALES =' SUM(QUANTY) --------------- -------------- ----------- Coimbatore Total Sales = 50 Trichy Total Sales = 65


Download ppt "ORDER BY clause in SELECT command: Normally, the result of the query will not be in ordered format. If we want to get the result of the query in specific."

Similar presentations


Ads by Google