Querying Data with Transact-SQL v1.0

Page:    1 / 14   
Exam contains 205 questions

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 on this series.
You have a database that tracks orders and deliveries for customers in North America. System versioning is enabled for all tables. The database contains the
Sales.Customers, Application.Cities, and Sales.CustomerCategories tables.
Details for the Sales.Customers table are shown in the following table:


Details for the Application.Cities table are shown in the following table:

Details for the Sales.CustomerCategories table are shown in the following table:

You need to create a query that meets the following requirements:
-> For customers that are not on a credit hold, return the CustomerID and the latest recorded population for the delivery city that is associated with the customer.
-> For customers that are on a credit hold, return the CustomerID and the latest recorded population for the postal city that is associated with the customer.
Which two Transact-SQL queries will achieve the goal? Each correct answer presents a complete solution.

A.

B.

C.

D.



Answer : AB

Explanation:

Using Cross Joins -
A cross join that does not have a WHERE clause produces the Cartesian product of the tables involved in the join. The size of a Cartesian product result set is the number of rows in the first table multiplied by the number of rows in the second table.
However, if a WHERE clause is added, the cross join behaves as an inner join.
B: You can use the IIF in the ON-statement.
IIF returns one of two values, depending on whether the Boolean expression evaluates to true or false in SQL Server.
References:
https://technet.microsoft.com/en-us/library/ms190690(v=sql.105).aspx https://msdn.microsoft.com/en-us/library/hh213574.aspx

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 on this series.
You have a database that tracks orders and deliveries for customers in North America. System versioning is enabled for all tables. The database contains the
Sales.Customers, Application.Cities, and Sales.CustomerCategories tables.
Details for the Sales.Customers table are shown in the following table:


Details for the Application.Cities table are shown in the following table:

Details for the Sales.CustomerCategories table are shown in the following table:

You discover an application bug that impacts customer data for records created on or after January 1, 2014. In order to fix the data impacted by the bug, application programmers require a report that contains customer data as it existed on December 31, 2013.
You need to provide the query for the report.
Which Transact-SQL statement should you use?

A.

B.

C.

D.

E.



Answer : C

Explanation:
To perform any type of time-based analysis, use the new FOR SYSTEM_TIME clause with four temporal-specific sub-clauses to query data across the current and history tables.
Use the AS OF sub-clause when you need to reconstruct state of data as it was at any specific time in the past.
References:
https://docs.microsoft.com/en-us/sql/relational-databases/tables/querying-data-in-a-system-versioned-temporal-table?view=sql-server-2017

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 on this series.
You have a database that tracks orders and deliveries for customers in North America. System versioning is enabled for all tables. The database contains the
Sales.Customers, Application.Cities, and Sales.CustomerCategories tables.
Details for the Sales.Customers table are shown in the following table:


Details for the Application.Cities table are shown in the following table:

Details for the Sales.CustomerCategories table are shown in the following table:

You are creating a report to measure the impact of advertising efforts that were designed to attract new customers. The report must show the number of new customers per day for each customer category, but only if the number of new customers is greater than five.
You need to write the query to return data for the report.
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.
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 on this series.
You have a database that tracks orders and deliveries for customers in North America. System versioning is enabled for all tables. The database contains the
Sales.Customers, Application.Cities, and Sales.CustomerCategories tables.
Details for the Sales.Customers table are shown in the following table:


Details for the Application.Cities table are shown in the following table:

Details for the Sales.CustomerCategories table are shown in the following table:

The marketing department is performing an analysis of how discount affect credit limits. They need to know the average credit limit per standard discount percentage for customers whose standard discount percentage is between zero and four.
You need to create a query that returns the data for the analysis.
How should you complete the Transact-SQL statement? To answer, drag the appropriate Transact-SQL segments to the correct locations. Each Transact-SQL segments 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.
Select and Place:



Answer :

Explanation:

Box 1: 0, 1, 2, 3, 4 -
Pivot example:
-- Pivot table with one rowand five columns
SELECT 'AverageCost' AS Cost_Sorted_By_Production_Days,
[0], [1], [2], [3], [4]

FROM -
(SELECT DaysToManufacture, StandardCost
FROM Production.Product) AS SourceTable

PIVOT -
(
AVG(StandardCost)
FOR DaysToManufacture IN ([0], [1], [2], [3], [4])
) AS PivotTable;
Box 2: [CreditLimit]

Box 3: PIVOT -
You can use the PIVOT and UNPIVOT relational operators to change a table-valued expression into another table. PIVOT rotates a table-valued expression by turning the unique values from one column in the expression into multiple columns in the output, and performs aggregations where they are required on any remaining column values that are wanted in the final output.

Box 4: 0, 1, 2, 3, 4 -
The IN clause determines whether a specified value matches any value in a subquery or a list.
Syntax: test_expression [ NOT ] IN ( subquery | expression [ ,...n ] )
Where expression[ ,... n ] is a list of expressions to test for a match. All expressions must be of the same type as test_expression.
References:
https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

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 on this series.
You have a database that tracks orders and deliveries for customers in North America. System versioning is enabled for all tables. The database contains the
Sales.Customers, Application.Cities, and Sales.CustomerCategories tables.
Details for the Sales.Customers table are shown in the following table:


Details for the Application.Cities table are shown in the following table:

Details for the Sales.CustomerCategories table are shown in the following table:

You are preparing a promotional mailing. The mailing must only be sent to customers in good standing that live in medium and large cities.
You need to write a query that returns all customers that are not on credit hold who live in cities with a population greater than 10,000.
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.
Select and Place:



Answer :

Explanation:
Box 1: IN (
The IN clause determines whether a specified value matches any value in a subquery or a list.
Syntax: test_expression [ NOT ] IN ( subquery | expression [ ,...n ] )

Where subquery -
Is a subquery that has a result set of one column. This column must have the same data type as test_expression.

Box 2: WHERE -

Box 3: AND [IsOnCreditHold] = 0 -
Box 4: )
References:
https://msdn.microsoft.com/en-us/library/ms177682.aspx

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.
You have a table named Products that contains information about the products that your company sells. The table contains many columns that do not always contain values.
You need to implement an ANSI standard method to convert the NULL values in the query output to the phrase "Not Applicable".
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 : A

Explanation:
COALESCE evaluates the arguments in order and returns the current value of the first expression that initially doesn't evaluate to NULL.
Incorrect Answers:
F: ISNULL is not a ANSI standard function. The COALESCE function is preferred.
References:
https://docs.microsoft.com/en-us/sql/t-sql/language-elements/coalesce-transact-sql?view=sql-server-2017

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.
You have a database that is denormalized. Users make frequent changes to data in a primary table.
You need to ensure that users cannot change the tables directly, and that changes made to the primary table also update any related tables.
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 : B

Explanation:
Using an Indexed View would allow you to keep your base data in properly normalized tables and maintain data-integrity while giving you the denormalized "view" of that data.
References:
http://stackoverflow.com/questions/4789091/updating-redundant-denormalized-data-automatically-in-sql-server

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.
You have a database that stores sales and order information.
Users must be able to extract information from the tables on an ad hoc basis. They must also be able to reference the extracted information as a single table.
You need to implement a solution that allows users to retrieve the data required, based on variables defined at the time of the query.
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 : C

Explanation:
User-defined functions that return a table data type can be powerful alternatives to views. These functions are referred to as table-valued functions. A table-valued user-defined function can be used where table or view expressions are allowed in Transact-SQL queries. While views are limited to a single SELECT statement, user-defined functions can contain additional statements that allow more powerful logic than is possible in views.
A table-valued user-defined function can also replace stored procedures that return a single result set.
References:
https://technet.microsoft.com/en-us/library/ms191165(v=sql.105).aspx

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.
You have a table named AuditTrail that tracks modifications to data in other tables. The AuditTrail table is updated by many processes. Data input into AuditTrail may contain improperly formatted date time values. You implement a process that retrieves data from the various columns in AuditTrail, but sometimes the process throws an error when it is unable to convert the data into valid date time values.
You need to convert the data into a valid date time value using the en-US format culture code. If the conversion fails, a null value must be returned in the column output. The conversion process must not throw 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:
A TRY_CONVERT function returns a value cast to the specified data type if the cast succeeds; otherwise, returns null.
References:
https://msdn.microsoft.com/en-us/library/hh230993.aspx

HOTSPOT -
You have the following subqueries: Subquery1, Subquery2, and Subquery3.
You need to replace the three subqueries with named result sets or temporary tables. The following requirements must be met:


Which replacement techniques should you use? To answer, select the appropriate options in the answer area.
Hot Area:



Answer :

Explanation:
Subquery1: common table expression (CTE)
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. Unlike a derived table, a CTE can be self-referencing and can be referenced multiple times in the same query.
Subquery2: global temporary table
Global temporary tables are visible to any user and any connection after they are created, and are deleted when all users that are referencing the table disconnect from the instance of SQL Server.
Subquery3: local temporary table
Local temporary tables are visible only to their creators during the same connection to an instance of SQL Server as when the tables were first created or referenced. Local temporary tables are deleted after the user disconnects from the instance of SQL Server.
References:
https://technet.microsoft.com/en-us/library/ms190766(v=sql.105).aspx https://technet.microsoft.com/en-us/library/ms186986.aspx

You have a database that stored information about servers and application errors. The database contains the following tables.

Servers -



Errors -

You need to return all error log messages and the server where the error occurs most often.
Which Transact-SQL statement should you run?

A.

B.

C.

D.



Answer : A

Explanation -
Incorrect Answers:
C: This would get all the ServerIDs, and not all the error logs

DRAG DROP -
You have a database that stored information about servers and application errors. The database contains the following tables.

Servers -



Errors -

You are building a webpage that shows the three most common errors for each server.
You need to return the data for the webpage.
How should you complete the Transact-SQL statement? To answer, drag the appropriate Transact-SQL segments to the correct location. 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 :

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.



Answer : A

Explanation:
Incorrect Answers:
C: count(*) always give 1 as it will have some data in the overall table

SIMULATION -
You have a table named Cities that has the following two columns: CityID and CityName. The CityID column uses the int data type, and CityName uses nvarchar
(max).
You have a table named RawSurvey. Each row includes an identifier for a question and the number of persons that responded to that question from each of four cities. The table contains the following representative data:


A reporting table named SurveyReport has the following columns: CityID, QuestionID, and RawCount, where RawCount is the value from the RawSurvey table.
You need to write a Transact-SQL query to meet the following requirements:
-> Retrieve data from the RawSurvey table in the format of the SurveyReport table.
-> The CityID must contain the CityID of the city that was surveyed.
-> The order of cities in all SELECT queries must match the order in the RawSurvey table.
-> The order of cities in all IN statements must match the order in the RawSurvey table.
Construct the query using the following guidelines:
-> Use one-part names to reference tables and columns, except where not possible.
-> ALL SELECT statements must specify columns.
-> Do not use column or table aliases, except those provided.
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.

Use the Check Syntax button to verify your work. Any syntax or spelling errors will be reported by line and character position.



Answer : Please see explanation

Explanation:
1 Select CityID, QuestionID, RawCount
2 FROM (SELECT QuestionId, Tokyo, Boston, London, NewYork FROM RawSurvey) AS t1
3 UNPIVOT (RawCount FOR CityName IN (Tokyo, Boston, London, NewYork)) AS t2
4 JOIN Cities ON t2.CityName = t1.CityName
UNPIVOT must be used to rotate columns of the Rawsurvey table into column values.
References:
https://technet.microsoft.com/en-us/library/ms177410(v=sql.105).aspx

DRAG DROP -
You create three tables by running the following Transact-SQL statements:


For reporting purposes, you need to find the active user count for each role, and the total active user count. The result must be ordered by active user count of each role. You must use common table expressions (CTEs).
Which four 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:
Order by cannot be used in sub quires without offset, and with active users segment should be on the top only in the first of CTE's.

Page:    1 / 14   
Exam contains 205 questions

Talk to us!


Have any questions or issues ? Please dont hesitate to contact us

Certlibrary.com is owned by MBS Tech Limited: Room 1905 Nam Wo Hong Building, 148 Wing Lok Street, Sheung Wan, Hong Kong. Company registration number: 2310926
Certlibrary doesn't offer Real Microsoft Exam Questions. Certlibrary Materials do not contain actual questions and answers from Cisco's Certification Exams.
CFA Institute does not endorse, promote or warrant the accuracy or quality of Certlibrary. CFA® and Chartered Financial Analyst® are registered trademarks owned by CFA Institute.
Terms & Conditions | Privacy Policy