SQL in Deep

SQL commands are the only one way to retrive,update,delete data from database.As I worked with several small projects where I have to play with sql quiries.A strong query can minimize hard work of the developer,even can make an application dynamic and well structured.Couple of months ago Saikat and me was wondering to find out the higest value of a field that means in a field the higest number of entity was inserted.Expalin with an example suppose a table is “A” and in this table a field is “B”,B is filled with respectively a,a,b,c,s,s,s,a,x so I have to find out which value is populated the field “B” most of the time?The real life problem I faced during my “Blood Donation Management System” that I have to find out the Blood Group that is most available(frequent blood_group) in the system.So I tried with my sql knowledge(basics L).Then just googled,but unfortunately I found nothing.Then I just pack the problem L.After couple of days just trying to learn the SQL queries and practice in to HeidiSQL.Just got an old function “count” and trying to play with that.Suddenly I tought “Why I should not solve the problem with this COUNT?”

My table name is:’info’;
Field name is : ‘blood_group’;
SQL QUERY>>


 ”select blood_group,count(*) from info group by blood_group”;

Output>>

blood_group Count(*)
O+ 3
A- 10
B+ 1
B- 1
Ab+ 1
A+ 1
O- 1


 

 

 

 

 

The next step to add order by,limt to find out the most frequent value.

”select blood_group,count(*) from info group by blood_group order by count(*) desc limit 0,1”;

Output>>

bood_group Count(*)
A- 10

 

 

 

If I wanted to to know the least number of entity in the blood_group field,the command wil be as belows:

”select blood_group,count(*) from info group by blood_group order by count(*) asc limit 0,1”;

That’s all  🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s