I have 4 SQL server(2008 version) tables
1) USER- to store user information (Fields : UserId,UserName)
2) FILES - to store files uploaded by user (FileId,FileName,UserId)
3) PHOTOS -to store files uploaded by user (PhotoId,PhotoName,UserId)
4) GROUPS= to store groups created by user ( GroupId,GroupName,UserId)
Now I want to get a USER record with id=5 along with Total number of Files uploaded by userid 5,Total Photos uploaded by user id 5,Total groups created by userid 5.Can i get all these with a single query ? OR should i write a SELECT COUNT of each table with USER ID=5 in the where clause ( 3 queries.. ?)
I can write an inner join with USER and FILES and do the count to get the number of files uploaded by a purticular user.But how to add the number of photos and number of groups to the result ?
What is the best approach to do this ?
Sample Data
USER
USERID USERNAME
------ ---------
1 Shyju
2 OMG
3 Gus
FILES
FILEID FILENAME USERID
------ -------- ------
101 Sample1 1
102 Secondone 1
103 NewOne 2
104 BetterOne 3
105 Ocean 3
106 Sea 3
GROUPS
GROUPID GROUPNAME USERID
------- --------- ------
51 Group-A 1
52 Group-B 2
53 Group-C 2
54 Group-D 2
55 Group-E 3
56 Group-F 3
The Result i am looking for is
For User ID=1
USERID USERNAME FILECOUNT GROUPCOUNT
------ -------- --------- ----------
1 Shyju 2 1
For UserId=2
USERID USERNAME FILECOUNT GROUPCOUNT
------ -------- --------- ----------
2 OMG 1 3