Arn Vanhoutte Arn Vanhoutte - 6 months ago 9
SQL Question

How to get amount of records of a specific ID in a database?

I want to check which users have the most records in a database. So every user has a specific Id, and that Id is used as a reference in a few tables.

There are a few tables that contain a column UserId, like the table Exams, Answers, Questions, Classes, etc. Is it possible to count all the records with a specific UserId in all those tables?

Answer
;with cte as (
select rowsCount = count(*) from A where UserId = 1
union all
select rowsCount = count(*) from B where UserId = 1
union all
select rowsCount = count(*) from C where UserId = 1
)
select sum(rowsCount) from cte
Comments