Advertisement
Silviya7

AdditionalExersises

Jun 14th, 2022
1,768
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 10.99 KB | None | 0 0
  1. USE Diablo
  2.  
  3. --T01 Number of Users for Email Provider
  4.  
  5. SELECT ep.[Email Provider], COUNT(ep.[Email Provider]) AS [Number Of Users] FROM
  6. (SELECT SUBSTRING(Email, CHARINDEX('@', Email, 1) +1, LEN(Email)-CHARINDEX('@', Email, 1)) AS [Email Provider]
  7. FROM Users) AS ep
  8. GROUP BY ep.[Email Provider]
  9. ORDER BY [Number Of Users] DESC, ep.[Email Provider]
  10.  
  11. --T02 All Users in Games
  12.  
  13. SELECT g.[Name] AS [Game]
  14. , gt.[Name] AS [Game Type]
  15. , u.Username
  16. , ug.[Level]
  17. , ug.Cash
  18. , c.[Name] AS [Character]
  19. FROM Users AS u
  20. JOIN UsersGames AS ug ON u.Id = ug.UserId
  21. JOIN Characters AS c ON ug.CharacterId = c.Id
  22. JOIN Games AS g ON ug.GameId = g.Id
  23. JOIN GameTypes AS gt ON g.GameTypeId = gt.Id
  24. ORDER BY ug.[Level] DESC, u.Username, g.[Name]
  25.  
  26. --T03 Users in Games with Their Items
  27.  
  28. SELECT u.Username
  29. , g.[Name] AS [Game]
  30. , COUNT(i.Id) AS [Items Count]
  31. , SUM(i.Price) AS [Items Price]
  32. FROM Users AS u
  33. JOIN UsersGames AS ug ON u.Id=ug.UserId
  34. JOIN Games AS g ON ug.GameId = g.Id
  35. JOIN UserGameItems AS ugi ON ug.Id = ugi.UserGameId
  36. JOIN Items AS i ON ugi.ItemId = i.Id
  37. GROUP BY u.Username, G.[Name]
  38. HAVING COUNT(i.Id) >=10
  39. ORDER BY [Items Count] DESC, [Items Price] DESC, Username
  40.  
  41. --T04 *User in Games with Their Statistics
  42.  
  43. SELECT u.Username,
  44. g.[Name] AS [Game]
  45. , MAX(c.[Name]) AS [Character]
  46. , SUM(sta.Strength)+MAX(st.Strength)+MAX(s.Strength) AS [Strength]
  47. , SUM(sta.Defence)+MAX(st.Defence)+MAX(s.Defence) AS [Defence]
  48. , SUM(sta.Speed)+MAX(st.Speed)+MAX(s.Speed) AS [Speed]
  49. , SUM(sta.Mind)+MAX(st.Mind)+MAX(s.Mind) AS [Mind]
  50. , SUM(sta.Luck)+MAX(st.Luck)+MAX(s.Luck) AS [Luck]
  51. FROM Users AS u
  52. JOIN UsersGames AS ug ON u.Id = ug.UserId
  53. JOIN Characters AS c ON ug.CharacterId = c.Id
  54. JOIN [Statistics] AS s ON c.StatisticId = s.Id
  55. JOIN Games AS g ON ug.GameId = g.Id
  56. JOIN GameTypes AS gt ON g.GameTypeId = gt.Id
  57. JOIN [Statistics]  AS st ON gt.BonusStatsId = st.Id
  58. JOIN UserGameItems AS ugi ON ug.Id = ugi.UserGameId
  59. JOIN Items AS i ON i.Id = ugi.ItemId
  60. JOIN [Statistics] AS sta ON i.StatisticId = sta.Id
  61. GROUP BY u.Username, g.[Name]
  62. ORDER BY Strength DESC, Defence DESC, Speed DESC, Mind DESC, Luck DESC
  63.  
  64. --T05 All Items with Greater than Average Statistics
  65.  
  66. SELECT i.[Name], i.[Price], i.[MinLevel], s.[Strength], s.[Defence], s.[Speed], s.[Luck], s.[Mind]
  67. FROM [Items] AS i
  68. JOIN [Statistics] AS s ON i.StatisticId = s.Id
  69. WHERE s.[Mind] > (SELECT AVG([Mind]) FROM [Statistics]) AND s.[Luck] > (SELECT AVG([Luck]) FROM [Statistics]) AND s.[Speed] > (SELECT AVG([Speed]) FROM [Statistics])
  70. ORDER BY i.[Name]
  71.  
  72. --T06 Display All Items with Information about Forbidden Game Type
  73.  
  74. SELECT i.[Name], i.Price, i.MinLevel, gt.[Name] FROM Items AS i
  75. LEFT JOIN GameTypeForbiddenItems AS gtfi ON i.Id = gtfi.ItemId
  76. LEFT JOIN GameTypes AS gt ON gtfi.GameTypeId = gt.Id
  77. ORDER BY gt.[Name] DESC,i.[Name]
  78.  
  79. --T07 Buy Items for User in Game
  80.  
  81. --NOT WORKING IN JUDGE, WORKING LOCALLY
  82.  
  83. SELECT Id FROM Items WHERE [Name] IN ('Blackguard', 'Bottomless Potion of Amplification', 'Eye of Etlich (Diablo III)', 'Gem of Efficacious Toxin', 'Golden Gorget of Leoric', 'Hellfire Amulet') --51, 71, 157, 184, 197, 223
  84.  
  85. SELECT ug.Id FROM Users AS u
  86. JOIN UsersGames AS ug ON u.Id = ug.UserId
  87. JOIN Games AS g ON ug.GameId = g.Id
  88. WHERE Username = 'Alex' AND g.[Name] = 'Edinburgh'
  89.  
  90. GO
  91.  
  92. CREATE OR ALTER PROC usp_BuyItemsForGameAndUser(@UserName NVARCHAR(50), @GameName NVARCHAR(50), @ItemName NVARCHAR(50))
  93. AS
  94. BEGIN TRANSACTION
  95. DECLARE @AvailableCash MONEY = (SELECT Cash FROM Users AS u JOIN UsersGames AS ug ON u.Id = ug.UserId JOIN Games AS g ON ug.GameId = g.Id WHERE Username = @UserName AND g.[Name] = @GameName)
  96. DECLARE @ItemPrice MONEY = (SELECT Price FROM Items WHERE [Name] = @ItemName)
  97. DECLARE @UserGameId INT = (SELECT ug.Id FROM Users AS u JOIN UsersGames AS ug ON u.Id = ug.UserId JOIN Games AS g ON ug.GameId = g.Id WHERE u.Username = @UserName AND g.[Name] = @GameName)
  98. DECLARE @ItemId INT = (SELECT Id FROM Items WHERE [Name] = @ItemName)
  99.  
  100. IF @AvailableCash < @ItemPrice
  101. BEGIN
  102.     ROLLBACK
  103.     RETURN
  104. END
  105.  
  106. BEGIN
  107. UPDATE UsersGames
  108. SET Cash -= @ItemPrice
  109. WHERE Id = @UserGameId
  110.  
  111. INSERT INTO UserGameItems VALUES
  112. (@ItemId, @UserGameId)
  113. END
  114. COMMIT
  115.  
  116. SELECT * FROM UserGameItems AS ugi
  117. JOIN Items AS i ON ugi.ItemId = i.Id
  118. WHERE ugi.UserGameId= 235
  119.  
  120.  
  121. EXEC usp_BuyItemsForGameAndUser 'Alex', 'Edinburgh', 'Blackguard'
  122. EXEC usp_BuyItemsForGameAndUser 'Alex', 'Edinburgh', 'Bottomless Potion of Amplification'
  123. EXEC usp_BuyItemsForGameAndUser 'Alex', 'Edinburgh', 'Eye of Etlich (Diablo III)'
  124. EXEC usp_BuyItemsForGameAndUser 'Alex', 'Edinburgh', 'Gem of Efficacious Toxin'
  125. EXEC usp_BuyItemsForGameAndUser 'Alex', 'Edinburgh', 'Golden Gorget of Leoric'
  126. EXEC usp_BuyItemsForGameAndUser 'Alex', 'Edinburgh', 'Hellfire Amulet'
  127.  
  128. SELECT u.Username, g.[Name],ug.Cash, i.[Name] FROM Users AS u
  129. JOIN UsersGames AS ug ON u.Id = ug.UserId
  130. JOIN Games AS g ON ug.GameId = g.Id
  131. JOIN UserGameItems AS ugi ON ug.Id = ugi.UserGameId
  132. JOIN Items AS i ON ugi.ItemId = i.Id
  133. WHERE g.Name = 'Edinburgh'
  134. ORDER BY i.[Name]
  135.  
  136. --ANOTHER SOLUTION WORKING IN JUDGE SYSTEM
  137.  
  138. SELECT Id FROM Items
  139. WHERE [Name] IN ('Blackguard', 'Bottomless Potion of Amplification', 'Eye of Etlich (Diablo III)', 'Gem of Efficacious Toxin', 'Golden Gorget of Leoric', 'Hellfire Amulet') --51, 71, 157, 184, 197, 223
  140.  
  141. SELECT ug.Id FROM Users AS u
  142. JOIN UsersGames AS ug ON u.Id = ug.UserId
  143. JOIN Games AS g ON ug.GameId = g.Id
  144. WHERE Username = 'Alex' AND g.[Name] = 'Edinburgh' --235
  145.  
  146. DECLARE @AvailableCash1 MONEY = (Select Cash FROM Users AS u JOIN UsersGames AS ug ON u.Id = ug.UserId JOIN Games AS g ON ug.GameId = g.Id WHERE Username = 'Alex' AND g.[Name] = 'Edinburgh')
  147. DECLARE @ItemsPrice MONEY = (SELECT SUM(Price) FROM Items WHERE [Name] IN ('Blackguard', 'Bottomless Potion of Amplification', 'Eye of Etlich (Diablo III)', 'Gem of Efficacious Toxin', 'Golden Gorget of Leoric', 'Hellfire Amulet'))
  148. DECLARE @UserGameId1 INT = (SELECT ug.Id FROM Users AS u JOIN UsersGames AS ug ON u.Id = ug.UserId JOIN Games AS g ON ug.GameId = g.Id WHERE Username = 'Alex' AND g.[Name] = 'Edinburgh')
  149.  
  150. BEGIN TRANSACTION
  151.  
  152. IF @AvailableCash1 >= @ItemsPrice
  153.  
  154. BEGIN
  155.  
  156. UPDATE UsersGames
  157. SET Cash -= @ItemsPrice
  158. WHERE Id = @UserGameId1
  159.  
  160. INSERT INTO UserGameItems VALUES
  161. ((SELECT Id FROM Items WHERE [Name] = 'Blackguard'), @UserGameId1),
  162. ((SELECT Id FROM Items WHERE [Name] = 'Bottomless Potion of Amplification'), @UserGameId1),
  163. ((SELECT Id FROM Items WHERE [Name] = 'Eye of Etlich (Diablo III)'), @UserGameId1),
  164. ((SELECT Id FROM Items WHERE [Name] = 'Gem of Efficacious Toxin'), @UserGameId1),
  165. ((SELECT Id FROM Items WHERE [Name] = 'Golden Gorget of Leoric'), @UserGameId1),
  166. ((SELECT Id FROM Items WHERE [Name] = 'Hellfire Amulet'), @UserGameId1)
  167. END
  168. COMMIT
  169.  
  170. SELECT u.Username
  171. , g.[Name]
  172. , ug.Cash
  173. , i.[Name] AS [Item Name]
  174. FROM Users AS u
  175. JOIN UsersGames AS ug ON u.Id = ug.UserId
  176. JOIN Games AS g ON ug.GameId = g.Id
  177. JOIN UserGameItems AS ugi ON ug.Id = ugi.UserGameId
  178. JOIN Items AS i ON ugi.ItemId = i.Id
  179. WHERE g.[Name] = 'Edinburgh'
  180. ORDER BY i.[Name]
  181.  
  182. USE Geography
  183.  
  184. --T08 Peaks and Mountains
  185.  
  186. SELECT p.PeakName
  187. , m.MountainRange AS [Mountain]
  188. , p.Elevation
  189. FROM Mountains AS m
  190. JOIN Peaks AS p ON m.Id = p.MountainId
  191. ORDER BY p.Elevation DESC, p.PeakName
  192.  
  193. --T09 Peaks with Their Mountain, Country and Continent
  194.  
  195. SELECT p.PeakName
  196. , m.MountainRange AS [Mountain]
  197. , c.CountryName
  198. , cont.ContinentName
  199. FROM Peaks AS p
  200. JOIN Mountains AS m ON p.MountainId = m.Id
  201. JOIN MountainsCountries AS mc ON m.Id = mc.MountainId
  202. JOIN Countries AS c ON mc.CountryCode = c.CountryCode
  203. JOIN Continents AS cont ON c.ContinentCode = cont.ContinentCode
  204. ORDER BY p.PeakName, c.CountryName
  205.  
  206. --T10 Rivers by Country
  207.  
  208. SELECT c.CountryName
  209. , cont.ContinentName
  210. , ISNULL(COUNT(r.Id), 0) AS [RiversCount]
  211. , SUM(ISNULL(r.Length,0)) AS [TotalLength]
  212. FROM Countries AS c
  213. LEFT JOIN Continents AS cont ON c.ContinentCode = cont.ContinentCode
  214. LEFT JOIN CountriesRivers AS cr ON c.CountryCode = cr.CountryCode
  215. LEFT JOIN Rivers AS r ON cr.RiverId = r.Id
  216. GROUP BY c.CountryName, cont.ContinentName
  217. ORDER BY RiversCount DESC, TotalLength DESC, c.CountryName
  218.  
  219. --T11 Count of Countries by Currency
  220.  
  221. SELECT curr.CurrencyCode
  222. , curr.[Description] AS [Currency]
  223. , COUNT(c.CountryCode) AS [NumberOfCountries]
  224. FROM Currencies AS curr
  225. LEFT JOIN Countries AS c ON curr.CurrencyCode = c.CurrencyCode
  226. GROUP BY curr.CurrencyCode, curr.[Description]
  227. ORDER BY NumberOfCountries DESC, curr.[Description]
  228.  
  229. --T12 Population and Area by Continent
  230.  
  231. SELECT cont.ContinentName
  232. , SUM(CAST(c.AreaInSqKm AS bigint)) AS [CountriesArea]
  233. , SUM(CAST(c.[Population] AS bigint)) AS [CountriesPopulation]
  234. FROM Continents AS cont
  235. LEFT JOIN Countries AS c ON cont.ContinentCode = c.ContinentCode
  236. GROUP BY cont.ContinentName
  237. ORDER BY CountriesPopulation DESC
  238.  
  239. --T13 Monasteries by Country
  240.  
  241. --13.1
  242.  
  243. CREATE TABLE Monasteries
  244. (
  245. Id INT PRIMARY KEY IDENTITY,
  246. [Name] VARCHAR(50),
  247. CountryCode CHAR(2) FOREIGN KEY REFERENCES Countries(CountryCode)
  248. )
  249.  
  250. --13.2
  251.  
  252. INSERT INTO Monasteries(Name, CountryCode) VALUES
  253. ('Rila Monastery “St. Ivan of Rila”', 'BG'),
  254. ('Bachkovo Monastery “Virgin Mary”', 'BG'),
  255. ('Troyan Monastery “Holy Mother''s Assumption”', 'BG'),
  256. ('Kopan Monastery', 'NP'),
  257. ('Thrangu Tashi Yangtse Monastery', 'NP'),
  258. ('Shechen Tennyi Dargyeling Monastery', 'NP'),
  259. ('Benchen Monastery', 'NP'),
  260. ('Southern Shaolin Monastery', 'CN'),
  261. ('Dabei Monastery', 'CN'),
  262. ('Wa Sau Toi', 'CN'),
  263. ('Lhunshigyia Monastery', 'CN'),
  264. ('Rakya Monastery', 'CN'),
  265. ('Monasteries of Meteora', 'GR'),
  266. ('The Holy Monastery of Stavronikita', 'GR'),
  267. ('Taung Kalat Monastery', 'MM'),
  268. ('Pa-Auk Forest Monastery', 'MM'),
  269. ('Taktsang Palphug Monastery', 'BT'),
  270. ('Sьmela Monastery', 'TR')
  271.  
  272. --13.3 THIS POINT NOT FOR SUBMISSION IN JUDGE SYSTEM
  273.  
  274. ALTER TABLE Countries
  275. ADD IsDeleted BIT NOT NULL DEFAULT 0
  276.  
  277. --13.4
  278.  
  279. UPDATE Countries
  280. SET IsDeleted = 1
  281. WHERE CountryName IN (SELECT c.CountryName FROM Countries AS c
  282. JOIN CountriesRivers AS cr ON c.CountryCode = cr.CountryCode
  283. JOIN Rivers AS r ON cr.RiverId = r.Id
  284. GROUP BY c.CountryName
  285. HAVING COUNT(cr.RiverId) >3)
  286.  
  287. --13.5
  288.  
  289. SELECT m.[Name] AS [Monastery]
  290. , c.CountryName AS [Country]
  291. FROM Monasteries AS m
  292. JOIN Countries AS c ON m.CountryCode = c.CountryCode
  293. WHERE IsDeleted = 0
  294. ORDER BY m.[Name]
  295.  
  296. --T14 Monasteries by Continents and Countries
  297.  
  298. --14.1
  299.  
  300. UPDATE Countries
  301. SET CountryName  = 'Burma'
  302. WHERE CountryName = 'Myanmar'
  303.  
  304. --14.2
  305.  
  306. INSERT INTO Monasteries VALUES
  307. ('Hanga Abbey', (SELECT CountryCode FROM Countries WHERE CountryName = 'Tanzania'))
  308.  
  309. --14.3
  310.  
  311. INSERT INTO Monasteries VALUES
  312. ('Myin-Tin-Daik', (SELECT CountryCode FROM Countries WHERE CountryName = 'Myanmar'))
  313.  
  314. --14.4
  315.  
  316. SELECT cont.ContinentName
  317. , c.CountryName
  318. , COUNT(m.Id) AS [MonasteriesCount]
  319. FROM Countries AS c
  320. LEFT JOIN Continents AS cont ON c.ContinentCode = cont.ContinentCode
  321. LEFT JOIN Monasteries AS m ON c.CountryCode = m.CountryCode
  322. WHERE c.IsDeleted = 0
  323. GROUP BY cont.ContinentName,c.CountryName
  324. ORDER BY MonasteriesCount DESC, c.CountryName
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement