SQL

SQL Query for Outputting Sorted Data Using ‘Group By’

Hello All,

Hope this post finds you in good health and spirit.

Structured Query Language, abbreviated as SQL, is a domain-specific language used in programming and designed for managing data held in a relational database management system, or for stream processing in a relational data stream management system.

An good developer must be familiar with a few commands in SQL because it is such a strong development tool. The SQL queries listed below are important for coding and optimization. Almost every system that interacts with a SQL database will be affected by each query in the SQL tutorial.

SQL Query for Outputting Sorted Data Using ‘Group By’

The ‘Group By’ property groups the resulting data according to the specified attribute. The SQL query below will select Name, Age columns from the Patients table, then will filter them by Age value to include records where Age is more than 40 and then will group records with similar Age value and then finally will output them sorted by Name. The basic rule is that the group by clause should always follow a where clause in a Select statement and must precede the Order by clause.

SELECT Name, Age FROM Patients WHERE Age > 40
GROUP BY Name, Age ORDER BY Name;

Another sample of use of Group By: this expression will select records with a price lesser than 70 from the Orders table, will group records with a similar price, will sort the output by price, and will also add the column COUNT(price) that will display how many records with similar price were found:

SELECT COUNT(price), price FROM orders 
WHERE price < 70 GROUP BY price ORDER BY price

Note: you should use the very same set of columns for both SELECT and GROUP BY commands, otherwise you will get an error. Many thanks to Sachidannad for pointing it out!

SQL Queries for Data Manipulation Using Math Functions

There are a lot of built-in math functions like COUNT and AVG which provide basic functionalities of counting the number of results and averaging them respectively.

So, that’s all in this blog. I will meet you soon with next stuff .Have a nice day !!!

Recommended contents

Guys please don’t forget to like and share the post.Also join our WindowsTechno Community and where you can post your queries/doubts and our experts will address them .

You can also share the feedback on below windows techno email id.

If you have any questions feel free to contact us on admin@windowstechno.com also follow us on facebook@windowstechno to get updates about new blog posts.

How useful was this post?

Click on a star to rate it!

As you found this post useful...

Follow us on social media!

Was this article helpful?
YesNo

Leave a Reply

Check Also
Close
Back to top button