You are on page 1of 28

Exercise: 1 (Serge I: 2002-09-30)

Find the model number, speed and hard drive capacity for all the PCs with prices below $500.
Result set: model, speed, hd.

SELECT model, speed, hd

FROM pc

WHERE pc.price < $500;

Exercise: 2 (Serge I: 2002-09-21)


List all printer makers. Result set: maker.

SELECT DISTINCT maker

FROM product

WHERE product.type = 'printer';

Exercise: 3 (Serge I: 2002-09-30)


Find the model number, RAM and screen size of the laptops with prices over $1000.

SELECT model, ram, screen

FROM laptop

WHERE laptop.price > 1000;

Exercise: 4 (Serge I: 2002-09-21)


Find all records from the Printer table containing data about color printers.

SELECT *

FROM printer

WHERE printer.color = 'y';

Exercise: 5 (Serge I: 2002-09-30)


Find the model number, speed and hard drive capacity of PCs cheaper than $600 having a 12x or a 24x
CD drive.

SELECT model, speed, hd

FROM pc

WHERE pc.price < 600 AND (pc.cd = '12x' OR pc.cd = '24x');

Exercise: 6 (Serge I: 2002-10-28)


For each maker producing laptops with a hard drive capacity of 10 Gb or higher, find the speed of such
laptops. Result set: maker, speed.

SELECT DISTINCT product.maker, laptop.speed

FROM laptop

JOIN product ON product.model = laptop.model

WHERE laptop.hd >= 10;

Exercise: 7 (Serge I: 2002-11-02)


Get the models and prices for all commercially available products (of any type) produced by maker B.

SELECT product.model, pc.price

FROM product

JOIN pc ON pc.model = product.model

WHERE product.maker = 'B'

UNION

SELECT product.model, laptop.price

FROM product

JOIN laptop ON laptop.model = product.model

WHERE product.maker = 'B'

UNION

SELECT product.model, printer.price

FROM product

JOIN printer ON printer.model = product.model

WHERE product.maker = 'B';

Exercise: 8 (Serge I: 2003-02-03)


Find the makers producing PCs but not laptops.

SELECT DISTINCT maker

FROM Product

WHERE type = 'PC' AND

maker NOT IN (SELECT maker

FROM Product

WHERE type = 'Laptop');


Exercise: 9 (Serge I: 2002-11-02)
Find the makers of PCs with a processor speed of 450 MHz or more. Result set: maker.

SELECT DISTINCT maker

FROM product

JOIN pc ON product.model = pc.model

WHERE pc.speed >= 450;

Exercise: 10 (Serge I: 2002-09-23)


Find the printer models having the highest price. Result set: model, price.

SELECT model, price

FROM printer

WHERE price = (SELECT MAX(price)

FROM printer);

Exercise: 11 (Serge I: 2002-11-02)


Find out the average speed of PCs.

SELECT AVG(speed) FROM pc;

Exercise: 12 (Serge I: 2002-11-02)


Find out the average speed of the laptops priced over $1000.

SELECT AVG(speed)

FROM laptop

WHERE price>1000;

Exercise: 13 (Serge I: 2002-11-02)


Find out the average speed of the PCs produced by maker A.

SELECT AVG(speed)

FROM pc

JOIN product ON product.model = pc.model

WHERE maker = 'A';

Exercise: 14 (Serge I: 2002-11-05)


For the ships in the Ships table that have at least 10 guns, get the class, name, and country.
SELECT ships.class, ships.name, classes.country

FROM ships

JOIN classes ON classes.class = ships.class

WHERE numGuns >= 10;

Exercise: 15 (Serge I: 2003-02-03)


Get hard drive capacities that are identical for two or more PCs.
Result set: hd.

SELECT hd

FROM pc

GROUP BY hd

HAVING COUNT(hd)>=2;

Exercise: 16 (Serge I: 2003-02-03)


Get pairs of PC models with identical speeds and the same RAM capacity. Each resulting pair should be
displayed only once, i.e. (i, j) but not (j, i).
Result set: model with the bigger number, model with the smaller number, speed, and RAM.

SELECT p1.model, p2.model, p1.speed, p1.ram

FROM PC p1, PC p2

WHERE p1.speed = p2.speed AND p1.ram = p2.ram

GROUP BY p1.model, p2.model, p1.speed, p1.ram

HAVING p1.model > p2.model;

Exercise: 17 (Serge I: 2003-02-03)


Get the laptop models that have a speed smaller than the speed of any PC.
Result set: type, model, speed.

SELECT DISTINCT product.type, laptop.model, laptop.speed

FROM product, laptop

WHERE laptop.speed < (SELECT MIN(speed) FROM pc)

AND product.type = 'laptop';

Exercise: 18 (Serge I: 2003-02-03)


Find the makers of the cheapest color printers.
Result set: maker, price.
SELECT DISTINCT pro.maker, pri.price

FROM product pro

INNER JOIN printer pri on pro.model = pri.model

WHERE color='y' AND pri.price = (SELECT MIN(price) FROM printer WHERE color='y');

Exercise: 19 (Serge I: 2003-02-13)


For each maker having models in the Laptop table, find out the average screen size of the laptops he
produces.
Result set: maker, average screen size.

SELECT maker, AVG(screen) as Avg_screen

FROM product

JOIN laptop ON product.model = laptop.model

GROUP BY product.maker;

Exercise: 20 (Serge I: 2003-02-13)


Find the makers producing at least three distinct models of PCs.
Result set: maker, number of PC models.

SELECT DISTINCT maker, COUNT(model) AS Count_Model

FROM product

WHERE type = 'pc'

GROUP BY maker

HAVING COUNT(DISTINCT model) >= 3;

Exercise: 21 (Serge I: 2003-02-13)


Find out the maximum PC price for each maker having models in the PC table. Result set: maker,
maximum price.

SELECT maker, MAX(price)

FROM product

JOIN pc ON pc.model = product.model

GROUP BY maker;

Exercise: 22 (Serge I: 2003-02-13)


For each value of PC speed that exceeds 600 MHz, find out the average price of PCs with identical
speeds.
Result set: speed, average price.

SELECT speed, AVG(price)

FROM pc

WHERE speed > 600

GROUP BY speed;

Exercise: 23 (Serge I: 2003-02-14)


Get the makers producing both PCs having a speed of 750 MHz or higher and laptops with a speed of
750 MHz or higher.
Result set: maker

SELECT p1.maker FROM product p1

INNER JOIN pc p2 ON p1.model = p2.model

WHERE p2.speed >= 750

INTERSECT

SELECT p.maker FROM product p

INNER JOIN laptop l ON p.model = l.model

WHERE l.speed >= 750;

Exercise: 24 (Serge I: 2003-02-03)


List the models of any type having the highest price of all products present in the database.

SELECT DISTINCT product.model

FROM product, pc, laptop, printer

WHERE pc.price = (SELECT MAX(price) FROM pc)

AND laptop.price = (SELECT MAX(price) FROM laptop)

AND printer.price = (SELECT MAX(price) FROM printer)

AND (

(pc.price >= laptop.price AND pc.price >= printer.price

AND product.model = pc.model)

OR

(laptop.price >= pc.price AND laptop.price >= printer.price

AND product.model = laptop.model)

OR
(printer.price >= laptop.price AND printer.price >= pc.price

AND product.model = printer.model));

Exercise: 25 (Serge I: 2003-02-14)


Find the printer makers also producing PCs with the lowest RAM capacity and the highest processor
speed of all PCs having the lowest RAM capacity.
Result set: maker.

WITH MAX_SPEED AS(

SELECT MAX(speed) AS 'speed'

FROM pc

WHERE ram IN (SELECT MIN(ram) FROM pc)

SELECT DISTINCT p.maker

FROM pc

INNER JOIN product p ON pc.model = p.model

WHERE speed IN (SELECT speed FROM MAX_SPEED)

AND ram IN (SELECT MIN(ram) FROM pc)

INTERSECT

SELECT maker

FROM product

WHERE type = 'Printer';

Exercise: 26 (Serge I: 2003-02-14)


Find out the average price of PCs and laptops produced by maker A.
Result set: one overall average price for all items.

SELECT AVG(price) FROM (

SELECT price FROM pc WHERE model IN

(SELECT model FROM product WHERE maker='A' AND type='PC')

UNION ALL

SELECT price FROM laptop WHERE model IN

(SELECT model FROM product WHERE maker='A' AND type='Laptop')

) AS avgprice;
Exercise: 27 (Serge I: 2003-02-03)
Find out the average hard disk drive capacity of PCs produced by makers who also manufacture printers.
Result set: maker, average HDD capacity.

SELECT product.maker, AVG(pc.hd)

FROM pc

INNER JOIN product ON pc.model = product.model

WHERE product.maker IN (SELECT maker FROM product WHERE type = 'Printer')

GROUP BY maker;

Exercise: 28 (Serge I: 2012-05-04)


Using Product table, find out the number of makers who produce only one model.

SELECT COUNT(maker)

FROM (SELECT maker

FROM product

GROUP BY maker

HAVING count(model)=1) AS a;

Exercise: 29 (Serge I: 2003-02-14)


Under the assumption that receipts of money (inc) and payouts (out) are registered not more than once a
day for each collection point [i.e. the primary key consists of (point, date)], write a query displaying cash
flow data (point, date, income, expense).
Use Income_o and Outcome_o tables.

SELECT i.point, i.date, i.inc, o.out

FROM Income_o i

LEFT JOIN Outcome_o o ON i.point = o.point AND i.date = o.date

UNION

SELECT o.point, o.date, i.inc, o.out

FROM outcome_o o

LEFT JOIN income_o i ON o.point = i.point AND o.date = i.date;

Exercise: 30 (Serge I: 2003-02-14)


Under the assumption that receipts of money (inc) and payouts (out) can be registered any number of
times a day for each collection point [i.e. the code column is the primary key], display a table with one
corresponding row for each operating date of each collection point.
Result set: point, date, total payout per day (out), total money intake per day (inc).
Missing values are considered to be NULL.

SELECT DISTINCT point,date,SUM(out) AS out, SUM(inc) AS inc FROM (

SELECT Income.point, Income.date, out, inc

FROM Income LEFT JOIN

Outcome ON Income.point = Outcome.point AND

Income.date = Outcome.date AND Income.code= Outcome.code

UNION ALL

SELECT Outcome.point, Outcome.date, out, inc

FROM Outcome LEFT JOIN

Income ON Income.point = Outcome.point AND

Income.date = Outcome.date AND Income.code=Outcome.code) AS t1

GROUP BY point, date;

Exercise: 31 (Serge I: 2002-10-22)


For ship classes with a gun caliber of 16 in. or more, display the class and the country.

SELECT class, country

FROM classes

WHERE bore >= 16;

Exercise: 32 (Serge I: 2003-02-17)


One of the characteristics of a ship is one-half the cube of the calibre of its main guns (mw).
Determine the average ship mw with an accuracy of two decimal places for each country having ships in
the database.

SELECT country, CONVERT(NUMERIC(10, 2), AVG(POWER(bore, 3)/2)) AS weight

FROM (SELECT country, bore, name from classes c, ships s

WHERE s.class=c.class

UNION

SELECT country, bore, ship FROM classes c, outcomes o

WHERE o.ship=c.class

AND o.ship NOT IN(SELECT DISTINCT name FROM ships))x

GROUP BY country;

Exercise: 33 (Serge I: 2002-11-02)


Get the ships sunk in the North Atlantic battle.
Result set: ship.

SELECT ship

FROM Outcomes

WHERE battle = 'North Atlantic' AND result = 'sunk';

Exercise: 34 (Serge I: 2002-11-04)


In accordance with the Washington Naval Treaty concluded in the beginning of 1922, it was prohibited to
build battle ships with a displacement of more than 35 thousand tons.
Get the ships violating this treaty (only consider ships for which the year of launch is known).
List the names of the ships.

SELECT name

FROM Ships

JOIN Classes ON Classes.class = Ships.class

WHERE Classes.displacement > 35000 AND Ships.launched >= 1922 AND Classes.type = 'bb';

Exercise: 35 (qwrqwr: 2012-11-23)


Find models in the Product table consisting either of digits only or Latin letters (A-Z, case insensitive)
only.
Result set: model, type.

SELECT model, type

FROM product

WHERE model

NOT LIKE '%[^A-Z]%' OR model NOT LIKE '%[^0-9]%';

Exercise: 36 (Serge I: 2003-02-17)


List the names of lead ships in the database (including the Outcomes table).

SELECT name

FROM ships

WHERE name IN (SELECT class FROM classes)

UNION

SELECT ship

FROM outcomes

WHERE ship IN (SELECT class FROM classes);


Exercise: 37 (Serge I: 2003-02-17)
Find classes for which only one ship exists in the database (including the Outcomes table).

SELECT c.class

FROM

(SELECT s.class, s.name

FROM ships s

UNION

SELECT o.ship as 'class', o.ship

FROM Outcomes o

WHERE NOT EXISTS( SELECT * FROM Ships s WHERE s.name = o.ship)) s

INNER JOIN Classes c ON c.class = s.class

GROUP by c.class

HAVING count(*) = 1;

Exercise: 38 (Serge I: 2003-02-19)


Find countries that ever had classes of both battleships (‘bb’) and cruisers (‘bc’).

SELECT country

FROM Classes

WHERE type = 'bb'

INTERSECT

SELECT country

FROM Classes

WHERE type ='bc';

Exercise: 39 (Serge I: 2003-02-14)


Find the ships that `survived for future battles`; that is, after being damaged in a battle, they participated
in another one, which occurred later.

SELECT DISTINCT o.ship

FROM outcomes o

LEFT JOIN Battles b ON b.name=o.battle

WHERE o.result = 'damaged'


and EXISTS(SELECT *

FROM outcomes o2

LEFT JOIN battles b2 ON b2.name=o2.battle

WHERE o2.ship=o.ship

and b2.date > b.date);

Exercise: 40 (Serge I: 2012-04-20)


Get the makers who produce only one product type and more than one model. Output: maker, type.

SELECT DISTINCT maker, type

FROM product

WHERE maker IN

(SELECT DISTINCT maker

FROM product

GROUP BY maker

HAVING COUNT(DISTINCT type) = 1

AND COUNT(model) > 1);

Exercise: 41 (Serge I: 2019-05-31)


For each maker who has models at least in one of the tables PC, Laptop, or Printer, determine the
maximum price for his products.
Output: maker; if there are NULL values among the prices for the products of a given maker, display
NULL for this maker, otherwise, the maximum price.

-------------------------------------------------------------------------

Exercise: 42 (Serge I: 2002-11-05)


Find the names of ships sunk at battles, along with the names of the corresponding battles.

SELECT ship, battle

FROM outcomes

WHERE result = 'sunk';

Exercise: 43 (qwrqwr: 2011-10-28)


Get the battles that occurred in years when no ships were launched into water.

SELECT name

FROM battles

WHERE YEAR(date) NOT IN


(SELECT launched FROM ships WHERE launched IS NOT NULL);

Exercise: 44 (Serge I: 2002-12-04)


Find all ship names beginning with the letter R.

SELECT name

FROM ships

WHERE name LIKE 'R%'

UNION

SELECT ship

FROM outcomes

WHERE ship LIKE 'R%';

Exercise: 45 (Serge I: 2002-12-04)


Find all ship names consisting of three or more words (e.g., King George V).
Consider the words in ship names to be separated by single spaces, and the ship names to have no
leading or trailing spaces.

SELECT name

FROM ships

WHERE name LIKE '%% %% %%'

UNION

SELECT ship

FROM outcomes

WHERE ship LIKE '%% %% %%';

Exercise: 46 (Serge I: 2003-02-14)


For each ship that participated in the Battle of Guadalcanal, get its name, displacement, and the number
of guns.

SELECT name, displacement, numGuns

FROM outcomes

JOIN (classes JOIN ships ON classes.class=ships.class) ON ship=name

WHERE battle='Guadalcanal'

UNION

SELECT ship, displacement, numGuns

FROM outcomes
LEFT JOIN classes ON ship = classes.class

WHERE battle = 'Guadalcanal'

AND ship NOT IN (SELECT name FROM ships);

Exercise: 47 (Serge I: 2019-06-07)


Find the countries that have lost all their ships in battles.

--------------------------------------------------------------

Exercise: 48 (Serge I: 2003-02-16)


Find the ship classes having at least one ship sunk in battles.

SELECT class

FROM ships, outcomes

WHERE outcomes.ship = ships.name

AND result = 'sunk'

UNION

SELECT ship

FROM outcomes, classes

WHERE classes.class = outcomes.ship

AND result = 'sunk';

Exercise: 49 (Serge I: 2003-02-17)


Find the names of the ships having a gun caliber of 16 inches (including ships in the Outcomes table).

SELECT name

FROM ships

JOIN classes ON classes.class = ships.class

WHERE classes.bore = 16

UNION

SELECT ship

FROM outcomes, classes

WHERE outcomes.ship = classes.class

AND classes.bore = 16;

Exercise: 50 (Serge I: 2002-11-05)


Find the battles in which Kongo-class ships from the Ships table were engaged.

SELECT DISTINCT battle

FROM outcomes, ships

WHERE outcomes.ship = ships.name

AND ships.class = 'Kongo';

Exercise: 51 (Serge I: 2003-02-17)


Find the names of the ships with the largest number of guns among all ships having the same
displacement (including ships in the Outcomes table).

SELECT name

FROM (SELECT name, numGuns, displacement

FROM Ships JOIN Classes ON Classes.class=Ships.class

UNION

SELECT ship, numGuns, displacement

FROM Outcomes JOIN Classes ON ship=class) AS x

WHERE numGuns=(SELECT MAX(numGuns)

FROM(SELECT name, numGuns, displacement

FROM Ships JOIN Classes

ON Classes.class=Ships.class

UNION

SELECT ship,numGuns,displacement

FROM Outcomes JOIN Classes

ON ship=class) AS y

WHERE x.displacement=y.displacement);

Exercise: 52 (qwrqwr: 2010-04-23)


Determine the names of all ships in the Ships table that can be a Japanese battleship having at least nine
main guns with a caliber of less than 19 inches and a displacement of not more than 65 000 tons.

SELECT s.name

FROM ships s

LEFT JOIN classes c ON c.class = s.class

WHERE CASE WHEN c.numguns IS NULL THEN 9 ELSE c.numguns END > 8
AND CASE WHEN c.bore IS NULL THEN 8 ELSE c.bore END <19

AND CASE WHEN c.displacement IS NULL THEN 8 ELSE c.displacement END <= 65000

AND CASE WHEN c.type IS NULL THEN 'bb' ELSE c.type END = 'bb'

AND CASE WHEN c.country IS NULL THEN 'Japan' ELSE c.country END = 'Japan';

Exercise: 53 (Serge I: 2002-11-05)


With a precision of two decimal places, determine the average number of guns for the battleship classes.

SELECT CAST ( AVG(numGuns+0.0) AS NUMERIC(10,2) )

FROM Classes

WHERE type = 'bb';

Exercise: 54 (Serge I: 2003-02-14)


With a precision of two decimal places, determine the average number of guns for all battleships
(including the ones in the Outcomes table).

SELECT CAST(AVG(CAST(all_ships.numguns AS NUMERIC(4,2))) AS NUMERIC(4,2)) avg_guns

FROM (SELECT o.ship, c.numguns

FROM outcomes o, classes c

WHERE o.ship = c.class AND c.type = 'bb'

UNION

SELECT s.name, c.numguns

FROM ships s

INNER JOIN classes c ON s.class = c.class

WHERE c.type = 'bb') all_ships;

Exercise: 55 (Serge I: 2003-02-16)


For each class, determine the year the first ship of this class was launched.
If the lead ship’s year of launch is not known, get the minimum year of launch for the ships of this class.
Result set: class, year.

SELECT classes.class, MIN(launched)

FROM classes

FULL JOIN ships ON classes.class=ships.class

GROUP BY classes.class;
Exercise: 56 (Serge I: 2003-02-16)
For each class, find out the number of ships of this class that were sunk in battles.
Result set: class, number of ships sunk.

WITH t AS (SELECT c.class

FROM outcomes o, ships s, classes c

WHERE result = 'sunk'

AND o.ship = s.name

AND s.class = c.class

UNION ALL

SELECT c.class

FROM outcomes o, classes c

WHERE result = 'sunk'

AND o.ship = c.class

AND o.ship NOT IN (SELECT name FROM ships))

SELECT t1.class, (SELECT COUNT(1) FROM t t2 WHERE t1.class = t2.class )

FROM classes t1;

Exercise: 57 (Serge I: 2003-02-14)


For classes having irreparable combat losses and at least three ships in the database, display the name
of the class and the number of ships sunk.

SELECT class, COUNT(*)

FROM (SELECT class, name FROM ships

UNION

SELECT ship AS class, ship AS name

FROM outcomes

WHERE ship IN (SELECT class FROM classes)) AS a

JOIN outcomes b ON name=ship

WHERE result='sunk' AND class IN

(SELECT class FROM

(SELECT class, name FROM ships


UNION

SELECT ship AS class, ship AS name

FROM outcomes

WHERE ship IN (SELECT class

FROM classes)) c

GROUP BY class

HAVING COUNT(*)>=3)

GROUP BY class;

Exercise: 58 (Serge I: 2009-11-13)


For each product type and maker in the Product table, find out, with a precision of two decimal places, the
percentage ratio of the number of models of the actual type produced by the actual maker to the total
number of models by this maker.
Result set: maker, product type, the percentage ratio mentioned above.

SELECT p1.maker, p2.type,

CAST( 100.0 *

(SELECT COUNT(1) FROM product p WHERE p.maker = p1.maker AND p.type= p2.type)

(SELECT COUNT(1) FROM product p WHERE p.maker = p1.maker)

AS NUMERIC(12, 2))

FROM product p1, product p2

GROUP BY p1.maker, p2.type;

Exercise: 59 (Serge I: 2003-02-15)


Calculate the cash balance of each buy-back center for the database with money transactions being
recorded not more than once a day.
Result set: point, balance.

SELECT point, balance-

(CASE WHEN o2 IS NULL THEN 0 ELSE o2 END)

FROM (SELECT point, SUM(inc) balance

FROM income_o
GROUP BY point) AS t1

LEFT JOIN

(SELECT point o1, SUM(out) o2

FROM outcome_o

GROUP BY point) AS t2

ON point=o1;

Exercise: 60 (Serge I: 2003-02-15)


For the database with money transactions being recorded not more than once a day, calculate the cash
balance of each buy-back center at the beginning of 4/15/2001.
Note: exclude centers not having any records before the specified date.
Result set: point, balance

SELECT point, balance-

(CASE WHEN o2 IS NULL THEN 0 ELSE o2 END)

FROM (SELECT point, SUM(inc) balance FROM income_o

WHERE date<'2001-04-15'

GROUP BY point) AS t1

LEFT JOIN

(SELECT point o1, SUM(out) o2 FROM outcome_o

WHERE date<'2001-04-15'

GROUP BY point) AS t2

ON point=o1;

Exercise: 61 (Serge I: 2003-02-14)


For the database with money transactions being recorded not more than once a day, calculate the total
cash balance of all buy-back centers.

SELECT SUM(i) FROM

(SELECT point, SUM(inc) AS i FROM

income_o

GROUP BY point

UNION

SELECT point, -SUM(out) as i FROM


outcome_o

GROUP BY point

) AS t;

Exercise: 62 (Serge I: 2003-02-15)


For the database with money transactions being recorded not more than once a day, calculate the total
cash balance of all buy-back centers at the beginning of 04/15/2001.

SELECT

(SELECT SUM(in2.inc)

FROM income_o in2

WHERE date < '04/15/01')

(SELECT SUM(out2.out)

FROM outcome_o out2

WHERE date < '04/15/01');

Exercise: 63 (Serge I: 2003-04-08)


Find the names of different passengers that ever travelled more than once occupying seats with the same
number.

SELECT name FROM Passenger

WHERE ID_psg IN (SELECT ID_psg

FROM Pass_in_trip

GROUP BY place, ID_psg

HAVING COUNT(*)>1);
Exercise: 64 (Serge I: 2010-06-04)
Using the Income and Outcome tables, determine for each buy-back center the days when it received
funds but made no payments, and vice versa.
Result set: point, date, type of operation (inc/out), sum of money per day.

SELECT point, date, 'inc' type, SUM(inc) sum

FROM income i

WHERE NOT EXISTS (SELECT 1 FROM outcome o WHERE i.point = o.point AND i.date = o.date)

GROUP BY point, date

UNION

SELECT point, date, 'out' type, SUM(out) sum


FROM outcome o

WHERE NOT EXISTS (SELECT 1 FROM income i WHERE i.point = o.point AND i.date = o.date)

GROUP BY point, date;

Exercise: 65 (Serge I: 2009-08-24)


Number the unique pairs {maker, type} in the Product table, ordering them as follows:
- maker name in ascending order;
- type of product (type) in the order PC, Laptop, Printer.
If a manufacturer produces more than one type of product, its name should be displayed in the first row
only;
other rows for THIS manufacturer should contain an empty string (').

SELECT ROW_NUMBER() OVER(ORDER BY maker,

CASE

WHEN type LIKE 'pc' THEN 1

WHEN type LIKE 'laptop' THEN 2

ELSE 3

END) AS num,

CASE

WHEN ROW_NUMBER() OVER(PARTITION BY maker ORDER BY maker) <> 1

THEN ''

ELSE maker

END AS maker , type

FROM (SELECT DISTINCT maker, type FROM product) as q;

Exercise: 66 (Serge I: 2003-04-09)


For all days between 2003-04-01 and 2003-04-07 find the number of trips from Rostov.
Result set: date, number of trips.

WITH t1 AS (

SELECT '2003-04-01 00:00:00.000' date UNION

SELECT '2003-04-02 00:00:00.000' date UNION

SELECT '2003-04-03 00:00:00.000' date UNION

SELECT '2003-04-04 00:00:00.000' date UNION

SELECT '2003-04-05 00:00:00.000' date UNION

SELECT '2003-04-06 00:00:00.000' date UNION


SELECT '2003-04-07 00:00:00.000' date

SELECT tt.date, (SELECT COUNT(1) FROM (SELECT DISTINCT t.trip_no

FROM pass_in_trip pip, trip t

WHERE pip.trip_no = t.trip_no

AND t.town_from = 'rostov'

AND tt.date = pip.date) trips )

FROM t1 tt;

Exercise: 67 (Serge I: 2010-03-27)


Find out the number of routes with the greatest number of flights (trips).
Notes.
1) A - B and B - A are to be considered DIFFERENT routes.
2) Use the Trip table only.

SELECT COUNT(*)

FROM (SELECT TOP 1 WITH TIES COUNT(*) c, town_from, town_to

FROM trip

GROUP BY town_from, town_to

ORDER BY c DESC) AS t;

Exercise: 68 (Serge I: 2010-03-27)


Find out the number of routes with the greatest number of flights (trips).
Notes.
1) A - B and B - A are to be considered the SAME route.
2) Use the Trip table only.

SELECT COUNT(*)

FROM (SELECT TOP 1 WITH TIES SUM(c) cc, c1, c2

FROM (SELECT COUNT(*) c, town_from c1, town_to c2

FROM trip

WHERE town_from>=town_to

GROUP BY town_from, town_to


UNION ALL

SELECT COUNT(*) c,town_to, town_from FROM trip

WHERE town_to>town_from

GROUP BY town_from, town_to

) AS t

GROUP BY c1,c2

ORDER BY cc DESC

) AS tt;

Exercise: 69 (Serge I: 2011-01-06)


Using the Income and Outcome tables, find out the balance for each buy-back center by the end of each
day when funds were received or payments were made.
Note that the cash isn’t withdrawn, and the unspent balance/debt is carried forward to the next day.
Result set: buy-back center ID (point), date in dd/mm/yyyy format, unspent balance/debt by the end of
this day.

WITH base_set AS (SELECT point, date, inc FROM income

UNION ALL

SELECT point, date, -out FROM outcome

SELECT DISTINCT bs.point, CONVERT(varchar(10), bs.date, 103) day,

(SELECT SUM(inc) FROM base_set WHERE date <= bs.date AND point = bs.point) rem

FROM base_set bs;

Exercise: 70 (Serge I: 2003-02-14)


Get the battles in which at least three ships from the same country took part.

SELECT DISTINCT o.battle

FROM outcomes o

LEFT JOIN ships s ON s.name = o.ship

LEFT JOIN classes c ON o.ship = c.class OR s.class = c.class

WHERE c.country IS NOT NULL

GROUP BY c.country, o.battle

HAVING COUNT(o.ship) >= 3;

Exercise: 71 (Serge I: 2008-02-23)


Find the PC makers with all personal computer models produced by them being present in the PC table.

SELECT p.maker

FROM product p

LEFT JOIN pc ON pc.model = p.model

WHERE p.type = 'PC'

GROUP BY p.maker

HAVING COUNT(p.model) = COUNT(pc.model);

Exercise: 72 (Serge I: 2003-04-29)


Among the customers using a single airline, find distinct passengers who have flown most frequently.
Result set: passenger name, number of trips.

SELECT TOP 1 WITH TIES name, trip_Qty FROM passenger

JOIN

(SELECT c1, MAX(trip_Qty) trip_Qty FROM

(SELECT pass_in_trip.ID_psg c1, Trip.ID_comp c2, COUNT(*) trip_Qty FROM pass_in_trip

JOIN trip ON trip.trip_no=pass_in_trip.trip_no

GROUP BY pass_in_trip.ID_psg, Trip.ID_comp

) AS t

GROUP BY c1

HAVING COUNT(*)=1) AS tt

ON ID_psg=c1

ORDER BY trip_Qty DESC;

Exercise: 73 (Serge I: 2009-04-17)


For each country, determine the battles in which the ships of this country did not participate.
Result set: country, battle.

WITH t1 AS (SELECT c.country, o.battle

FROM classes c, outcomes o

WHERE c.class = o.ship

UNION

SELECT c.country, o.battle

FROM classes c, ships s, outcomes o


WHERE c.class = s.class AND s.name = o.ship

SELECT DISTINCT country, b.name

FROM classes c, battles b

WHERE

(SELECT COUNT(1)

FROM t1

WHERE t1.country = c.country

AND t1.battle = b.name) = 0;

Exercise: 83 (dorin_larsen: 2006-03-14)


Find out the names of the ships in the Ships table that meet at least four criteria from the following list:
numGuns = 8,
bore = 15,
displacement = 32000,
type = bb,
launched = 1915,
class = Kongo,
country = USA.

SELECT t.name

FROM

(SELECT s.name,

CASE WHEN c.numGuns = 8 THEN 1 ELSE 0 END AS c1,

CASE WHEN c.bore = 15 THEN 1 ELSE 0 END AS c2,

CASE WHEN c.displacement = 32000 THEN 1 ELSE 0 END AS c3,

CASE WHEN c.type = 'bb' THEN 1 ELSE 0 END AS c4,

CASE WHEN s.launched = 1915 THEN 1 ELSE 0 END AS c5,

CASE WHEN c.class = 'Kongo' THEN 1 ELSE 0 END AS c6,

CASE WHEN c.country = 'USA' THEN 1 ELSE 0 END AS c7

FROM ships s INNER JOIN classes c ON s.class = c.class

)t

WHERE (t.c1 + t.c2 + t.c3 + t.c4 + t.c5 + t.c6 + t.c7) >= 4;

Exercise: 92 (ZrenBy: 2003-09-01)


Get all white squares that have been painted only with spray cans empty at present.
Output the square names.

SELECT Q_NAME

FROM utQ

WHERE Q_ID IN (SELECT DISTINCT B.B_Q_ID

FROM (SELECT B_Q_ID

FROM utB

GROUP BY B_Q_ID

HAVING SUM(B_VOL) = 765) AS B

WHERE B.B_Q_ID NOT IN (SELECT B_Q_ID

FROM utB

WHERE B_V_ID IN (SELECT B_V_ID

FROM utB

GROUP BY B_V_ID

HAVING SUM(B_VOL) < 255)));

Exercise: 96 (ZrenBy: 2003-09-01)


Considering only red spray cans used more than once, get those that painted squares currently having a
non-zero blue component.
Result set: spray can name.

SELECT v2.v_name

FROM utv v, utb b, utq q, utb b2, utv v2

WHERE v.v_color = 'b' AND

b.b_q_id = q.q_id AND

b.b_v_id = v.v_id AND

b2.b_q_id = q.q_id AND

v2.v_id = b2.b_v_id

GROUP BY v2.v_name

INTERSECT

SELECT v.v_name

FROM utv v, utb b


WHERE v.v_id = b.b_v_id AND

v.v_color = 'r'

GROUP BY v.v_name

HAVING COUNT(1) > 1;

Exercise: 110 (Serge I: 2003-12-24)


Find out the names of different passengers who ever travelled on a flight that took off on Saturday and
landed on Sunday.

SELECT name

FROM passenger

WHERE id_psg IN (SELECT id_psg

FROM pass_in_trip pit

JOIN trip t ON pit.trip_no = t.trip_no

WHERE time_in < time_out AND datepart(dw, date) = 7);

Exercise: 113 (Serge I: 2003-12-24)


How much paint of each color is needed to dye all squares white?
Result set: amount of each paint in order (R,G,B).

SELECT

- (SELECT SUM(b_vol)

FROM utb b

WHERE b.b_v_id IN (SELECT v_id FROM utv v WHERE v.v_color = 'R'))

+ (SELECT SUM(255) FROM utq) red,

- (SELECT SUM(b_vol)

FROM utb b

WHERE b.b_v_id IN (SELECT v_id FROM utv v WHERE v.v_color = 'g'))

+ (SELECT SUM(255) FROM utq) green,

- (SELECT SUM(b_vol)

FROM utb b

WHERE b.b_v_id IN (SELECT v_id FROM utv v WHERE v.v_color = 'b'))


+ (SELECT SUM(255) FROM utq) blue;

You might also like