67% found this document useful (3 votes)
6K views28 pages

SQL EX - Ru Exercises

The document contains 32 multi-line SQL queries and exercises. The queries select data from various tables like pc, laptop, printer, product, and more. They perform operations like finding specific records that meet criteria, aggregating results, finding averages, and joining tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
67% found this document useful (3 votes)
6K views28 pages

SQL EX - Ru Exercises

The document contains 32 multi-line SQL queries and exercises. The queries select data from various tables like pc, laptop, printer, product, and more. They perform operations like finding specific records that meet criteria, aggregating results, finding averages, and joining tables.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

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 [Link] < $500;

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


List all printer makers. Result set: maker.

SELECT DISTINCT maker

FROM product

WHERE [Link] = '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 [Link] > 1000;

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


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

SELECT *

FROM printer

WHERE [Link] = '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 [Link] < 600 AND ([Link] = '12x' OR [Link] = '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 [Link], [Link]

FROM laptop

JOIN product ON [Link] = [Link]

WHERE [Link] >= 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 [Link], [Link]

FROM product

JOIN pc ON [Link] = [Link]

WHERE [Link] = 'B'

UNION

SELECT [Link], [Link]

FROM product

JOIN laptop ON [Link] = [Link]

WHERE [Link] = 'B'

UNION

SELECT [Link], [Link]

FROM product

JOIN printer ON [Link] = [Link]

WHERE [Link] = '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 [Link] = [Link]

WHERE [Link] >= 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 [Link] = [Link]

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 [Link], [Link], [Link]

FROM ships

JOIN classes ON [Link] = [Link]

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 [Link], [Link], [Link], [Link]

FROM PC p1, PC p2

WHERE [Link] = [Link] AND [Link] = [Link]

GROUP BY [Link], [Link], [Link], [Link]

HAVING [Link] > [Link];

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 [Link], [Link], [Link]

FROM product, laptop

WHERE [Link] < (SELECT MIN(speed) FROM pc)

AND [Link] = 'laptop';

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


Find the makers of the cheapest color printers.
Result set: maker, price.
SELECT DISTINCT [Link], [Link]

FROM product pro

INNER JOIN printer pri on [Link] = [Link]

WHERE color='y' AND [Link] = (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 [Link] = [Link]

GROUP BY [Link];

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 [Link] = [Link]

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 [Link] FROM product p1

INNER JOIN pc p2 ON [Link] = [Link]

WHERE [Link] >= 750

INTERSECT

SELECT [Link] FROM product p

INNER JOIN laptop l ON [Link] = [Link]

WHERE [Link] >= 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 [Link]

FROM product, pc, laptop, printer

WHERE [Link] = (SELECT MAX(price) FROM pc)

AND [Link] = (SELECT MAX(price) FROM laptop)

AND [Link] = (SELECT MAX(price) FROM printer)

AND (

([Link] >= [Link] AND [Link] >= [Link]

AND [Link] = [Link])

OR

([Link] >= [Link] AND [Link] >= [Link]

AND [Link] = [Link])

OR
([Link] >= [Link] AND [Link] >= [Link]

AND [Link] = [Link]));

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 [Link]

FROM pc

INNER JOIN product p ON [Link] = [Link]

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 [Link], AVG([Link])

FROM pc

INNER JOIN product ON [Link] = [Link]

WHERE [Link] 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 [Link], [Link], [Link], [Link]

FROM Income_o i

LEFT JOIN Outcome_o o ON [Link] = [Link] AND [Link] = [Link]

UNION

SELECT [Link], [Link], [Link], [Link]

FROM outcome_o o

LEFT JOIN income_o i ON [Link] = [Link] AND [Link] = [Link];

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 [Link], [Link], out, inc

FROM Income LEFT JOIN

Outcome ON [Link] = [Link] AND

[Link] = [Link] AND [Link]= [Link]

UNION ALL

SELECT [Link], [Link], out, inc

FROM Outcome LEFT JOIN

Income ON [Link] = [Link] AND

[Link] = [Link] AND [Link]=[Link]) 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 [Link]=[Link]

UNION

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

WHERE [Link]=[Link]

AND [Link] 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 [Link] = [Link]

WHERE [Link] > 35000 AND [Link] >= 1922 AND [Link] = '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 [Link]

FROM

(SELECT [Link], [Link]

FROM ships s

UNION

SELECT [Link] as 'class', [Link]

FROM Outcomes o

WHERE NOT EXISTS( SELECT * FROM Ships s WHERE [Link] = [Link])) s

INNER JOIN Classes c ON [Link] = [Link]

GROUP by [Link]

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 [Link]

FROM outcomes o

LEFT JOIN Battles b ON [Link]=[Link]

WHERE [Link] = 'damaged'


and EXISTS(SELECT *

FROM outcomes o2

LEFT JOIN battles b2 ON [Link]=[Link]

WHERE [Link]=[Link]

and [Link] > [Link]);

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 [Link]=[Link]) ON ship=name

WHERE battle='Guadalcanal'

UNION

SELECT ship, displacement, numGuns

FROM outcomes
LEFT JOIN classes ON ship = [Link]

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 [Link] = [Link]

AND result = 'sunk'

UNION

SELECT ship

FROM outcomes, classes

WHERE [Link] = [Link]

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 [Link] = [Link]

WHERE [Link] = 16

UNION

SELECT ship

FROM outcomes, classes

WHERE [Link] = [Link]

AND [Link] = 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 [Link] = [Link]

AND [Link] = '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 [Link]=[Link]

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 [Link]=[Link]

UNION

SELECT ship,numGuns,displacement

FROM Outcomes JOIN Classes

ON ship=class) AS y

WHERE [Link]=[Link]);

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 [Link]

FROM ships s

LEFT JOIN classes c ON [Link] = [Link]

WHERE CASE WHEN [Link] IS NULL THEN 9 ELSE [Link] END > 8
AND CASE WHEN [Link] IS NULL THEN 8 ELSE [Link] END <19

AND CASE WHEN [Link] IS NULL THEN 8 ELSE [Link] END <= 65000

AND CASE WHEN [Link] IS NULL THEN 'bb' ELSE [Link] END = 'bb'

AND CASE WHEN [Link] IS NULL THEN 'Japan' ELSE [Link] 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 [Link], [Link]

FROM outcomes o, classes c

WHERE [Link] = [Link] AND [Link] = 'bb'

UNION

SELECT [Link], [Link]

FROM ships s

INNER JOIN classes c ON [Link] = [Link]

WHERE [Link] = '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 [Link], MIN(launched)

FROM classes

FULL JOIN ships ON [Link]=[Link]

GROUP BY [Link];
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 [Link]

FROM outcomes o, ships s, classes c

WHERE result = 'sunk'

AND [Link] = [Link]

AND [Link] = [Link]

UNION ALL

SELECT [Link]

FROM outcomes o, classes c

WHERE result = 'sunk'

AND [Link] = [Link]

AND [Link] NOT IN (SELECT name FROM ships))

SELECT [Link], (SELECT COUNT(1) FROM t t2 WHERE [Link] = [Link] )

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 [Link], [Link],

CAST( 100.0 *

(SELECT COUNT(1) FROM product p WHERE [Link] = [Link] AND [Link]= [Link])

(SELECT COUNT(1) FROM product p WHERE [Link] = [Link])

AS NUMERIC(12, 2))

FROM product p1, product p2

GROUP BY [Link], [Link];

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([Link])

FROM income_o in2

WHERE date < '04/15/01')

(SELECT SUM([Link])

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 [Link] = [Link] AND [Link] = [Link])

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 [Link] = [Link] AND [Link] = [Link])

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 [Link].000' date UNION

SELECT '2003-04-02 [Link].000' date UNION

SELECT '2003-04-03 [Link].000' date UNION

SELECT '2003-04-04 [Link].000' date UNION

SELECT '2003-04-05 [Link].000' date UNION

SELECT '2003-04-06 [Link].000' date UNION


SELECT '2003-04-07 [Link].000' date

SELECT [Link], (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 [Link] = [Link]) 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 [Link], CONVERT(varchar(10), [Link], 103) day,

(SELECT SUM(inc) FROM base_set WHERE date <= [Link] AND point = [Link]) 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 [Link]

FROM outcomes o

LEFT JOIN ships s ON [Link] = [Link]

LEFT JOIN classes c ON [Link] = [Link] OR [Link] = [Link]

WHERE [Link] IS NOT NULL

GROUP BY [Link], [Link]

HAVING COUNT([Link]) >= 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 [Link]

FROM product p

LEFT JOIN pc ON [Link] = [Link]

WHERE [Link] = 'PC'

GROUP BY [Link]

HAVING COUNT([Link]) = COUNT([Link]);

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 [Link], [Link]

FROM classes c, outcomes o

WHERE [Link] = [Link]

UNION

SELECT [Link], [Link]

FROM classes c, ships s, outcomes o


WHERE [Link] = [Link] AND [Link] = [Link]

SELECT DISTINCT country, [Link]

FROM classes c, battles b

WHERE

(SELECT COUNT(1)

FROM t1

WHERE [Link] = [Link]

AND [Link] = [Link]) = 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 [Link]

FROM

(SELECT [Link],

CASE WHEN [Link] = 8 THEN 1 ELSE 0 END AS c1,

CASE WHEN [Link] = 15 THEN 1 ELSE 0 END AS c2,

CASE WHEN [Link] = 32000 THEN 1 ELSE 0 END AS c3,

CASE WHEN [Link] = 'bb' THEN 1 ELSE 0 END AS c4,

CASE WHEN [Link] = 1915 THEN 1 ELSE 0 END AS c5,

CASE WHEN [Link] = 'Kongo' THEN 1 ELSE 0 END AS c6,

CASE WHEN [Link] = 'USA' THEN 1 ELSE 0 END AS c7

FROM ships s INNER JOIN classes c ON [Link] = [Link]

)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