Page 273 - Information_Practice_Fliipbook_Class11
P. 273

Table 9.19: Unique department numbers from EMPLOYEE table.
                                                          +-----------+
                                                          | Dept_No   |
                                                          +-----------+
                                                          | 2         |
                                                          | 1         |
                                                          | 4         |
                                                          | 5         |
                                                          +-----------+

            9.7 ORDER By Clause

            The SQL ORDER BY clause  sorts data on one or more columns in ascending or descending order. By default, SQL sorts
            the data in ascending order of the specified attribute.


                  ORDER BY clause:
                  Sorts data based on specified attribute(s)
                  Default order: Ascending, or specify DESC

            To display ID and the first name of all employees arranged alphabetically according to their first names, we use the
            statement:

            SELECT ID, FName
            FROM EMPLOYEE
            ORDER BY FName;
            On  executing  the  above  statement,  SQL  will  produce  output  shown  in  Table  9.20.  Note  the  ORDER  BY clause's
            default setting (ascending order) for the attribute FName.

                                     Table 9.20: ID, FName for employees in ascending order of FName
                                                       +-------+--------+
                                                       | ID    | FName  |
                                                       +-------+--------+
                                                       | 10005 | Anshul |
                                                       | 10002 | Dhiraj |
                                                       | 10004 | Hiten  |
                                                       | 10003 | Muskan |
                                                       | 10008 | Naval  |
                                                       | 10009 | Naveen |
                                                       | 10001 | Raj    |
                                                       | 10006 | Rajit  |
                                                       | 10010 | Savita |
                                                       | 10007 | Taran  |
                                                       +-------+--------+

            To display ID, first name, and the Salary of all employees ordered by their Salary in descending order, the following
            query may be used:

            SELECT ID, FName, Salary
            FROM EMPLOYEE
            ORDER BY Salary DESC;
            On executing the above statement, SQL will produce output shown in Table 9.21.







                                                                Database Concepts and the Structured Query Language  259
   268   269   270   271   272   273   274   275   276   277   278