Hi,
How to sort the results in MYSQL query?
Thanks
Hi,
You can use the order by clause in SQL statement to sort the data in ascending or descending orders.
Lets consider the following table:
CREATE TABLE employee ( id int(10), emp_name varchar(60), dob date );
Following query for entering the data:
insert into employee values(01,'Ashok','1981-12-20'); insert into employee values(02,'Kamlesh','1982-12-21'); insert into employee values(03,'Vishal','1981-12-21'); insert into employee values(04,'Sumit','2008-12-20'); insert into employee values(02,'Kim','1981-12-23'); insert into employee values(02,'Dinesh','1981-12-24'); insert into employee values(02,'Rajeev','2001-12-25'); insert into employee values(01,'John',' 2000-12-03');
Now you can get the data sorted by id using following query:
select * from employee order by id asc;
Following is other examples of sorting:
mysql> select * from employee order by id; +------+----------+------------+ | id | emp_name | dob | +------+----------+------------+ | 1 | Ashok | 1981-12-20 | | 1 | John | 2000-12-03 | | 2 | Kamlesh | 1982-12-21 | | 2 | Kim | 1981-12-23 | | 2 | Dinesh | 1981-12-24 | | 2 | Rajeev | 2001-12-25 | | 3 | Vishal | 1981-12-21 | | 4 | Sumit | 2008-12-20 | +------+----------+------------+ 8 rows in set (0.00 sec) mysql> select * from employee order by id asc; +------+----------+------------+ | id | emp_name | dob | +------+----------+------------+ | 1 | Ashok | 1981-12-20 | | 1 | John | 2000-12-03 | | 2 | Kamlesh | 1982-12-21 | | 2 | Kim | 1981-12-23 | | 2 | Dinesh | 1981-12-24 | | 2 | Rajeev | 2001-12-25 | | 3 | Vishal | 1981-12-21 | | 4 | Sumit | 2008-12-20 | +------+----------+------------+ 8 rows in set (0.01 sec) mysql> select * from employee order by id desc; +------+----------+------------+ | id | emp_name | dob | +------+----------+------------+ | 4 | Sumit | 2008-12-20 | | 3 | Vishal | 1981-12-21 | | 2 | Kamlesh | 1982-12-21 | | 2 | Kim | 1981-12-23 | | 2 | Dinesh | 1981-12-24 | | 2 | Rajeev | 2001-12-25 | | 1 | Ashok | 1981-12-20 | | 1 | John | 2000-12-03 | +------+----------+------------+ 8 rows in set (0.00 sec) mysql> select * from employee order by emp_name desc; +------+----------+------------+ | id | emp_name | dob | +------+----------+------------+ | 3 | Vishal | 1981-12-21 | | 4 | Sumit | 2008-12-20 | | 2 | Rajeev | 2001-12-25 | | 2 | Kim | 1981-12-23 | | 2 | Kamlesh | 1982-12-21 | | 1 | John | 2000-12-03 | | 2 | Dinesh | 1981-12-24 | | 1 | Ashok | 1981-12-20 | +------+----------+------------+ 8 rows in set (0.00 sec) mysql> select * from employee order by emp_name asc; +------+----------+------------+ | id | emp_name | dob | +------+----------+------------+ | 1 | Ashok | 1981-12-20 | | 2 | Dinesh | 1981-12-24 | | 1 | John | 2000-12-03 | | 2 | Kamlesh | 1982-12-21 | | 2 | Kim | 1981-12-23 | | 2 | Rajeev | 2001-12-25 | | 4 | Sumit | 2008-12-20 | | 3 | Vishal | 1981-12-21 | +------+----------+------------+ 8 rows in set (0.00 sec) mysql> select * from employee order by id,emp_name asc; +------+----------+------------+ | id | emp_name | dob | +------+----------+------------+ | 1 | Ashok | 1981-12-20 | | 1 | John | 2000-12-03 | | 2 | Dinesh | 1981-12-24 | | 2 | Kamlesh | 1982-12-21 | | 2 | Kim | 1981-12-23 | | 2 | Rajeev | 2001-12-25 | | 3 | Vishal | 1981-12-21 | | 4 | Sumit | 2008-12-20 | +------+----------+------------+ 8 rows in set (0.00 sec) mysql> select * from employee order by id,emp_name asc; +------+----------+------------+ | id | emp_name | dob | +------+----------+------------+ | 1 | Ashok | 1981-12-20 | | 1 | John | 2000-12-03 | | 2 | Dinesh | 1981-12-24 | | 2 | Kamlesh | 1982-12-21 | | 2 | Kim | 1981-12-23 | | 2 | Rajeev | 2001-12-25 | | 3 | Vishal | 1981-12-21 | | 4 | Sumit | 2008-12-20 | +------+----------+------------+ 8 rows in set (0.00 sec) mysql> select * from employee order by id,emp_name desc; +------+----------+------------+ | id | emp_name | dob | +------+----------+------------+ | 1 | John | 2000-12-03 | | 1 | Ashok | 1981-12-20 | | 2 | Rajeev | 2001-12-25 | | 2 | Kim | 1981-12-23 | | 2 | Kamlesh | 1982-12-21 | | 2 | Dinesh | 1981-12-24 | | 3 | Vishal | 1981-12-21 | | 4 | Sumit | 2008-12-20 | +------+----------+------------+ 8 rows in set (0.00 sec) mysql> select * from employee order by id,emp_name asc; +------+----------+------------+ | id | emp_name | dob | +------+----------+------------+ | 1 | Ashok | 1981-12-20 | | 1 | John | 2000-12-03 | | 2 | Dinesh | 1981-12-24 | | 2 | Kamlesh | 1982-12-21 | | 2 | Kim | 1981-12-23 | | 2 | Rajeev | 2001-12-25 | | 3 | Vishal | 1981-12-21 | | 4 | Sumit | 2008-12-20 | +------+----------+------------+ 8 rows in set (0.00 sec)
Thanks
Check following video:
Thanks
Ads