exam questions

Exam 70-761 All Questions

View all questions & answers for the 70-761 exam

Exam 70-761 topic 1 question 64 discussion

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

Note: This question is part of a series of questions that present the same scenario. Each question in the series contains a unique solution that might meet the stated goals. Some question sets might have more than one correct solution, while others might not have a correct solution.
After you answer a question in this section. You will NOT be able to return to it. As a result, these questions will not appear in the review screen.
You have a database that includes the tables shown in the exhibit (Click the Exhibit button.)

You need to create a Transact-SQL query that returns the following information:
✑ the customer number
✑ the customer contact name
✑ the date the order was placed, with a name of DateofOrder
✑ a column named Salesperson, formatted with the employee first name, a space, and the employee last name
✑ orders for customers where the employee identifier equals 4
The output must be sorted by order date, with the newest orders first.
The solution must return only the most recent order for each customer.
Solution: You run the following Transact-SQL statement:

Does the solution meet the goal?

  • A. Yes
  • B. No
Show Suggested Answer Hide Answer
Suggested Answer: B 🗳️
We should use a WHERE clause, not a HAVING clause. The HAVING clause would refer to aggregate data.

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
chaoxes
Highly Voted 4 years, 11 months ago
I tested it and it works good so the answer is A. Yes. ------------------------------------------------------------------ CREATE TABLE #cust ( custid int ,contactname varchar(100) ) INSERT INTO #cust VALUES (1, 'A') ,(2, 'B') ,(3, 'C') CREATE TABLE #orders ( custid int ,orderid int ,orderdate date ,empid int ) INSERT INTO #orders VALUES (1,991,getdate(),4) ,(1,992,getdate(),4) ,(3,993,getdate(),4) CREATE TABLE #employees ( empid int ,firstname varchar(100) ,lastname varchar(100) ) INSERT INTO #employees VALUES (4, 'AB', 'CD') ,(5, 'AA', 'BB') SELECT c.custid, contactname, MAX(orderdate) AS DateOfOrder, e.firstname + ' ' + e.lastname as Salesperson FROM #cust AS c INNER JOIN #orders AS o ON c.custid = o.custid INNER JOIN #employees AS e ON o.empid = e.empid GROUP BY c.custid, contactname, firstname, lastname, o.empid HAVING o.empid = 4 ORDER BY DateofOrder DESC
upvoted 6 times
...
Billybob0604
Most Recent 4 years, 5 months ago
the o.empid in the group by is not in the select..................
upvoted 1 times
...
Vermonster
4 years, 5 months ago
Absolutely A. HAVING filters after the aggregation occurs but can be applied to any column. It may be less efficient than WHERE when applied to a non-aggregated column (because you included data to later filter it out) but returns the same result
upvoted 2 times
...
SimSql
4 years, 6 months ago
I tested it on the TSQLV4 database, all the tables and columns are from this database. The code works fine and the answer is 100% A.
upvoted 2 times
...
Andy7622
4 years, 7 months ago
Using empid either in WHERE or HAVING clause gives the same result set I checked it on the Sample database for these queries were created. As far as it is mentioned in requirements that 'Some question sets might have more than one correct solution' and nothing said about performance requirements for the query I can suggest that both are valid solutions .
upvoted 1 times
...
AEscajeda
4 years, 8 months ago
The format of full name is not the correct, there is not a blank space between first and last name, so even if the having clause works, this statement does not meet the speficiations.
upvoted 1 times
...
Oooo
4 years, 9 months ago
Let me clarify here- The Tsql mentioned meets the solution however its not an optimal solution. The reason to this is when you use having clause, the sql server has already performed all the operation like joining then grouping, then the filtering is done. This is looked with a performance point of view it hurts. But on the other hand if where is used before Grouping by then already the rows get filtered and then grouping is done- this is any time better than first option. So the answer is not about the solution its about optimal Solution.
upvoted 1 times
BabyBee
4 years, 6 months ago
But where does it asks if it is an optimal solution?
upvoted 2 times
...
...
Backy
4 years, 11 months ago
The answer should be B The problem is not with HAVING The problem is with ORDER BY because it references column DateofOrder which is not included in GROUP BY
upvoted 2 times
Backy
4 years, 11 months ago
My wrong, DateofOrder is not a column, it is aggregate function around a column so the column does not have to be part of GROUP BY. In other words, both HAVING and ORDER BY are good, so the answer is A
upvoted 2 times
...
...
mamarach
5 years ago
It's NO bc at the level of GROUP BY, they added o.empid.
upvoted 2 times
...
Anette
5 years, 1 month ago
It doe not have to do with Where or Having clause. But the SalesPerson is not being fetched as first name space last name. SO THE ANSWER IS NOO
upvoted 1 times
Anette
5 years, 1 month ago
or better the space is not displayed
upvoted 1 times
...
...
Ricky7876
5 years, 1 month ago
I wonder if the space within the string concatenation is considered a space... In SSMS it doesn't look like it.
upvoted 2 times
...
humpp
5 years, 2 months ago
"The solution must return only the most recent order for each customer.", so why there is a empid in the group by?
upvoted 2 times
...
moehijawe
5 years, 5 months ago
The answer is yes, having is applied on aggregate data (means after grouping data) and can be apply on group by column
upvoted 3 times
jortizhuedo
5 years, 3 months ago
Where is more efficient than having... having only for agrupate clause.. for example count, sum... --> having count(balls)>5, having sum(balls)>300$. I think
upvoted 2 times
...
Vanesa30
5 years, 3 months ago
I think the clause where is should be applied instead of having because the performance of where is better than the other one. so it just retrieve a fewer data
upvoted 2 times
...
geekeek1
4 years, 5 months ago
agreed
upvoted 1 times
...
...
avramov
5 years, 7 months ago
YES, because you can use empid in Having if it is in the Group by clause i. e. having can contain aggregates and columns being used in Group by
upvoted 4 times
...
Abhilash_KK
5 years, 9 months ago
Alias name DateOfOrder cannot be used for order clause
upvoted 1 times
M4x
5 years, 9 months ago
Why not ? Try...
upvoted 5 times
...
Tr4ckz
5 years, 8 months ago
Order by is the only section that can use aliases defined in the SELECT part, as of all 6 parts, SELECT is processed 5th and ORDER by 6th and last
upvoted 2 times
...
Almosawi
5 years, 8 months ago
ORDDER BY clause is logically executed after SELECT clause which has ALIAS.
upvoted 2 times
...
Andy7622
4 years, 7 months ago
ORDER BY execution logically comes after SELECT so alias can be used in there
upvoted 2 times
...
...
exam_taker5
5 years, 11 months ago
Why is it an issue that HAVING refers to aggregate data? Wouldn't it still accomplish the goal?
upvoted 1 times
AshleyLiang
5 years, 10 months ago
Yes. The HAVING clause here is equivalent to WHERE as a filter.
upvoted 1 times
...
M4x
5 years, 9 months ago
WHERE clause is evaluated before the GROUP BY, consequently is more efficient and logical correct than the HAVING
upvoted 6 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 ...