does the column we use in group by effect the sorting order of the table?

25 Views Asked by At

there are two tables

  1. teacher(id,dept,name,phone,mobile)
  2. dept(id,name)

here teacher.dept acts as a foreign key for dept table. now they are asking to show each department and the number of staff in the sorted manner. so here are two queries which produce same result

  1. select dept.name ,count(teacher.name) from teacher right join dept on(teacher.dept=dept.id) group by dept.id

  2. select dept.name ,count(teacher.name) from teacher right join dept on(teacher.dept=dept.id) group by teacher.dept order by count(teacher.name) desc

the above two queries produce the correct result. but i don't understand why i have to use the order by clause explicitly in query no 2 and why with out even mentioning order by clause the first query produced in the sorted manner. what makes the difference between the two and why??

can you please help me with this?

0

There are 0 best solutions below