Advertisement
Guest User

Untitled

a guest
Feb 12th, 2015
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.52 KB | None | 0 0
  1. USE SoftUni
  2. GO
  3.  
  4. --Problem 4.    Write a SQL query to find all information about all departments
  5. SELECT * FROM Departments
  6.  
  7. --Problem 5.    Write a SQL query to find all department names.
  8. SELECT Name FROM Departments
  9.  
  10. --Problem 6.    Write a SQL query to find the salary of each employee.
  11. SELECT FirstName + ' ' + LastName AS FullName, Salary FROM Employees
  12.  
  13. --Problem 7.    Write a SQL to find the full name of each employee.
  14. SELECT
  15. FirstName, LastName,
  16. FirstName + ' ' + LastName AS FullName
  17. FROM Employees
  18.  
  19. --Problem 8.    Write a SQL query to find the email addresses of each employee.
  20. SELECT
  21. FirstName,
  22. LastName,
  23. FirstName + '.' + LastName + '@softuni.bg' AS [FULL Email Address]
  24. FROM Employees
  25.  
  26. --Problem 9.    Write a SQL query to find all different employee salaries.
  27. SELECT DISTINCT Salary FROM Employees
  28.  
  29. --Problem 10.   Write a SQL query to find all information about the employees whose job title is “Sales Representative“.
  30. SELECT *
  31. FROM Employees
  32. WHERE JobTitle = 'Sales Representative'
  33.  
  34. --Problem 11.   Write a SQL query to find the names of all employees whose first name starts with "SA".
  35. SELECT FirstName, LastName
  36. FROM Employees
  37. WHERE FirstName LIKE 'SA%'
  38.  
  39. --Problem 12.   Write a SQL query to find the names of all employees whose last name contains "ei".
  40. SELECT FirstName, LastName
  41. FROM Employees
  42. WHERE LastName LIKE '%ei%'
  43.  
  44. --Problem 13.   Write a SQL query to find the salary of all employees whose salary is in the range [20000…30000].
  45. SELECT FirstName, LastName, Salary
  46. FROM Employees
  47. WHERE Salary BETWEEN 20000 AND 30000
  48.  
  49. --Problem 14.   Write a SQL query to find the names of all employees whose salary is 25000, 14000, 12500 or 23600.
  50. SELECT FirstName, LastName, Salary
  51. FROM Employees
  52. WHERE Salary IN (25000, 14000, 12500, 23600)
  53.  
  54. --Problem 15.   Write a SQL query to find all employees that do not have manager.
  55. SELECT FirstName, LastName
  56. FROM Employees
  57. WHERE ManagerID IS NULL
  58.  
  59. --Problem 16.   Write a SQL query to find all employees that have salary more than 50000. Order them in decreasing order by salary.
  60. SELECT FirstName, LastName, Salary
  61. FROM Employees
  62. WHERE Salary > 50000
  63. ORDER BY Salary DESC
  64.  
  65. --Problem 17.   Write a SQL query to find the top 5 best paid employees.
  66. SELECT TOP 5 FirstName, LastName, Salary
  67. FROM Employees
  68. ORDER BY Salary DESC
  69.  
  70. --Problem 18.   Write a SQL query to find all employees along with their address.
  71. SELECT e.FirstName, e.LastName, a.AddressText
  72. FROM Employees AS e
  73.     INNER JOIN Addresses AS a
  74.         ON a.AddressID = e.AddressID
  75.        
  76. --Problem 19.   Write a SQL query to find all employees and their address.
  77. SELECT e.FirstName, e.LastName, a.AddressText
  78. FROM Employees AS e, Addresses AS a
  79. WHERE a.AddressID = e.AddressID
  80.  
  81. --Problem 20.   Write a SQL query to find all employees along with their manager.
  82. SELECT
  83. e.FirstName, e.LastName,
  84. m.FirstName + ' ' + m.LastName AS ManagerName
  85. FROM Employees AS e
  86.     INNER JOIN Employees AS m
  87.         ON e.ManagerID = m.EmployeeID
  88.  
  89. --Problem 21.   Write a SQL query to find all employees, along with their manager and their address.
  90. SELECT
  91. e.FirstName, e.LastName,
  92. m.FirstName + ' ' + m.LastName AS ManagerName,
  93. a.AddressText AS ManagerAddress,
  94. ad.AddressText AS EmployeeAddress
  95. FROM Employees AS e
  96.     INNER JOIN Employees AS m
  97.         ON e.ManagerID = m.EmployeeID
  98.     INNER JOIN Addresses AS a
  99.         ON a.AddressID = m.AddressID
  100.     INNER JOIN Addresses AS ad
  101.         ON ad.AddressID = e.AddressID
  102.        
  103. --Problem 22.   Write a SQL query to find all departments and all town names as a single list.
  104. SELECT Name FROM Departments
  105. UNION
  106. SELECT Name FROM Towns
  107.  
  108. --Problem 23.   Write a SQL query to find all the employees and the manager for each of them along with the employees that do not have manager.
  109. --Variant with LEFT OUTER JOIN
  110. SELECT
  111. e.FirstName + ' ' + e.LastName AS EmployeeName,
  112. m.FirstName + ' ' + m.LastName AS ManagerName
  113. FROM Employees e
  114.     LEFT OUTER JOIN Employees AS m
  115.         ON e.ManagerID = m.EmployeeID
  116. --Variant with RIGHT OUTER JOIN
  117. SELECT
  118. e.FirstName + ' ' + e.LastName AS ManagerName,
  119. m.FirstName + ' ' + m.LastName AS EmployeeName
  120. FROM Employees e
  121.     RIGHT OUTER JOIN Employees AS m
  122.         ON m.ManagerID = e.EmployeeID
  123.  
  124. --Problem 24.   Write a SQL query to find the names of all employees from the departments "Sales" and "Finance" whose hire year is between 1995 and 2005.
  125. SELECT
  126. e.FirstName + ' ' + e.LastName AS EmployeeName,
  127. d.Name AS DepartmentName,
  128. e.HireDate
  129. FROM Employees e
  130.     INNER JOIN Departments AS d
  131.         ON e.DepartmentID = d.DepartmentID
  132.         WHERE e.HireDate BETWEEN '1995-01-01' AND '2005-01-01'
  133.         AND d.Name IN ('Sales', 'Finance')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement