Querying Data with Transact-SQL v1.0 (70-761)

Page:    1 / 14   
Total 205 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.
A database has two tables as shown in the following database diagram:


You need to list all provinces that have at least two large cities. A large city is defined as having a population of at least one million residents. The query must return the following columns:
-> tblProvince.ProvinceId
-> tblProvince.ProvinceName
-> a derived column named LargeCityCount that presents the total count of large cities for the province
Solution: You run the following Transact-SQL statement:

Does the solution meet the goal?

  • A. Yes
  • B. No


Answer : B

Explanation:
The SQL CROSS JOIN produces a result set which is the number of rowsin the first table multiplied by the number of rows in the second table if no WHERE clause is used along with CROSS JOIN. This kind of result is called as Cartesian Product.
This is not what is required in this scenario.
References:
https://technet.microsoft.com/en-us/library/ms190690(v=sql.105).aspx

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.
A database has two tables as shown in the following database diagram:


You need to list all provinces that have at least two large cities. A large city is defined as having a population of at least one million residents. The query must return the following columns:
-> tblProvince.ProvinceId
-> tblProvince.ProvinceName
-> a derived column named LargeCityCount that presents the total count of large cities for the province
Solution: You run the following Transact-SQL statement:

Does the solution meet the goal?

  • A. Yes
  • B. No


Answer : A

Explanation:
The requirement to list all provinces that have at least two large cities is meet by the WHERE CitySummary.LargeCityCount >=2 clause.
CROSS APPLY willwork fine here.
Note:
The APPLY operator allows you to invoke a table-valued function for each row returned by an outer table expression of a query. The table-valued function acts as the right input and the outer table expression acts as the left input. The right input is evaluated for each row from the left input and the rows produced are combined for the final output. The list of columns produced by the APPLY operator is the set of columns in the left input followed by the list of columns returned by the right input.
There are two forms of APPLY: CROSS APPLY and OUTER APPLY. CROSS APPLY returns only rows from the outer table that produce a result set from the table-valued function. OUTER APPLY returns both rows that produce a result set, and rows that do not, with NULL values in the columns produced by the table- valued function.
References:
https://technet.microsoft.com/en-us/library/ms175156(v=sql.105).aspx

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 tracks orders and deliveries for customers in North America. The database contains the following tables:

Sales.Customers -



Application.Cities -


Sales.CustomerCategories -

The company’s development team is designing a customer directory application. The application must list customers by the area code of their phone number. The area code is defined as the first three characters of the phone number.
The main page of the application will be based on an indexed view that contains the area and phone number for all customers.
You need to return the area code from the PhoneNumber field.
Solution: You run the following Transact-SQL statement:

Does the solution meet the goal?

  • A. Yes
  • B. No


Answer : A

Explanation:
The following indicates a correct solution:
-> The function returns a nvarchar(10) value.
-> Schemabinding is used.
-> SELECT TOP 1 … gives a single value
Note: nvarchar(max) is correct statement.
nvarchar [ ( n | max ) ]
Variable-length Unicode string data. n defines the string length and can be a value from 1 through 4,000. max indicates that the maximum storage size is 2^31-1 bytes (2 GB).
References:
https://docs.microsoft.com/en-us/sql/t-sql/data-types/nchar-and-nvarchar-transact-sql https://sqlstudies.com/2014/08/06/schemabinding-what-why/

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


Answer : A

Explanation:
Complaints must be returned even if no interaction has occurred.

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


Answer : A

Explanation:
The MAX(orderdate) in the SELECT statement makes sure we return only the most recent order.
AWHERE o.empiD =4 clause is correctly used.
GROUP BY is also required.

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


Answer : B

Explanation:
We need a GROUP BY statement as we want to return an order for each customer.

DRAG DROP -
You have two tables named UserLogin and Employee respectively.
You need to create a Transact-SQL script that meets the following requirements:
-> The script must update the value of the IsDeleted column for the UserLogin table to 1 if the value of the Id column for the UserLogin table is equal to 1.
-> The script must update the value of the IsDeleted column of the Employee table to 1 if the value of the Id column is equal to 1 for the Employee table when an update to the UserLogin table throws an error.
-> The error message “No tables updated!†must be produced when an update to the Employee table throws an error.
Which five Transact-SQL segments should you use to develop the solution? To answer, move the appropriate Transact-SQL segments from the list of Transact-
SQL segments to the answer area and arrange them in the correct order.
Select and Place:




Answer :

You need to create a table named MiscellaneousPayment that meets the following requirements:


Which Transact-SQL statement should you run?

A.

B.

C.

D.

E.

F.



Answer : D

Explanation:
Incorrect Answers:
A: For column Reason we must use nvarchar, not varchar, as multilingual values must be supported. NEWSEQUENTIALID cannot be referenced in queries. In addition, the money datatype uses rounding and will result in rounding errors.
B: We cannot use INT for the Id column as new values must be automatically generated.
C: For column Reason we must use nvarchar, not varchar, as multilingual values must be supported.
E: NEWSEQUENTIALID cannot be referenced in queries.
F: The money datatype uses rounding and will result in rounding errors. We should use decimal instead.
Note: Nvarchar stores UNICODE data. If you have requirements to store UNICODE or multilingual data, nvarchar is the choice. Varchar stores ASCII data and should be your data type of choice for normal use.
References:
https://docs.microsoft.com/en-us/sql/t-sql/data-types/nchar-and-nvarchar-transact-sql

SIMULATION -
You have a database that contains the following tables.


You need to create a query that returns each complaint, the names of the employees handling the complaint, and the notes on each interaction. The Complaint field must be displayed first, followed by the employee’s name and the notes. Complaints must be returned even if no interaction has occurred.
Construct the query using the following guidelines:
-> Use two-part column names.
-> Use one-part table names.
-> Use the first letter of the table name as its alias.
-> Do not Transact-SQL functions.
-> Do not use implicit joins.
-> Do not surround object names with square brackets.
Part of the correct Transact-SQL has been provided in the answer area below. Enter the code in the answer area that resolves the problem and meets the stated goals or requirements. You can add code within the code that has been provided as well as below it.

1 SELECT c.Complaint, e.Name, i.Notes
2 FROM Complaints c
3 JOIN __________________
4 JOIN __________________



Answer : Please see explanation

Explanation:
1 SELECT c.Complaint, e.Name, i.Notes
2 FROM Complaints c
3 LEFT JOIN Interactions i ON c.ComplaintID = i.ComplaintID
4 LEFT JOIN Employees e ON i.EmployeeID = E.EmployeeID
Complaints must be returned even if no interaction has occurred, so we must use the LEFT JOIN, instead of just JOIN.
Note: You can drop the OUTER in LEFT OUTER JOIN, as has been done here on line 3 and line 4.

SIMULATION -
You create a table named Products.Sales by running the following Transact-SQL statement:


You add the following data to the table.

You are developing a report to display monthly sales data.
You need to create a Transact-SQL query to meet the following requirements:
-> Retrieve a column for the year followed by a column for each month from January through December.
-> Include the total sales amount for each month.
Aggregate columns by year, month, and then amount.

Construct the query using the following guidelines:
-> Use the MONTH keyword as the interval when using the DATANAME function.
-> Do not modify the provided IN clause.
-> Do not surround object names with square brackets.
-> Do not use implicit joins.
-> Do not use the DATEPART function.
Part of the correct Transact-SQL has been provided in the answer area below. Enter the code in the answer area that resolves the problem and meets the stated goals or requirements. You can add code within the code that has been provided as well as below it.
1. SELECT * FROM
2. (SELECT YEAR(SalesData)) AS Year, DATENAME (MONTH, SalesDate) AS Month, SalesAmount AS Amount
3.
4. ) AS MonthlySalesData
5.
6. FOR Month IN (January, February, March, April, May, June, July, August, September, October, November, December))

AS MonthNamePivot -



Answer : Please see explanation

Explanation:
1 SELECT * FROM
2 (SELECT YEAR(SalesData)) AS Year, DATENAME (MONTH, SalesDate) AS Month, SalesAmount AS Amount
3 FROM Products.Sales
4 ) AS MonthlySalesData
5 PIVOT SUM(Amount)
6 FOR Month IN (January, February, March, April, May, June, July, August, September, October, November, December))

AS MonthNamePivot -

Note: This question is part of a series of questions that use the same or similar answer choices. An answer choice may be correct for more than one question in the series. Each question is independent of the other questions in this series. Information and details provided in a question apply only to that question.
Multiple processes use the data from a table named Sales and place it in other databases across the organization. Some of the processes are not completely aware of the data types in the Sales table. This leads to data type conversion errors.
You need to implement a method that returns a NULL value id data conversion fails instead of throwing an error.
What should you implement?

  • A. the COALESCE function
  • B. a view
  • C. a table-valued function
  • D. the TRY_PARSE function
  • E. a stored procedure
  • F. the ISNULL function
  • G. a scalar function
  • H. the TRY_CONVERT function


Answer : H

Explanation:
TRY_CONVERT returns a value cast to the specified data type if the cast succeeds; otherwise, returns null.
References:
https://docs.microsoft.com/en-us/sql/t-sql/functions/try-convert-transact-sql

You have a database that contains the following tables:


You need to write a query that returns a list of all customers who have not placed orders.
Which Transact-SQL statement should you run?

  • A. SELECT c.custid FROM Sales.Customers c INNER JOIN Sales.Order o ON c.custid = o.custid
  • B. SELECT custid FROM Sales.Customers INTERSECT SELECT custid FROM Sales.Orders
  • C. SELECT c.custid FROM Sales.Customers c LEFT OUTER Sales.Order o ON c.custid = o.custid
  • D. SELECT c.custid FROM Sales.Customers c LEFT OUTER JOIN Sales.Order o ON c.custid = o.custid WHERE orderid IS NULL
  • E. SELECT custid FROM Sales.Customers UNION ALL SELECT custid FROM Sales.Orders
  • F. SELECT custid FROM Sales.Customers UNION SELECT custid FROM Sales.Orders
  • G. SELECT c.custid FROM Sales.Customers c RIGHT OUTER JOIN Sales.Orders o ON c.custid = o.custid


Answer : D

Explanation:
Inner joins return rows only when there is at least one row from both tables that matches the join condition. Inner joins eliminate the rows that do not match with a row from the other table. Outer joins, however, return all rows from at least one of the tables or views mentioned in the FROM clause, as long as those rows meet any WHERE or HAVING search conditions. All rows are retrieved from the left table referenced with a left outer join, and all rows from the right table referenced in a right outer join. All rows from both tables are returned in a full outer join.
References:
https://technet.microsoft.com/en-us/library/ms187518(v=sql.105).aspx

DRAG DROP -
You have a database that contains the following tables:


A delivery person enters an incorrect value for the CustomerID column in the Invoices table and enters the following text in the ConfirmedReceivedBy column: “Package signed for by the owner Tim.â€
You need to find the records in the Invoices table that contain the word Tim in the CustomerName field along with the incorrectly entered row from the
Sales.Invoice table.
How should you complete the Transact-SQL statement? To answer, drag the appropriate Transact-SQL segments to the correct locations. Each Transact-SQL segment may be used once, more than once, or not at all. You may need to drag the split bar between panes or scroll to view content.
NOTE: Each correct selection is worth one point.
Select and Place:



Answer :

DRAG DROP -
Note: This question is part of a series of questions that use the same scenario. For your convenience, the scenario is repeated in each question. Each question presents a different goal and answer choices, but the text of the scenario is exactly the same in each question in this series.

Start of repeated scenario -
You have a database that contains the tables shown in the exhibit. (Click the Exhibit button.)


You review the Employee table and make the following observations:
-> Every record has a value in the ManagerID except for the Chief Executive Officer (CEO).
-> The FirstName and MiddleName columns contain null values for some records.
-> The valid values for the Title column are Sales Representative manager, and CEO.
You review the SalesSummary table and make the following observations:
-> The ProductCode column contains two parts: The first five digits represent a product code, and the last seven digits represent the unit price. The unit price uses the following pattern: ####.##.
-> You observe that for many records, the unit price portion of the ProductCode column contains values.
-> The RegionCode column contains NULL for some records.
-> Sales data is only recorded for sales representatives.
You are developing a series of reports and procedures to support the business. Details for each report or procedure follow.
Sales Summary report: This report aggregates data by year and quarter. The report must resemble the following table.

Sales Manager report: This report lists each sales manager and the total sales amount for all employees that report to the sales manager.
Sales by Region report: This report lists the total sales amount by employee and by region. The report must include the following columns: EmployeeCode,
MiddleName, LastName, RegionCode, and SalesAmount. If MiddleName is NULL, FirstName must be displayed. If both FirstName and MiddleName have null values, the world Unknown must be displayed/ If RegionCode is NULL, the word Unknown must be displayed.
Report1: This report joins data from SalesSummary with the Employee table and other tables. You plan to create an object to support Report1. The object has the following requirements:
-> be joinable with the SELECT statement that supplies data for the report
-> can be used multiple times with the SELECT statement for the report
-> be usable only with the SELECT statement for the report
-> not be saved as a permanent object
Report2: This report joins data from SalesSummary with the Employee table and other tables.
You plan to create an object to support Report1. The object has the following requirements: be joinable with the SELECT statement that supplies data for the report
-> can be used multiple times for this report and other reports
-> accept parameters
-> be saved as a permanent object
Sales Hierarchy report: This report aggregates rows, creates subtotal rows, and super-aggregates rows over the SalesAmount column in a single result-set. The report uses SaleYear, SaleQuarter, and SaleMonth as a hierarchy. The result set must not contain a grand total or cross-tabulation aggregate rows.
Current Price Stored Procedure: This stored procedure must return the unit price for a product when a product code is supplied. The unit price must include a dollar sign at the beginning. In addition, the unit price must contain a comma every three digits to the left of the decimal point, and must display two digits to the left of the decimal point. The stored procedure must not throw errors, even if the product code contains invalid data.

End of Repeated Scenario -
You need to create a query to return the data for the Sales Summary report.
Which three Transact-SQL segments should you use to develop the solution? To answer, move the appropriate Transact-SQL segments from the list of Transact-
SQL segments to the answer area and arrange them in the correct order.
Select and Place:



Answer :

Explanation:
Use two CTEexpressions, one for salesYear and one for SalesQuarter, and combine them with a SELECT statement.
Note: A common table expression (CTE) can be thought of as a temporary result set that is defined within the execution scope of a single SELECT, INSERT,
UPDATE, DELETE, or CREATE VIEW statement. A CTE is similar to a derived table in that it is not stored as an object and lasts only for the duration of the query.
References:
https://technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx

HOTSPOT -
Note: This question is part of a series of questions that use the same scenario. For your convenience, the scenario is repeated in each question. Each question presents a different goal and answer choices, but the text of the scenario is exactly the same in each question in this series.

Start of repeated scenario -
You have a database that contains the tables shown in the exhibit. (Click the Exhibit button.)


You review the Employee table and make the following observations:
-> Every record has a value in the ManagerID except for the Chief Executive Officer (CEO).
-> The FirstName and MiddleName columns contain null values for some records.
-> The valid values for the Title column are Sales Representative manager, and CEO.
You review the SalesSummary table and make the following observations:
-> The ProductCode column contains two parts: The first five digits represent a product code, and the last seven digits represent the unit price. The unit price uses the following pattern: ####.##.
-> You observe that for many records, the unit price portion of the ProductCode column contains values.
-> The RegionCode column contains NULL for some records.
-> Sales data is only recorded for sales representatives.
You are developing a series of reports and procedures to support the business. Details for each report or procedure follow.
Sales Summary report: This report aggregates data by year and quarter. The report must resemble the following table.

Sales Manager report: This report lists each sales manager and the total sales amount for all employees that report to the sales manager.
Sales by Region report: This report lists the total sales amount by employee and by region. The report must include the following columns: EmployeeCode,
MiddleName, LastName, RegionCode, and SalesAmount. If MiddleName is NULL, FirstName must be displayed. If both FirstName and MiddleName have null values, the world Unknown must be displayed/ If RegionCode is NULL, the word Unknown must be displayed.
Report1: This report joins data from SalesSummary with the Employee table and other tables. You plan to create an object to support Report1. The object has the following requirements:
-> be joinable with the SELECT statement that supplies data for the report
-> can be used multiple times with the SELECT statement for the report
-> be usable only with the SELECT statement for the report
not be saved as a permanent object

Report2: This report joins data from SalesSummary with the Employee table and other tables.
You plan to create an object to support Report1. The object has the following requirements:
-> be joinable with the SELECT statement that supplies data for the report
-> can be used multiple times for this report and other reports
-> accept parameters
-> be saved as a permanent object
Sales Hierarchy report: This report aggregates rows, creates subtotal rows, and super-aggregates rows over the SalesAmount column in a single result-set. The report uses SaleYear, SaleQuarter, and SaleMonth as a hierarchy. The result set must not contain a grand total or cross-tabulation aggregate rows.
Current Price Stored Procedure: This stored procedure must return the unit price for a product when a product code is supplied. The unit price must include a dollar sign at the beginning. In addition, the unit price must contain a comma every three digits to the left of the decimal point, and must display two digits to the left of the decimal point. The stored procedure must not throw errors, even if the product code contains invalid data.

End of Repeated Scenario -
You need to create the query for the Sales by Region report.
Which function should you apply to each column? To answer, select the appropriate options in the answer area.
Hot Area:



Answer :

Explanation:

Box 1: COALESCE -
COALESCE evaluates the arguments in order and returns the current value of the first expression that initially does not evaluate to NULL.
If MiddleName is NULL, FirstName must be displayed. If both FirstName and MiddleName have null values, the world Unknown must be displayed.
The following example shows how COALESCE selects the data from the first column that has a nonnull value.
SELECT Name, Class, Color, ProductNumber,
COALESCE(Class, Color, ProductNumber) AS FirstNotNull
FROM Production.Product;
Not NULLIF: NULLIF returns the first expression if the two expressions are not equal. If the expressions are equal, NULLIF returns a null value of the type of the first expression.

Box 2: COALESCE -
If RegionCodeis NULL, the word Unknown must be displayed.
References:
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql

Page:    1 / 14   
Total 205 questions