SQL Count Rows From Multiple tables

Gabriel Matusevich

Lets say i have 2 tables

Companies

company_id    
name

Users

id
company_id
name

each company has multiple users assign to it... which is referenced in the company_id field from each record in the users table

HOW can i get a record showing the (company_id), (company_name) and (number or users)

for eg:

id# 1234 | name# Microsoft | n of users# 2000 

I dont know how to make this query, i know i have to use the function COUNT() but i dont know how

peterm

If you want to get all companies even if they don't have any users yet use OUTER JOIN

SELECT c.company_id, c.name company_name, COUNT(u.id) no_of_users
  FROM companies c LEFT JOIN users u
    ON c.company_id = u.company_id
 GROUP BY c.company_id, c.name

Sample output:

| COMPANY_ID | COMPANY_NAME | NO_OF_USERS |
|------------|--------------|-------------|
|          1 |     Company1 |           3 |
|          2 |     Company2 |           2 |
|          3 |     Company3 |           0 |

Here is SQLFiddle demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

sql - Get count of matching rows from multiple joined tables

From Dev

sql - Get count of matching rows from multiple joined tables

From Dev

sql count() from multiple tables

From Dev

COUNT rows from multiple joined tables - MSSQL

From Dev

MySQL count rows from multiple tables using join

From Dev

Select count dependent rows from multiple tables with union

From Dev

Count from multiple tables

From Dev

Count Rows on multiple tables for each of multiple ids

From Dev

Count Total Rows from Multiple Tables Which is Generated by Codeigniter -> list_tables()

From Dev

SQL Insert from 2 tables with multiple rows on subquery

From Dev

SQL Select rows and delete/update from dynamic multiple tables

From Dev

SQL: Fetch rows based on even, odd values from multiple tables

From Dev

SQL: Selecting count of multiple tables

From Dev

SQL - Join multiple tables and count()

From Dev

SQL count involving multiple tables

From Dev

Count Data from multiple tables

From Dev

Count Data from multiple tables

From Dev

Querying rows from multiple tables

From Dev

Count rows by date on multiple tables within a Big Query database using Legacy SQL

From Dev

Count rows from different tables in join statement

From Dev

Postgresql delete multiple rows from multiple tables

From Dev

Combine multiple rows from multiple tables in MySQL

From Dev

SQL count duplicate values across multiple tables

From Dev

SQL query, AVG and COUNT on multiple tables

From Dev

SQL Sum & Count with Multiple Conditions and Tables

From Dev

SQL Count Not working over multiple tables

From Dev

SQL Join on Multiple Tables with Rows Filtered on Condition

From Dev

Get count of foreign key from multiple tables

From Dev

Select row count from multiple tables

Related Related

  1. 1

    sql - Get count of matching rows from multiple joined tables

  2. 2

    sql - Get count of matching rows from multiple joined tables

  3. 3

    sql count() from multiple tables

  4. 4

    COUNT rows from multiple joined tables - MSSQL

  5. 5

    MySQL count rows from multiple tables using join

  6. 6

    Select count dependent rows from multiple tables with union

  7. 7

    Count from multiple tables

  8. 8

    Count Rows on multiple tables for each of multiple ids

  9. 9

    Count Total Rows from Multiple Tables Which is Generated by Codeigniter -> list_tables()

  10. 10

    SQL Insert from 2 tables with multiple rows on subquery

  11. 11

    SQL Select rows and delete/update from dynamic multiple tables

  12. 12

    SQL: Fetch rows based on even, odd values from multiple tables

  13. 13

    SQL: Selecting count of multiple tables

  14. 14

    SQL - Join multiple tables and count()

  15. 15

    SQL count involving multiple tables

  16. 16

    Count Data from multiple tables

  17. 17

    Count Data from multiple tables

  18. 18

    Querying rows from multiple tables

  19. 19

    Count rows by date on multiple tables within a Big Query database using Legacy SQL

  20. 20

    Count rows from different tables in join statement

  21. 21

    Postgresql delete multiple rows from multiple tables

  22. 22

    Combine multiple rows from multiple tables in MySQL

  23. 23

    SQL count duplicate values across multiple tables

  24. 24

    SQL query, AVG and COUNT on multiple tables

  25. 25

    SQL Sum & Count with Multiple Conditions and Tables

  26. 26

    SQL Count Not working over multiple tables

  27. 27

    SQL Join on Multiple Tables with Rows Filtered on Condition

  28. 28

    Get count of foreign key from multiple tables

  29. 29

    Select row count from multiple tables

HotTag

Archive