Chapter 8 material Flashcards

1
Q

define correlated subquery

A

executes once for each row in the outer query

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

define trigger

A

SQL code that is automatically invoked by the RDBMS upon occurrence of a data manipulation event

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

List the ticker symbol and the average closing for a stock that has the largest average closing price.

A

SELECT TOP 1 TickerSymbol, AVG (ST_Close) AS AverageClose

FROM StockData

GROUP BY TickerSymbol

ORDER BY AverageClose DESC;

or

SELECT TickerSymbol, AVG (ST_Close) AS AverageClose

FROM StockData

GROUP BY TickerSymbol

HAVING AVG(ST_Close) >= ANY (SELECT AVG (ST_Close)

FROM StockData

GROUP BY TickerSymbol)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

List stocks that were trades both on 7/1/2013 and on 7/1/2014

A

SELECT DISTINCT TickerSymbol

FROM StockData

WHERE TickerSymbol in

(SELECT tickersymbol

FROM StockData

WHERE TradeDate = ‘2013-7-1’) and TickerSymbol in

(SELECT tickersymbol

FROM StockData

WHERE TradeDate = ‘2014-7-1’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

List stocks that were trades both on 7/1/2013 and on 7/1/2014 using JOIN

A

SELECT DISTINCT T1.TickerSymbol

FROM

(SELECT tickersymbol

FROM StockData

WHERE TradeDate = ‘2013-7-1’) as T1 JOIN

(SELECT tickersymbol

FROM StockData

WHERE TradeDate = ‘2014-7-1’) as T2 ON T1.tickersymbol = T2.tickersymbol;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

List the ticker symbol and the percentage of each ticker symbol’s total volume to the total volume of all stocks.

A

SELECT tickersymbol, SUM(Volume)*100/ (SELECT SUM(Volume) from StockData)

FROM stockdata

GROUP BY TickerSymbol;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

-List tickersymbol, trade date, and all trade volumes that are greater than the average trade volume of the particular firm

A

SELECT tickersymbol, tradedate, volume, (SELECT AVG(volume)

FROM StockData S2

WHERE S1.Tickersymbol = S2.TickerSymbol)

FROM stockdata S1

WHERE volume > (SELECT AVG(volume)

FROM StockData S2

WHERE S1.Tickersymbol = S2.TickerSymbol);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

–List all information from the rows sharing the single least common closing price for the particular firm

A

SELECT *

FROM stockdata S1

WHERE st_Close = (SELECT TOP 1 s2.ST_Close

FROM StockData S2

WHERE S1.Tickersymbol = S2.TickerSymbol Group by st_close ORDER BY count(ST_close) DESC)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

–List companies whose close stock price is greater than double the open price at least once (derived

A

SELECT tickersymbol

FROM StockData

WHERE St_Close > St_Open;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

–List companies whose close stock price is greater than double the open price at least once using the EXISTS operator

A

SELECT tickersymbol

FROM StockData

WHERE EXISTS (SELECT tickersymbol, st_open

FROM StockData

WHERE St_Close > St_Open);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

–List the TickerSymbol, Company name and Industry for all stocks were traded on Tuesday (Derived table)

A

SELECT s.TickerSymbol, c.CompanyName, s.Industry

FROM StockData s JOIN CompanyInformation c ON s.TickerSymbol=c.TickerSymbol JOIN Calendar d ON s.TradeDate = d.ActualDate

WHERE DayOfWeek = ‘Tuesday’;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

–List the TickerSymbol, Company name and Industry for all stocks were traded on both a Tuesday and a Wednesday. (CTE common table expression)

A

SELECT StockTue.Tickersymbol, CompanyName, Industry

FROM

(SELECT s.TickerSymbol, DayOfWeek

FROM StockData s JOIN Calendar d ON s.TradeDate = d.ActualDate

WHERE DayOfWeek = ‘Tuesday’) AS StockTue

JOIN

(SELECT s.TickerSymbol, DayOfWeek

FROM StockData s JOIN Calendar d ON s.TradeDate = d.ActualDate

WHERE DayOfWeek = ‘Wednesday’) AS StockWed

ON StockTue.TickerSymbol=StockWed.TickerSymbol

JOIN

Companyinformation AS Cominfo

ON Stockwed.Tickersymbol = Cominfo.Tickersymbol;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

–List the TickerSymbol, Company name and Industry for all stocks were traded on both a Tuesday and a Wednesday. (VIEW)

A

CREATE VIEW StockTue AS (

SELECT s.TickerSymbol, DayOfWeek

FROM StockData s JOIN Calendar d ON s.TradeDate = d.ActualDate

WHERE DayOfWeek = ‘Tuesday’);

CREATE VIEW StockWed AS

(SELECT s.TickerSymbol, DayOfWeek

FROM StockData s JOIN Calendar d ON s.TradeDate = d.ActualDate

WHERE DayOfWeek = ‘Wednesday’);

SELECT StockTue.Tickersymbol, CompanyName, Industry

FROM StockTue, StockWed, Companyinformation AS c

WHERE StockTue.TickerSymbol=StockWed.TickerSymbol AND Stockwed.Tickersymbol = C.Tickersymbol;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

IF
(SELECT COUNT(PriceID)
FROM Stockdata
WHERE TickerSymbol = ‘aapl’) > 10000
PRINT ‘There are more than 10,000 Apple stocks traded.’
ELSE PRINT ‘There are 10,000 or less than 10,000 Apple stocks traded.’;

A

If the number of price id’s from the stockdata table with the apple ticker symbol are greater than 10000 then return the statement ‘there are more than 10,000 apple stocks traded’ if less than 10,000 then return ‘there are 10,000 or less than 10,000 apple stocks traded’

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

what does the pivot operator do?

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly