MySQL | Order By Complete Explanation | The SQL Gym

Execute following code in MySQL WorkBench, and Let’s start with exercise

create database if not exists tg117;

use tg117;

create table employee(eid int,ename varchar(50),city varchar(20),dept varchar(20),salary float) ;
insert into employee(eid,ename,city,dept,salary) values
(1,'Bahubali','mahishmati','DEV',5.8),
(2,'Munna Bhaiyya', 'mirzapur','HR',3.6),
(3,'Devasena','pune','HR',3.2),
(4,'Bhallaldev','mahishmati','ADMIN',4.1),
(5,'Jethalal','Ahemdabad','SALES',5.2),
(5,'Sunderlal','Ahemdabad','SALES',5.1),
(6,'Atmaram Bhide','pune','HR',4.8);

select * from employee ;

Now Let’s Start with OrderBy Queries :

  1. Select ename,dept,salary from employee ordered by salary (low to high)
    mysql> select ename,dept,salary from employee order by salary ;
    mysql> select ename,dept,salary from employee order by salary asc;
  2. Select ename,dept from employee ordered by salary (low to high)
    mysql> select ename,dept from employee order by salary ;
    mysql> select ename,dept from employee order by salary asc;
  3. Select ename,dept from employee ordered by salary (high to low)
    mysql> select ename,dept,salary from employee order by salary desc;
  4. Select ename,city from employee ordered by city and salary (both asc)
    mysql> select ename,city,salary from employee order by city, salary ;
    mysql> select ename,city,salary from employee order by city asc, salary asc;
  5. Select ename,city,salary from employee ordered by city and salary (both desc)
    mysql> select ename,city,salary from employee order by city desc, salary desc;
  6. Select ename,city from employee ordered by city and salary (city asc, salary desc)
    mysql> select ename,city,salary from employee order by city, salary desc;
    mysql> select ename,city,salary from employee order by city asc, salary desc;
  7. Select ename,city from employee ordered by city and salary (city desc, salary asc)
    mysql> select ename,city,salary from employee order by city desc, salary ;
    mysql> select ename,city,salary from employee order by city desc, salary asc;

Leave a Reply

Your email address will not be published. Required fields are marked *