You are on page 1of 3

Chapter 9

/* Display a list of all cities which have more than 2 companies registered in
them. */

SELECT
city
FROM
places
WHERE
place_id IN ( -- -- we don't know how many palce_ids there are. Hence I
need to use IN
SELECT
place_id
FROM
companies
HAVING
COUNT(place_id) > 2
GROUP BY
place_id
)
ORDER BY
city
;

/* Display the share_id with the most recent transaction_time. */

SELECT
share_id
FROM
trades
WHERE
TRANSACTION_TIME =
(SELECT
MAX(TRANSACTION_TIME)
FROM
trades)
;

/* The time_start with the highest share price for any share. */

SELECT
time_start
FROM
shares_prices
WHERE
price =
(SELECT MAX(price)
FROM
shares_prices)
;

/* Display the most popular first name from the brokers. */

SELECT
first_name
FROM
brokers
GROUP BY
first_name
HAVING
COUNT(first_name) =
(
SELECT MAX(COUNT(first_name))
FROM brokers
GROUP BY first_name
);

/* Display the currency_id which is used to price the most shares. */

SELECT
currency_id
FROM
shares
GROUP BY
currency_id
HAVING COUNT (share_id) =
(SELECT
MAX (COUNT(share_id))
FROM
shares
GROUP BY
currency_id
)
;

/* Display the highest number of times a single share has been traded. */

SELECT MAX(times_traded)
FROM (
SELECT share_id,
COUNT(share_id) AS times_traded
FROM trades
GROUP BY share_id
)
;
/* Display the amount of times each share has been traded. */ -- Simple Query of
Ist Question

SELECT share_id,
COUNT(share_id) AS times_traded
FROM trades
GROUP BY share_id
;

/* Display the total value of shares traded for this quarter for every broker.
Display these in a single query. */

SELECT
broker_id, -- as we need to find out the broker_id corresponding to each
broker
SUM(price_total) AS total --gives total value of shares
FROM
trades -- upto here gives price total for all quarters
WHERE
TO_CHAR(transaction_time,'Q') = TO_CHAR(SYSDATE,'Q') --For this Quarter
-- Converting todays_system
date to current quarter. So it will be 2.
GROUP BY
broker_id
; -- But this does not display name of Brokers.

You might also like