SQL Queries Flashcards
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
List the industry and the number of different companies in each industry.
Select Industry, count(distinct tickersymbol)
from stockdata
group by industry
List the industry and the number of different companies in each industry. Put the answer in order of my stocks to least stocks.
select industry, count(distinct tickersymbol) as CompaniesPerIndustry
from stockdata
Group by Industry
Order by CompaniesPerIndustry desc
List the industry and the number of different companies in each industry for only those industries that have more than three companies
select industry, count(distinct tickersymbol) as CountofIndustryCompaniesMorethan3
from stockdata
where (select count(distinct tickersymbol) from stockdata)>3
Group by industry
ORder by CountofIndustryCompaniesMorethan3
Create a stored procedure for all stocks in 2014 with a stock high of 100. Execute the stored procedure afterwards. Call the stored procedure Get2014StocksWithHighAbove100.
create proc Get2014stockswithhighat100 as select* from stockdata where st_high=100
Create a stored procedure to find the number of stocks that have a high stock price over 130 dollars. Execute the stored procedure afterwards. Call the stored procedure NumStocksHighAbove130.
create proc Numstockshighgreaterthan130 as select count(distinct tickersymbol) as Numstockshighabove130 from stockdata where st_high>130
exec Numstockshighgreaterthan130
Create a stored procedure to display the price ID, high stock price, and low stock price of all stocks that have a high stock price below 100 dollars and a low stock price greater than 90 dollars. Execute the stored procedure afterwards. Call the stored procedure GetHighBelow100LowOver90.
Create proc GetHighBelow100LowOver90 as Select PriceID, ST_high, ST_low From stockdata where st_high90
exec GetHighBelow100LowOver90
Create a stored procedure to display the trade date, closing stock price, and low stock price of all stocks that have a low stock price less or equal to 100 dollars and a closing price greater than or equal to 100 dollars. Execute the stored procedure afterwards. Call the stored procedure GetCloseUnder100LowOver100.
create proc GetCloseUnder100LowOver100 as Select tradedate, st_close, st_low from stockdata where st_low=100
exec GetCloseUnder100LowOver100
Create a stored procedure for all stocks in 2013 with a stock high of 100. Execute the stored procedure afterwards. Call the stored procedure Get2013StocksWithHighAbove100.
create proc Get2013StocksWithHighAbove100
as
Select*
from stockdata
where st_high=100 and year(tradedate)=2013
exec Get2013StocksWithHighAbove100
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 all columns and all rows from the StockData table.
Select*
From StockData
List the TickerSymbol, Industry, TradeDate and the Volume columns from the StockData table. List all rows.
Select tickersymbol, Industry, TradeDate, Volume
From StockData ;
List the TickerSymbol, Industry, TradeDate and the Volume columns from the StockData table. List only the rows that have a volume of more thirty million shares traded.
Select tickersymbol, Industry, TradeDate, Volume
From StockData
Where Volume > 30000000 ;
List the TickerSymbol, Industry, TradeDate and the Volume columns from the StockData table. List only the rows that have a volume of more thirty million shares traded. Arrange the answer in TickerSymbol order. This means for example that British Petroleum should appear before Sunoco.
Select tickersymbol, Industry, TradeDate, Volume
From StockData
Where Volume > 30000000
Order By TickerSymbol ;
List the TickerSymbol, Industry, TradeDate and the Volume columns from the StockData table. List only the rows that have a volume of more thirty million shares traded. Arrange the answer in TickerSymbol order and then in TradeDate order. This means that for a given stock, trading days should appear in chronological order.
Select tickersymbol, Industry, TradeDate, Volume
From StockData
Where Volume > 30000000
Order By TickerSymbol, TradeDate ;