Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQL - How To Order Using Count From Another Table

Tags:

1. Bloggers

blogger_id 1  2 3 

2. Posts

post_from_blogger_id 1  1 1 2 2 3 

As you can see blogger №1 posted more than the others and blogger №3 less. The question is how to build a query that selects all bloggers and sorts them by the number of their posts?

like image 532
Denis Bobrovnikov Avatar asked Jan 24 '11 16:01

Denis Bobrovnikov


People also ask

How do I count data from another table in SQL?

You can use Common Table Expression(Cte) to write such query with readability. ;With CteFamily AS ( SELECT family_id FROM dbo. Families --WHERE --Put your conditions to filter family ), --get childrens count, with family id for selected family CteChildrenCount AS ( SELECT family_id , Count(*) As ChildrenCount FROM dbo.

How do I use count by order in SQL?

Use ORDER BY with DESC to order in descending order. For counting the values, use the COUNT(). For example, if the name “John” appears thrice in the column, then a separate column will display the count 3 and in this way all the count values will be arranged in descending order using the ORDER BY DESC.

Can we use count in ORDER BY clause?

For uninitiated, a COUNT() function is used to find the total number of records in the result set. It is usually used in combination with the GROUP BY clause to prepare a summary of the total number of records in each group. It can further be used with ORDER BY clause to sort the obtained summary table.

Can you GROUP BY count in SQL?

SQL – count() with Group By clause The count() function is an aggregate function use to find the count of the rows that satisfy the fixed conditions. The count() function with the GROUP BY clause is used to count the data which were grouped on a particular attribute of the table.

How to order data by Count in SQL?

How to Order by Count in SQL? You aggregated data into groups, but you want to sort the records in descending order by the number of elements in the groups. Our database has a table named user with data in the following columns: id, first_name, last_name, and country.

How do you use count by and group by in SQL?

COUNT() with GROUP by. The use of COUNT() function in conjunction with GROUP BY is useful for characterizing our data under various groupings. A combination of same values (on a column) will be treated as an individual group.

How do I get the number of rows in an SQL table?

SQL COUNT (*) example. To get the number of rows in the employees table, you use the COUNT (*) function table as follows: SELECT COUNT (*) FROM employees; See it in action. To find how many employees who work in the department id 6, you add the WHERE clause to the query as follows: SELECT COUNT (*) FROM employees WHERE department_id = 6;

How do you sort rows in SQL with order by?

SQL COUNT(*) with ORDER BY clause example. You can use the COUNT(*) function in the ORDER BY clause to sort the number of rows per group. For example, the following statement gets the number of employees for each department and sorts the result set based on the number of employees in descending order.


1 Answers

 SELECT bloggers.*, COUNT(post_id) AS post_count     FROM bloggers LEFT JOIN blogger_posts      ON bloggers.blogger_id = blogger_posts.blogger_id     GROUP BY bloggers.blogger_id     ORDER BY post_count 

(Note: MySQL has special syntax that lets you GROUP BY without aggregating all values, it's intended for exactly this situation).

like image 143
Larry Lustig Avatar answered Oct 03 '22 11:10

Larry Lustig