Question

In: Computer Science

1. Each of the queries in this section contain mistakes that prevent them from running. Explain...

1. Each of the queries in this section contain mistakes that prevent them from running. Explain the mistake and rewrite the query so it works. a. SELECT Customers.custid, Customers.companyname, Orders.orderid, Orders.orderdate FROM Sales.Customers AS C INNER JOIN Sales.Orders AS O ON Customers.custid = Orders.custid b. SELECT C.custid, C.companyname, COUNT(*) as numOrders FROM Sales.Customers as C INNER JOIN Sales.Orders as O WHERE C.custid = O.custid c. SELECT E.empid, COALESCE(COUNT(C.orderid),0) as numOrders FROM HR.Employees as E OUTER JOIN Sales.Customers as C ON E.custid = C.custid;

Solutions

Expert Solution

a.
SELECT Customers.custid, Customers.companyname, Orders.orderid, Orders.orderdate
FROM Sales.Customers AS C
INNER JOIN Sales.Orders AS O
ON Customers.custid = Orders.custid

--after aliasing with C, It is not allowed to use Customers.col. It must be C.col... Replace Cusotmers with C
Modified Query:

SELECT C.custid, C.companyname, O.orderid, O.orderdate
FROM Sales.Customers AS C
INNER JOIN Sales.Orders AS O
ON C.custid = O.custid

b.
SELECT C.custid, C.companyname, COUNT(*) as numOrders
FROM Sales.Customers as C
INNER JOIN Sales.Orders as O
WHERE C.custid = O.custid

-- your code would run, but gives you fruiteless results.
--count(*) is of aggregation function which can only used by group by
Modified Query:

SELECT C.custid, C.companyname, COUNT(*) as numOrders
FROM Sales.Customers as C
INNER JOIN Sales.Orders as O
on C.custid = O.custid
group by .custid, C.companyname


c.
SELECT E.empid, COALESCE(COUNT(C.orderid),0) as numOrders
FROM HR.Employees as E
OUTER JOIN Sales.Customers as C
ON E.custid = C.custid;

--coalesce returns the First not null value from the given set. null values are replaced by 0 here.
Modified Query:

SELECT E.empid, COALESCE(COUNT(C.orderid),0) as numOrders
FROM HR.Employees as E
OUTER JOIN Sales.Customers as C
ON E.custid = C.custid
group by E.empid

/*

CustomerID numberOrders
2 1
3 1
4 2
5 3
7 4
8 1
9 3
10 4
11 1
13 1
14

3

*/


Related Solutions

Section 1.You must use sub-queries to answer section 1questions.Q1:  List the length of the...
Section 1. You must use sub-queries to answer section 1 questions. Q1:  List the length of the longest track in the 'metal' genre. Q2: List the artistid, artistname and entrydate of all artists whose entrydate is earlier than everyone who has a 'directmail' leadsource. Q3: List the artistid, artistname and entrydate of all artists whose entrydate is earlier than anyone who has a 'directmail' leadsource. Q4:  List the artistname and entrydate of the artist with the earliest entry date. Q5:  ...
Part 1) Please indicate which section of the statement of cash flows should contain each of...
Part 1) Please indicate which section of the statement of cash flows should contain each of the following items, and whether each item would result in an inflow or outflow of cash. The sections are Operating, Investing, and Financing. (30 points) (a) Increase in accounts receivable (b) Purchase of a factory with cash (c) Depreciation of a building (d) Retirement of bonds with cash (e) Receipt of cash dividends Part 2) Explain how to calculate free cash flow and the...
1. List and describe the mechanisms that can reproductively isolate species and prevent them from interbreeding....
1. List and describe the mechanisms that can reproductively isolate species and prevent them from interbreeding. Explain speciation and how it might benefit or harm a population. 2. Describe the various types of fossils and the unique circumstances under which they can form. Next, list and describe conditions that will prevent fossilization. What kinds of organisms are most likely and least likely to be preserved as fossils? Explain how fossils can illustrate the lineage between an ancestor species and its...
what methods can be used to prevent hydrogen damage (in each of them ) - hydrogen...
what methods can be used to prevent hydrogen damage (in each of them ) - hydrogen embrittlement - voids and blisters - hydride formation
There are two urns that, between them, contain five balls. At each time step, one of...
There are two urns that, between them, contain five balls. At each time step, one of the five balls is moved to the other urn. Let the state variable be the number of balls in Urn 1. Find the fixed vector. a) Draw a state transition diagram and find the transition matrix. b) Is this a regular chain? Is this an ergodic chain? c) Find the fixed vector. What is the probability that in the long run Urn 1 has...
Describe 5 health teaching items to prevent them from acquiring cestode infections to be discussed to...
Describe 5 health teaching items to prevent them from acquiring cestode infections to be discussed to the following: a. A group of young professionals who loves sushi and everything raw and Japanese b. A group of fishermen who enjoys kinilaw; who believes that the vinegar and chili are enough to kill any form of infection. c. Association of Dog breeders d. Busy mothers who are informal settlers along Pasig River
6. When monopolies waste resources attempting to prevent other firms from competing with them, it is...
6. When monopolies waste resources attempting to prevent other firms from competing with them, it is referred to as: A. rent seeking. B. price fixing. C. cream skimming. D. price discriminating. 7. Price discrimination by a monopolist leads to the firm earning higher profits but it also delivers benefits to: A. competitors, because the good produced by the monopolist becomes more expensive. B. consumers, because consumer surplus increases because of the monopolist’s pricing actions. C. consumers, because prices are lowered...
1. a. Explain in details why the Operating System switches the process from Running state to...
1. a. Explain in details why the Operating System switches the process from Running state to Waiting state when it issues any I/O operation.    b. Describe a situation where the Operating System switches the process from the Running state to Ready state.
What is the purpose of section 1245 and 1250? Explain rather than describe them.
What is the purpose of section 1245 and 1250? Explain rather than describe them.
Explain each in a few sentences: a. What is the purpose / goal of running a...
Explain each in a few sentences: a. What is the purpose / goal of running a regression? (2 points) b. What are the regressand, regressor, dependent variable and independent variable in the regression context? (1 point
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT