Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in SQL by (20.3k points)

When do you use which in general? Examples are highly encouraged!

I am referring so MySql, but can't imagine the concept being different on another DBMS

1 Answer

0 votes
by (40.7k points)

GROUP BY clause is used to aggregate records by the specified columns which allows you to perform aggregation functions on non-grouped columns 

(such as COUNT, AVG, SUM, etc). Whereas, ORDER BY clause is used to alter the order in which items are returned.

Try this:

TABLE:

ID NAME

1  Peter

2  John

3  Greg

4  Peter

SELECT *

FROM TABLE

ORDER BY NAME

3 Greg

2 John

1 Peter

4 Peter

SELECT Count(ID), NAME

FROM TABLE

GROUP BY NAME

1 Greg

1 John 

2 Peter

SELECT NAME

FROM TABLE

GROUP BY NAME

HAVING Count(ID) > 1

=

Peter

Related questions

0 votes
1 answer
0 votes
1 answer
asked Dec 26, 2020 in SQL by Appu (6.1k points)
0 votes
1 answer
asked Jul 9, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Jul 31, 2019 in SQL by Tech4ever (20.3k points)
0 votes
1 answer
asked Jul 11, 2019 in SQL by Tech4ever (20.3k points)

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...