Advertisement
Dianov

Databases MSSQL Server Exam - 19 Feb 2023

Feb 19th, 2023
2,113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.45 KB | Source Code | 0 0
  1. -- Databases MSSQL Server Exam - 19 Feb 2023
  2.  
  3. CREATE DATABASE Boardgames
  4. GO
  5.  
  6. USE Boardgames
  7. GO
  8.  
  9. -- Problem 01. DDL
  10.  
  11. CREATE TABLE [Categories]
  12. (
  13.     [Id] INT PRIMARY KEY IDENTITY(1, 1),
  14.     [Name] VARCHAR(50) NOT NULL
  15. )
  16.  
  17. CREATE TABLE [Addresses]
  18. (
  19.     [Id] INT PRIMARY KEY IDENTITY(1, 1),
  20.     [StreetName] NVARCHAR(100) NOT NULL,
  21.     [StreetNumber] INT NOT NULL,
  22.     [Town] VARCHAR(30) NOT NULL,
  23.     [Country] VARCHAR(50) NOT NULL,
  24.     [ZIP] INT NOT NULL
  25. )
  26.  
  27. CREATE TABLE [Publishers]
  28. (
  29.     [Id] INT PRIMARY KEY IDENTITY(1, 1),
  30.     [Name] VARCHAR(30) UNIQUE NOT NULL,
  31.     [AddressId] INT FOREIGN KEY REFERENCES [Addresses]([Id]) NOT NULL,
  32.     [Website] NVARCHAR(40),
  33.     [Phone] NVARCHAR(20)
  34. )
  35.  
  36. CREATE TABLE [PlayersRanges]
  37. (
  38.     [Id] INT PRIMARY KEY IDENTITY(1, 1),
  39.     [PlayersMin] INT NOT NULL,
  40.     [PlayersMax] INT NOT NULL
  41. )
  42.  
  43. CREATE TABLE [Boardgames]
  44. (
  45.     [Id] INT PRIMARY KEY IDENTITY(1, 1),
  46.     [Name] NVARCHAR(30) NOT NULL,
  47.     [YearPublished] INT NOT NULL,
  48.     [Rating] DECIMAL(18,2) NOT NULL,
  49.     [CategoryId] INT FOREIGN KEY REFERENCES [Categories]([Id]) NOT NULL,
  50.     [PublisherId] INT FOREIGN KEY REFERENCES [Publishers]([Id]) NOT NULL,
  51.     [PlayersRangeId] INT FOREIGN KEY REFERENCES [PlayersRanges]([Id]) NOT NULL
  52. )
  53.  
  54. CREATE TABLE [Creators]
  55. (
  56.     [Id] INT PRIMARY KEY IDENTITY(1, 1),
  57.     [FirstName] NVARCHAR(30) NOT NULL,
  58.     [LastName] NVARCHAR(30) NOT NULL,
  59.     [Email] NVARCHAR(30) NOT NULL
  60. )
  61.  
  62. CREATE TABLE [CreatorsBoardgames]
  63. (
  64.     [CreatorId] INT FOREIGN KEY REFERENCES [Creators]([Id]) NOT NULL,
  65.     [BoardgameId] INT FOREIGN KEY REFERENCES [Boardgames]([Id]) NOT NULL,
  66.     PRIMARY KEY ([CreatorId], [BoardgameId])
  67. )
  68.  
  69. GO
  70.  
  71. -- Problem 02. Insert
  72.  
  73. INSERT INTO [Boardgames]([Name], [YearPublished], [Rating], [CategoryId], [PublisherId], [PlayersRangeId]) VALUES
  74. ('Deep Blue', 2019, 5.67, 1, 15, 7),
  75. ('Paris', 2016, 9.78, 7, 1, 5),
  76. ('Catan: Starfarers', 2021, 9.87, 7, 13, 6),
  77. ('Bleeding Kansas', 2020, 3.25, 3, 7, 4),
  78. ('One Small Step', 2019, 5.75, 5, 9, 2)
  79.  
  80. INSERT INTO [Publishers]([Name], [AddressId], [Website], [Phone]) VALUES
  81. ('Agman Games', 5, 'www.agmangames.com', '+16546135542'),
  82. ('Amethyst Games', 7, 'www.amethystgames.com', '+15558889992'),
  83. ('BattleBooks', 13, 'www.battlebooks.com', '+12345678907')
  84.  
  85. GO
  86.  
  87. -- Problem 03. Update
  88.  
  89. UPDATE [PlayersRanges]
  90.    SET [PlayersMax] = [PlayersMax] + 1
  91.  WHERE [PlayersMin] = 2 AND [PlayersMax] = 2
  92.  
  93. UPDATE [Boardgames]
  94.    SET [Name] = [Name] + 'V2'
  95.  WHERE [YearPublished] >= 2020
  96.  
  97. GO
  98.  
  99. -- Problem 04. Delete
  100.  
  101. DELETE FROM [CreatorsBoardgames]
  102. WHERE [BoardgameId] IN (SELECT [Id] FROM [Boardgames] WHERE [PublisherId] = 1)
  103.  
  104. DELETE FROM [Boardgames]
  105. WHERE [PublisherId] IN (SELECT [Id] FROM [Publishers] WHERE [AddressId] = 5)
  106.  
  107. DELETE FROM [Publishers]
  108. WHERE [AddressId] IN (SELECT [Id] FROM [Addresses] WHERE [Town] LIKE 'L%')
  109.  
  110. DELETE FROM [Addresses]
  111. WHERE [Town] LIKE 'L%'
  112.  
  113. GO
  114.  
  115. -- Problem 05. Boardgames by Year of Publication
  116.  
  117.   SELECT [Name], [Rating]
  118.     FROM [Boardgames]
  119. ORDER BY [YearPublished] ASC, [Name] DESC
  120.  
  121. GO
  122.  
  123. -- Problem 06. Boardgames by Category
  124.  
  125.   SELECT [b].[Id],
  126.          [b].[Name],
  127.          [b].[YearPublished] AS 'YearPublished',
  128.          [c].[Name] AS 'CategoryName'
  129.     FROM [Boardgames] AS [b]
  130.     JOIN [Categories] AS [c] ON [b].[CategoryId] = [c].[Id]
  131.    WHERE [c].[Name] LIKE 'Strategy Games' OR
  132.          [c].[Name] LIKE 'Wargames'
  133. ORDER BY YearPublished DESC
  134.  
  135. GO
  136.  
  137. -- Problem 07. Creators without Boardgames
  138.  
  139.    SELECT [c].[Id],
  140.           [c].[FirstName] + ' ' + [c].[LastName] AS 'CreatorName',
  141.           [c].[Email]
  142.      FROM [Creators] AS [c]
  143. LEFT JOIN [CreatorsBoardgames] AS [cb] ON [c].[Id] = [cb].[CreatorId]
  144.     WHERE [cb].[BoardgameId] IS NULL
  145.  ORDER BY [CreatorName] ASC
  146.  
  147. GO
  148.  
  149. -- Problem 08. First 5 Boardgames
  150.  
  151. SELECT TOP(5)   [b].[Name],
  152.                 [b].[Rating],
  153.                 [c].[Name] AS 'CategoryName'
  154.       FROM [Boardgames] AS [b]
  155.       JOIN [PlayersRanges] AS [pr] ON [b].[PlayersRangeId] = [pr].[Id]
  156.       JOIN [Categories] AS [c] ON [b].[CategoryId] = [c].[Id]
  157.      WHERE ([Rating] > 7.00 AND [b].[Name] LIKE '%a%') OR
  158.            ([Rating] > 7.50 AND [pr].[PlayersMin] = 2 AND [pr].[PlayersMax] = 5)
  159.   ORDER BY [b].Name ASC, [b].[Rating] DESC
  160.  
  161. GO
  162.  
  163. -- Problem 09. Creators with Emails
  164.  
  165.   SELECT [c].[FirstName] + ' ' + [c].[LastName] AS 'FullName',
  166.          [c].[Email],
  167.          MAX([b].[Rating])
  168.     FROM [Creators] AS [c]
  169.     JOIN [CreatorsBoardgames] AS [cb] ON [cb].[CreatorId] = [c].[Id]
  170.     JOIN [Boardgames] AS [b] ON [cb].[BoardgameId] = [b].[Id]
  171.    WHERE [c].[Email] LIKE '%.com'
  172. GROUP BY [c].[FirstName], [c].[LastName], [c].[Email]
  173. ORDER BY [FullName] ASC
  174.  
  175. GO
  176.  
  177. -- Problem 10. Creators by Rating
  178.  
  179.    SELECT [c].[LastName],
  180.           CEILING(AVG([b].[Rating])),
  181.           [p].[Name]
  182.      FROM [Creators] AS [c]
  183. LEFT JOIN [CreatorsBoardgames] AS [cb] ON [c].[Id] = [cb].[CreatorId]
  184. LEFT JOIN [Boardgames] [b] ON [b].[Id] = [cb].[BoardgameId]
  185. LEFT JOIN [Publishers] [p] ON [p].[Id] = [b].[PublisherId]
  186.     WHERE [cb].[BoardgameId] IS NOT NULL AND [p].[Name] LIKE 'Stonemaier Games'
  187.  GROUP BY [c].[LastName], [p].[Name]
  188.  ORDER BY AVG([b].[Rating]) DESC
  189.  
  190. GO
  191.  
  192. -- Problem 11. Creator with Boardgames
  193.  
  194. CREATE FUNCTION [udf_CreatorWithBoardgames](@name NVARCHAR(30))
  195. RETURNS INT
  196. AS
  197. BEGIN
  198.         DECLARE @creatorId INT
  199.  
  200.         SET @creatorId = (SELECT [Id]
  201.                             FROM [Creators]
  202.                            WHERE [FirstName] = @name)
  203.  
  204.         DECLARE @result INT
  205.  
  206.         SET @result = (SELECT COUNT([cb].[CreatorId])
  207.                          FROM [Creators] AS [c]
  208.                          JOIN [CreatorsBoardgames] AS [cb] ON [c].[Id] = [cb].[CreatorId]
  209.                          JOIN [Boardgames] AS [b] ON [b].[Id] = [cb].[BoardgameId]
  210.                         WHERE [c].[Id] = @creatorId
  211.                      GROUP BY [cb].[CreatorId])
  212.  
  213.         IF (@result IS NULL)
  214.         BEGIN
  215.             SET @result = 0
  216.         END
  217.  
  218.         RETURN @result
  219. END
  220.  
  221. GO
  222.  
  223. SELECT [dbo.udf_CreatorWithBoardgames]('BRUNO')
  224.  
  225. GO
  226.  
  227. -- Problem 12. Search for Boardgame with Specific Category
  228.  
  229. CREATE PROCEDURE [usp_SearchByCategory](@category VARCHAR(50))
  230. AS
  231. BEGIN
  232.           SELECT [b].[Name],
  233.                  [b].[YearPublished],
  234.                  [b].[Rating],
  235.                  [c].[Name] AS 'CategoryName',
  236.                  [p].[Name] AS 'PublisherName',
  237.                  CONCAT([pr].[PlayersMin], ' people') AS 'MinPlayers',
  238.                  CONCAT([pr].[PlayersMax], ' people') AS 'MaxPlayers'
  239.             FROM [Categories] AS [c]
  240.             JOIN [Boardgames] AS [b] ON [b].[CategoryId] = [c].[Id]
  241.             JOIN [PlayersRanges] AS [pr] ON [b].[PlayersRangeId] = [pr].[Id]
  242.             JOIN [Publishers] AS [p] ON [b].[PublisherId] = [p].[Id]
  243.            WHERE [c].[Name] = @category
  244.         ORDER BY [p].[Name] ASC, [b].[YearPublished] DESC
  245. END
  246.  
  247. GO
  248.  
  249. EXEC [usp_SearchByCategory] 'Wargames'
  250.  
  251. GO
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement