SQL Queries Flashcards
(107 cards)
Create a stored procedure for all stocks in 2014 with a stock high of 100. Execute the stored procedure afterwards. Call the stored procedure 2014StocksGreater100.
CREATE PROC SP_2014StocksGreater100
AS
SELECT *
FROM StockData
WHERE ST_High > 100 AND YEAR(TradeDate) = 2014;
EXEC SP_2014StocksGreater100
Create a stored procedure that lists all attributes for TickerSymbol that is returned by the user.
CREATE PROCEDURE ChooseStock1 @Tick CHAR(10)
AS
SELECT *
FROM StockData
WHERE TickerSymbol = @Tick;
EXEC ChooseStock1 ‘aapl’
Create a stored procedure that lists the industry and the number of different companies in each industry. The industry should be returned by the user.
CREATE PROC indcompany @industry nchar(6)
AS
SELECT Industry, COUNT(DISTINCT TickerSymbol)
FROM stockdata
WHERE Industry = @industry
GROUP BY Industry;
EXEC indcompany ‘tech’
Create a stored procedure that allows to enter a new record into StockData
CREATE PROC EnterStock @PID CHAR(10), @SO FLOAT, @SC FLOAT
AS
INSERT INTO StockData (PriceID, ST_Open, ST_Close)
VALUES (@PID, @SO, @SC);
EXEC EnterStock ‘1321321’, 1.21, 1.01
List todays’ date, yesterday’s date , the current year, the number of days and months that have elapsed since 9/11.
SELECT GETDATE(), DATEADD(dd, -1, GETDATE()), DATEADD(dd, 1, GETDATE()), YEAR(GETDATE()), DATEDIFF(dd,’9/11/2001’, GETDATE()), YEAR(GETDATE()), DATEDIFF(mm,’9/11/2001’, GETDATE())
List the mean, variance, and standard deviation of volumes.
SELECT AVG(Volume), VAR(Volume), STDEV(Volume)
FROM stockdata;
List area codes of PhoneNumber
SELECT SUBSTRING(PhoneNumber,1,3)
FROM CompanyInformation;
SELECT LEFT(PhoneNumber,3)
FROM CompanyInformation;
SELECT RIGHT(PhoneNumber,3)
FROM CompanyInformation;
List the middle digits of PhoneNumber
SELECT SUBSTRING(PhoneNumber,5,3)
FROM CompanyInformation;
SELECT LEN(Phonenumber)
List industry in upper case.
SELECT UPPER(Industry)
FROM stockdata;
SELECT LOWER(Industry)
FROM stockdata;
Remove leading blanks in Address
SELECT LTRIM(Address)
FROM CompanyInformation;
-List the number of stocks that have a greater than average high price and were traded on September 9th, 2011. Call expensive stocks.
SELECT COUNT(ST_High)AS ExpensiveStocks
FROM StockData
WHERE ST_High > (SELECT AVG (ST_High) FROM StockData) AND TradeDate = ‘09/09/2011’;
List all information from the rows sharing the single most common closing price.
SELECT * from StockData where ST_Close = (SELECT Top 1 ST_close FROM StockData GROUP BY ST_Close ORDER BY COUNT(ST_Close) DESC);
-List calendar dates on which stock trading was recorded.
SELECT ActualDate
FROM Calendar
WHERE ActualDate NOT IN (SELECT TradeDate FROM Stockdata);
SELECT *
FROM Calendar;
List the ticker symbol and the average closing for a stock that has the largest average closing price.
SELECT TOP 1 TickerSymbol, AVG (ST_Close) AS AverageClose
FROM StockData
GROUP BY TickerSymbol
ORDER BY AverageClose DESC;
Write a query that lists the ticker symbol and the number of trade dates for those stocks that have more than 2000 trade dates.
Select tickersymbol, count(distinct tradedate)
from stockdata
where (select count(distinct tradedate)from stockdata)>2000
group by tickersymbol
Write a query that lists the TickerSymbol and average volume for those stocks that have an average greater than three million shares.
select tickersymbol, avg (volume)
from stockdata
where (select avg(volume) from stockdata)>3000000
Group by tickersymbol
Write a query that lists the TickerSymbol and average volume for those stocks that have a total shares traded greater than ten million shares.
select tickersymbol, avg(volume) as averagevolume , sum(volume) as sumofvolume
from stockdata
where (select sum(volume) from stockdata)>10000000
group by tickersymbol
List the TickerSymbol, Industry, the row number, and the stock close price for the tech industry. Arrange the answer to be in order of the highest closing price to the lowest closing price.
Select tickersymbol, industry ,ROW_NUMBER() OVER(ORDER BY tickersymbol) AS ‘row number’, ST_close
from stockdata
where industry=’TECH’
Order by ST_close desc
Create a stored procedure that lists all attributes for TickerSymbol that is returned by the user.
create proc tickersymbolattributes @tick nchar(10) as SELECT * FROM StockData WHERE TickerSymbol = @Tick; exec tickersymbolattributes 'hon'
List the trade dates in descending order and open price in ascending order of all the stocks that open with a price greater than the average open price.
select tradedate, st_open
from stockdata
where st_open > (select avg(st_open)from stockdata)
order by tradedate desc, st_open asc
List the number of stocks that have an opening price of 70 dollars and were traded in 2013. Call it NumberOfOpeningPricesOf70In2013
Select count(tickersymbol) as NumberofOpeningPricesof70in2013
from stockdata
where ST_open=70 and year(tradedate)=2013
Order by count(tickersymbol)
List the number of stocks that have a greater than average high price and were traded on September 9th, 2013. Call it NumberOfStocksAboveAverageHighIn2013.
select count(volume) as StocksGreaterthanAverageHighPriceSept92013 from stockdata where st_high>(select avg(st_high) from stockdata) and tradedate='09/09/2013'
List the highest high price of all the stocks that have been traded in 2013 and have a low price less than 100 dollars.
Select max(ST_high) as HighPriceStocksTradedin2014
from stockdata
where ST_low
List the number of trade dates of stocks that were traded in the month of August in 2014.
Select count(distinct tradedate) from stockdata where month(tradedate)=08 and year(tradedate)= 2014