Use app×
Join Bloom Tuition
One on One Online Tuition
JEE MAIN 2025 Foundation Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
+1 vote
520 views
in Computer by (49.7k points)

Write the differences between order by and group by commands with example.

1 Answer

+2 votes
by (49.6k points)
selected by
 
Best answer

SQL ORDER BY - clause is used in a SELECT statement to sort results either in ascending or descending order.

The syntax for using SQL ORDER BY clause to sort data is:
> SELECT column-list FROM table_name ORDER BY column1, column2,.. column [DESC]];
For Example: If you want to sort the employee table by salary of the employee, the SQL query would be.
> SELECT name, salary FROM employee ORDER BY salary;
By default, the. ORDER BY Clause sorts data in ascending order. If data to be sorted in descending order, the command would be as given- below
> SELECT name, salary FROM employee ORDER BY name, salary DESC;
SQL GROUP BY Clause -
The SQL GROUP BY Clause is used with the group functions to retrieve data grouped according to one or more columns.
The basic syntax of GROUP BY clause is given below. The GROUP BY clause must follow the conditions in the WHERE clause and must precede the ORDER BY clause if one is used.
> SELECT column1, column2 FROM table_name WHERE [ conditions ]
GROUP BY column1, column2
ORDER BY column1, column2
For Example: If you want to know the total amount of salary spent on each department, the query would be:
> SELECT dept, SUM (salary) FROM employee GROUP BY dept;
The group by clause should contain all the columns in the select list expect those used along with the group functions.
> SELECT location, dept, SUM (salary) FROM employee GROUP BY location, dept;

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

Categories

...