Advertisement
DimovIvan

MS SQL - Subqueries and Joins

Jun 7th, 2022 (edited)
1,366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
T-SQL 6.61 KB | None | 0 0
  1.                --Exercises: Subqueries and Joins
  2. --1. Employee Address
  3.  
  4. SELECT TOP(5)
  5.          e.EmployeeID
  6.          ,e.JobTitle
  7.          ,e.AddressID
  8.          ,a.AddressText
  9.     FROM Employees AS e
  10.     JOIN Addresses AS a
  11.       ON e.AddressID = a.AddressID
  12. ORDER BY e.AddressID
  13.  
  14. --02. Addresses with Towns
  15. SELECT TOP(50)
  16.               e.FirstName
  17.               ,e.LastName
  18.               ,t.[Name] AS Town
  19.               ,a.AddressText
  20.          FROM Employees AS e
  21.     LEFT JOIN Addresses AS a
  22.            ON e.AddressID = a.AddressID
  23.     LEFT JOIN Towns AS t
  24.            ON a.TownID = t.TownID
  25.      ORDER BY e.FirstName, e.LastName
  26.  
  27. --3. Sales Employee
  28.  
  29.   SELECT
  30.          e.EmployeeID
  31.          ,e.FirstName
  32.          ,e.LastName
  33.          ,d.[Name] AS DepartmentName
  34.     FROM Employees AS e
  35.     JOIN Departments AS d
  36.       ON e.DepartmentID = d.DepartmentID
  37.    WHERE d.[Name] = 'Sales'
  38. ORDER BY e.EmployeeID
  39.  
  40. --4. Employee Departments
  41.  
  42. SELECT TOP(5)
  43.          e.EmployeeID
  44.          ,e.FirstName
  45.          ,e.Salary
  46.          ,d.[Name] AS DepartmentName
  47.     FROM Employees AS e
  48.     JOIN Departments AS d
  49.       ON e.DepartmentID = d.DepartmentID
  50.    WHERE e.Salary > 15000
  51. ORDER BY d.DepartmentID
  52.  
  53. --5. Employees Without Project
  54.  
  55. SELECT TOP(3)
  56.           e.EmployeeID
  57.           ,e.FirstName
  58.           --,ep.ProjectID
  59.      FROM Employees AS e
  60. LEFT JOIN EmployeesProjects AS ep
  61.        ON e.EmployeeID = ep.EmployeeID
  62.     WHERE ep.ProjectID IS NULL
  63.  ORDER BY e.EmployeeID
  64.  
  65. --6. Employees Hired After
  66.  
  67.   SELECT  
  68.          e.FirstName
  69.          ,e.LastName
  70.          ,e.HireDate
  71.          ,d.[Name] AS DeptName
  72.     FROM Employees AS e
  73.     JOIN Departments AS d
  74.       ON e.DepartmentID = d.DepartmentID
  75.      AND e.HireDate > '1.1.1999'
  76.    WHERE d.[Name] IN ('Sales', 'Finance')
  77. ORDER BY e.HireDate
  78.  
  79. --07. Employees With Project
  80.  
  81. SELECT TOP(5)
  82.              e.EmployeeID
  83.              ,e.FirstName
  84.              ,p.[Name] AS ProjectName
  85.         FROM Employees AS e
  86.         JOIN EmployeesProjects AS ep
  87.           ON e.EmployeeID = ep.EmployeeID
  88.         JOIN Projects AS p
  89.           ON ep.ProjectID = p.ProjectID
  90.        WHERE p.StartDate > '2002-08-13'
  91.          AND p.EndDate IS NULL
  92.     ORDER BY e.EmployeeID
  93.  
  94. --08. Employee 24
  95.  
  96. SELECT
  97.        e.EmployeeID
  98.        ,e.FirstName
  99.        ,CASE
  100.             WHEN DATEPART(YEAR, p.StartDate) >= 2005 THEN NULL
  101.             ELSE p.[Name]
  102.        END AS ProjectName
  103.   FROM Employees AS e
  104.   JOIN EmployeesProjects AS ep
  105.     ON e.EmployeeID = ep.EmployeeID
  106.  JOIN Projects AS p
  107.    ON ep.ProjectID = p.ProjectID
  108. WHERE e.EmployeeID = 24
  109.  
  110. --09. Employee Manager
  111.  
  112. SELECT
  113.     e.EmployeeID
  114.     ,e.FirstName
  115.     ,e.ManagerID
  116.     ,m.FirstName AS ManagerName
  117. FROM Employees AS e
  118. JOIN Employees AS m
  119.   ON e.ManagerID = m.EmployeeID
  120. WHERE e.ManagerID IN (3, 7)
  121. ORDER BY e.EmployeeID
  122.  
  123. --10. Employees Summary
  124. SELECT TOP(50)
  125.     e.EmployeeID
  126.     ,CONCAT(e.FirstName, ' ', e.LastName) AS EmployeeName
  127.     ,CONCAT(m.FirstName, ' ', m.LastName) AS ManagerName
  128.     ,d.[Name] AS DepartmentName
  129. FROM Employees AS e
  130. JOIN Employees AS m
  131.   ON e.ManagerID = m.EmployeeID
  132. JOIN Departments AS d
  133.   ON e.DepartmentID = d.DepartmentID
  134. ORDER BY e.EmployeeID
  135.  
  136. --11. Min Average Salary
  137.  
  138. SELECT TOP(1)
  139.      AVG(Salary) AS MinAverageSalary
  140.     FROM Employees
  141. GROUP BY DepartmentID
  142. ORDER BY MinAverageSalary
  143.  
  144. --12. Highest Peaks in Bulgaria
  145. USE Geography
  146.  
  147. SELECT
  148.      c.CountryCode
  149.      ,m.MountainRange
  150.      ,p.PeakName
  151.      ,p.Elevation
  152. FROM Countries AS c
  153. JOIN MountainsCountries AS mc
  154.   ON c.CountryCode = mc.CountryCode
  155. JOIN Mountains AS m
  156.   ON mc.MountainId = m.Id
  157. JOIN Peaks AS p
  158.   ON m.Id = p.MountainId
  159. WHERE c.CountryCode = 'BG'
  160.   AND p.Elevation > 2835
  161. ORDER BY p.Elevation DESC
  162.  
  163. --13. Count Mountain Ranges
  164.  
  165. SELECT
  166.      c.CountryCode
  167.      ,COUNT(mC.MountainId) AS MountainRange
  168. FROM Countries AS c
  169. JOIN MountainsCountries AS mc
  170.   ON c.CountryCode = mc.CountryCode
  171. JOIN Mountains AS m
  172.   ON mc.MountainId = m.Id
  173. WHERE c.CountryCode IN ('BG', 'RU', 'US')
  174. GROUP BY c.CountryCode
  175.  
  176. --14. Countries With or Without Rivers
  177.  
  178. SELECT TOP(5)
  179.      c.CountryName
  180.      ,r.RiverName
  181. FROM Countries AS c
  182. LEFT JOIN CountriesRivers AS cr
  183.   ON c.CountryCode = cr.CountryCode
  184. LEFT JOIN Rivers AS r
  185.   ON cr.RiverId = r.Id
  186. WHERE c.ContinentCode = 'AF'
  187. ORDER BY c.CountryName
  188.  
  189. --15. Continents and Currencies
  190. USE Geography
  191.  
  192. SELECT
  193.        ContinentCode
  194.        ,CurrencyCode
  195.        ,CurrencyUsage
  196.   FROM (
  197.        SELECT *
  198.               ,DENSE_RANK() OVER (PARTITION BY ContinentCode order by CurrencyUsage DESC) AS CurrUsRank
  199.          FROM (
  200.                 SELECT
  201.                        c.ContinentCode
  202.                        ,ctr.CurrencyCode
  203.                        ,COUNT(ctr.CurrencyCode) AS CurrencyUsage
  204.                   FROM Continents AS c
  205.              LEFT JOIN Countries AS ctr ON c.ContinentCode = ctr.ContinentCode
  206.               GROUP BY c.ContinentCode, ctr.CurrencyCode
  207.               ) AS CurrUsageSubquery
  208.         WHERE CurrencyUsage > 1
  209.        ) AS RankingSubquery
  210. WHERE CurrUsRank = 1
  211. ORDER BY ContinentCode
  212.  
  213. --16. Countries Without any Mountains
  214.  
  215.    SELECT
  216.           COUNT(*) AS [Count]
  217.      FROM Countries AS c
  218. LEFT JOIN MountainsCountries AS m ON c.CountryCode = m.CountryCode
  219.     WHERE MountainId IS NULL
  220.  
  221. --17. Highest Peak and Longest River by Country
  222.  
  223.  SELECT TOP(5)
  224.               c.CountryName
  225.               ,MAX(p.Elevation) AS HighestPeakElevation
  226.               ,MAX(r.[Length]) AS LongestRiverLength
  227.          FROM Countries AS c
  228.     LEFT JOIN MountainsCountries AS mc ON c.CountryCode = mc.CountryCode
  229.     LEFT JOIN Mountains AS m ON mc.MountainId = m.Id
  230.     LEFT JOIN Peaks AS p ON m.Id = p.MountainId
  231.     LEFT JOIN CountriesRivers AS cr ON c.CountryCode = cr.CountryCode
  232.     LEFT JOIN Rivers AS r ON cr.RiverId = r.Id
  233.      GROUP BY c.CountryName
  234.      ORDER BY HighestPeakElevation DESC,
  235.               LongestRiverLength DESC,
  236.               c.CountryName
  237.  
  238. --18. Highest Peak Name and Elevation by Country
  239.  
  240. SELECT TOP(5)
  241.        Country
  242.        ,[Highest Peak Name]
  243.        ,[Highest Peak Elevation]
  244.        ,Mountain
  245.   FROM (SELECT
  246.               c.CountryName AS Country
  247.               ,CASE
  248.                     WHEN p.PeakName IS NULL THEN '(no highest peak)'
  249.                     ELSE p.PeakName
  250.                 END AS [Highest Peak Name]
  251.               ,CASE
  252.                     WHEN p.Elevation IS NULL THEN '0'
  253.                     ELSE p.Elevation
  254.                 END AS [Highest Peak Elevation]
  255.               ,CASE WHEN m.MountainRange IS NULL THEN '(no mountain)'
  256.                     ELSE m.MountainRange
  257.                 END AS [Mountain]
  258.               ,DENSE_RANK() OVER (PARTITION BY c.CountryName ORDER BY p.Elevation DESC) AS PeaksRanking
  259.               --,MAX(p.Elevation) AS HighestPeakElevation
  260.               --,MAX(r.[Length]) AS LongestRiverLength
  261.          FROM Countries AS c
  262.     LEFT JOIN MountainsCountries AS mc ON c.CountryCode = mc.CountryCode
  263.     LEFT JOIN Mountains AS m ON mc.MountainId = m.Id
  264.     LEFT JOIN Peaks AS p ON m.Id = p.MountainId
  265.         ) AS PeaksRankingSubquery
  266.  WHERE PeaksRanking = 1
  267.  ORDER BY Country, [Highest Peak Name]
  268.  
  269.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement