“COUNT”, group by” and “having”

To find out the total number of rows in the table:

SELECT COUNT(*) FROM blog_articles;

or (“blogID” being the primary key):

"SELECT COUNT(blogID) from blog_articles";

The differences between COUNT(*) and COUNT(col_name) are:

1. COUNT (*) and COUNT(col_name) will NOT necessarily return the same result. COUNT(col_name) will NOT return NULL rows, while COUNT(*) will;

2. Performance wise, there is a big difference as COUNT(*) is read from table info, while COUNT(col) really counts the rows. So COUNT(*) would be faster.

To find out if there are stories with different url sharing the same headline (“title”):

"SELECT * FROM blog_articles GROUP BY title HAVING COUNT(url)>1;"

To find out the total number of different headlines(“title”):

"select count(title) as count from (select title from blog_articles group by title) t;"

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


%d bloggers like this: