ORDER BY Clause- The ORDER BY clause is used to sort the data in ascending(its default ) or descending order of one or more columns and use the key word ORDER BY. ORDER BY clause display the rows in ASC|DESC order for an expression,column position or an alias. Syntax- SELECT *|[DISTINCT]Column|expression [Alias] From Table_Name [WHERE <Condition(s)>] [ORDER BY Column|Expression [ASC|DESC]]; SELECT -a list of columns * -Select the all columns DISTINCT-suppresses the duplicates alias-alternate name of the colunm(s) that is/are used as the heading of that column(s). Table_name-specifies the table name containing the column(s). WHERE<Condition(s)>- the condition must meet to retrieve the data from the database. ORDER BY Column|Expression -order the rows in Acending or descending order Example- 1.Write the query to find employee information order by name. Query- SELECT * FROM employee order by name; Output- Number of Records: 5 ID Name email address phone_number salary 2 Ajay ajay@gmail.com Hyderbad 16756890 51000 4 Aman Aman@gmail.com Lucknow 26756890 51000 3 Amit amit@gmail.com Delhi 16756890 51000 5 Hari Hari@gmail.com Lucknow 36756890 15000 1 sharat sarat@gmail.com Hyderbad 123456890 5000 2.Write the query to selects all employees from the "employee" table, sorted by the "Name" and the "Salary" column. Query- SELECT * FROM employee order by name, salary; Output- Number of Records: 5 ID Name email address phone_number salary 2 Ajay ajay@gmail.com Hyderbad 16756890 51000 4 Aman Aman@gmail.com Lucknow 26756890 51000 3 Amit amit@gmail.com Delhi 16756890 51000 5 Hari Hari@gmail.com Lucknow 36756890 15000 1 sharat sarat@gmail.com Hyderbad 123456890 5000 3.Display the name and the salary of the employees from the "employee" table, sorted ascending by the name and descending by the salary. Query- SELECT name,salary FROM employee order by name, salary DESC; Output- Number of Records: 5 Name salary Ajay 51000 Aman 51000 Amit 51000 Hari 15000 sharat 5000 4.Display the name and the yearly salary of the employees from the employee table and sort by Annual_salary in ascending. Query-SELECT Name , salary*12 Annual_salary FROM employee order by Annual_salary; Alternate -SELECT Name , salary*12 Annual_salary FROM employee order by 2; Output- Number of Records: 5 Name Annual_salary sharat 60000 Hari 180000 Ajay 612000 Amit 612000 Aman 612000 5.Display the name as Employee Name and the yearly salary as Annual_salary of the employees from the employee table and sorted descending order by the Annual_salary Query-SELECT Name "Employee Name", salary*12 Annual_salary FROM employee order by 2 DESC; Output- Number of Records: 5 Employee Name Annual_salary Ajay 612000 Amit 612000 Aman 612000 Hari 180000 sharat 60000
Showing posts with label ASC order DESC order. Show all posts
Showing posts with label ASC order DESC order. Show all posts
Wednesday, March 15, 2017
Sorting ASC,DESC-ORDER BY clause display the rows in ASC|DESC order for an expression,column position or an alias.
Subscribe to:
Posts (Atom)