exam questions

Exam 70-761 All Questions

View all questions & answers for the 70-761 exam

Exam 70-761 topic 1 question 43 discussion

Actual exam question from Microsoft's 70-761
Question #: 43
Topic #: 1
[All 70-761 Questions]

You have a database named MyDb. You run the following Transact-SQL statements:

A value of 1 in the IsActive column indicates that a user is active.
You need to create a count for active users in each role. If a role has no active users. You must display a zero as the active users count.
Which Transact-SQL statement should you run?

A.

B.

C.

D.

Show Suggested Answer Hide Answer
Suggested Answer: C

Comments

Chosen Answer:
This is a voting comment (?). It is better to Upvote an existing comment if you don't have anything to add.
Switch to a voting comment New
mattia_88
Highly Voted 5 years, 8 months ago
the correct is A. "If a role has no active users. You must display a zero as the active users count" WITH COUNT(*) RETURN ALWAYS 1
upvoted 12 times
...
mikemike
Highly Voted 5 years, 9 months ago
explanation: COUNT(*) – Returns the total number of records in a table (Including NULL valued records). COUNT(Column Name) – Returns the total number of Non-NULL records
upvoted 8 times
...
Billybob0604
Most Recent 4 years, 5 months ago
"If a role has no active users. You must display a zero as the active users count." You can only achieve this if you do a count on userid, so the answer has to be A.
upvoted 1 times
...
Vermonster
4 years, 5 months ago
Correct answer is A although you can't GROUP BY roleID since it isn't in the select clause
upvoted 1 times
...
Kiruu23
4 years, 10 months ago
Correct Ans is A we have to count the no. of active users so, COUNT(UserID) will also work.
upvoted 1 times
Kiruu23
4 years, 10 months ago
Yes i have tested it. The correct ans is A. coz Count(*) will return 1 when there is no active user not 0.
upvoted 1 times
...
...
Deimy
4 years, 10 months ago
tested. the answer is "A"
upvoted 2 times
...
stm22
4 years, 11 months ago
B: inner join omits roles with no active users C: group by has r.roleid...that is wrong D: cross join has no "ON" clause thus A is best answer
upvoted 2 times
...
MML
5 years, 1 month ago
the answer is A
upvoted 4 times
...
truthee
5 years, 1 month ago
CREATE TABLE tblRole ( RoleID int NOT NULL IDENTITY(1,1) PRIMARY KEY CLUSTERED, RoleName varchar(20) NOT NULL ) CREATE TABLE tblUsers ( UserID int NOT NULL IDENTITY(1000,1) PRIMARY KEY CLUSTERED, UserName varchar(20) UNIQUE NOT NULL, RoleID int NULL FOREIGN KEY REFERENCES tblRole(RoleID), IsActive bit NOT NULL DEFAULT(1) ) INSERT INTO tblRole VALUES ('Supervisory'), ('Management'), ('HR'), ('Accounting') INSERT INTO tblUsers VALUES ('Grakie', 2, DEFAULT), ('Jen', 2, 0), ('Bar', 2, DEFAULT), ('Jude', 3, DEFAULT), ('Paoula', 4, 0 ), ('Gooses', 3, DEFAULT), ('Harri', 3, DEFAULT) SELECT R.RoleName, ISNULL (U.ActiveUserCount,0) AS ActiveUserCount FROM tblRole R LEFT JOIN (SELECT RoleId, COUNT(*) AS ActiveUserCount FROM tblUsers WHERE IsActive = 1 GROUP BY RoleId) U ON r.RoleID = U.RoleID
upvoted 5 times
PHaringsNL
4 years, 7 months ago
The correct Answer is A. If you would choose C the NULL values are recorded as 1 not as 0. SELECT R.RoleName, COUNT(U.UserId) AS ActiveUserCount FROM tblRole R LEFT JOIN (SELECT UserId, RoleId FROM tblUsers WHERE IsActive = 1) U ON U.RoleID = R.RoleID GROUP BY R.RoleID, R.RoleName SELECT R.RoleName, COUNT(*) AS ActiveUserCount FROM tblRole R LEFT JOIN (SELECT UserId, RoleId FROM tblUsers WHERE IsActive = 1) U ON U.RoleID = R.RoleID GROUP BY R.RoleID, R.RoleName
upvoted 1 times
...
...
fabzo
5 years, 8 months ago
How does C return a 0 when a role has no active user?
upvoted 2 times
Hoglet
4 years, 6 months ago
It won't
upvoted 1 times
...
...
mattia_88
5 years, 8 months ago
because the record in table Roles is always present
upvoted 2 times
...
Dieter
5 years, 10 months ago
Any ideas for an explanatio why you cannot use Count(UserID)?
upvoted 2 times
Andy7622
4 years, 7 months ago
it's left join . We need to count all rows for each group or roles. not the rows which have userID not NULL
upvoted 1 times
...
AshleyLiang
5 years, 10 months ago
Actually we should use COUNT(U.UserID) as included in option A. It fulfills the requirement that when no active user it returns a 0, while COUNT(*) returns 1.
upvoted 25 times
...
...
Community vote distribution
A (35%)
C (25%)
B (20%)
Other
Most Voted
A voting comment increases the vote count for the chosen answer by one.

Upvoting a comment with a selected answer will also increase the vote count towards that answer by one. So if you see a comment that you already agree with, you can upvote it instead of posting a new comment.

SaveCancel
Loading ...