Profitable Stocks

A stock is considered profitable if the predicted price is strictly greater than the current price. Write a query to find the stock_codes of all the stocks which are profitable based on this definition. Sort the output in ascending order.

There are two tables in the database: price_today and price_tomorrow. Their primary keys are stock_code.

Schema

There are 2 tables: price_today, price_tomorrow.

price_today
NameTypeDescription
stock_codeSTRINGThe stock code for this stock. This is the primary key.
priceINTEGERToday’s price of the stock.
price_tomorrow
NameTypeDescription
stock_codeSTRINGThe stock code for this stock. This is the primary key.
priceINTEGERPredicted Price tomorrow for the stock

Note: Both tables contain all stocks listed on the stock exchange.

Sample Data Tables
price_today
stock_codeprice
TITAN560
MRF950
GOOGL200
price_tomorrow
stock_codeprice
TITAN750
MRF800
GOOGL210

Sample Output

GOOGL
TITAN

Explanation

  • For TITAN, today’s stock price is 560 and its predicted price is 750, hence it is a profitable stock
  • For MRF, today’s stock price is 950 and its predicted price is 800, hence it is not a profitable stock
  • For GOOGL, today’s stock price is 200 and its predicted price is 210, hence it is a profitable stock

SOLUTION:

				
					SELECT pt.stock_code
FROM price_today AS pt
JOIN price_tomorrow AS ptom ON pt.stock_code = ptom.stock_code
WHERE ptom.price > pt.price
ORDER BY pt.stock_code ASC;