Advertisement
vlad0

03.SQL-Intro-AllTasks

Jul 10th, 2013
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 5.12 KB | None | 0 0
  1. -- 04. Write a SQL query to find all information about all departments
  2. -- (use "TelerikAcademy" database).
  3. SELECT *
  4. FROM Departments
  5.  
  6. -- 05. Write a SQL query to find all department names.
  7. SELECT Name
  8. FROM Departments
  9.  
  10. -- 06. Write a SQL query to find the salary of each employee.
  11. SELECT FirstName + ' ' + LastName AS FullName, Salary
  12. FROM Employees
  13.  
  14. -- 07. Write a SQL to find the full name of each employee
  15. SELECT FirstName +' ' + ISNULL(MiddleName+' ', '') + LastName AS FullName
  16. FROM Employees e
  17.  
  18. -- 08. Write a SQL query to find the email addresses of each employee
  19. -- (by his first and last name). Consider that the mail domain is telerik.com.
  20. -- Emails should look like “John.Doe@telerik.com". The produced column should be
  21. -- named "Full Email Addresses".
  22.  
  23. SELECT FirstName + '.' + LastName+'@telerik.com' AS FullEmailAddress
  24. FROM Employees
  25.  
  26. -- 09. not sure what is the task excatly Write a SQL query to find all different employee salaries.
  27. SELECT DISTINCT Salary
  28. FROM Employees
  29.  
  30. -- 09. not sure what is the task exactly
  31. SELECT FirstName + ' ' + LastName AS FullName, Salary
  32. FROM Employees
  33.  
  34. -- 10. Write a SQL query to find all information about the employees
  35. -- whose job title is “Sales Representative“.
  36. SELECT *
  37. FROM Employees
  38. WHERE JobTitle = 'Sales Representative'
  39.  
  40. -- 11. Write a SQL query to find the names of all employees
  41. -- whose first name starts with "SA".
  42. SELECT FirstName, LastName
  43. FROM Employees
  44. WHERE FirstName LIKE 'SA%'
  45.  
  46. -- 12. Write a SQL query to find the names of all employees
  47. -- whose last name contains "ei".
  48. SELECT FirstName, LastName
  49. FROM Employees
  50. WHERE LastName LIKE '%ei%'
  51.  
  52. -- 13. Write a SQL query to find the salary of all employees
  53. -- whose salary is in the range [20000…30000].
  54.  
  55. -- option 1.
  56. SELECT FirstName, LastName, Salary
  57. FROM Employees
  58. WHERE Salary BETWEEN 20000 AND 30000
  59.  
  60. -- option 2.
  61. SELECT FirstName, LastName, Salary
  62. FROM Employees
  63. WHERE Salary >= 20000 AND Salary <= 30000
  64.  
  65. -- 14. Write a SQL query to find the names of all employees
  66. -- whose salary is 25000, 14000, 12500 or 23600.
  67.  
  68. -- option 1.
  69. SELECT FirstName, LastName, Salary
  70. FROM Employees
  71. WHERE Salary IN (25000, 14000, 12500, 23600)
  72.  
  73. -- option 2.
  74. SELECT FirstName, LastName, Salary
  75. FROM Employees
  76. WHERE Salary = 25000 OR Salary = 14000 OR Salary = 12500 OR Salary = 23600
  77.  
  78. -- 15. Write a SQL query to find all employees that do not have manager.
  79. SELECT FirstName, LastName, ManagerID
  80. FROM Employees
  81. WHERE ManagerID IS NULL
  82.  
  83. -- 16. Write a SQL query to find all employees that have
  84. -- salary more than 50000. Order them in decreasing order by salary.
  85. SELECT FirstName, LastName, Salary
  86. FROM Employees
  87. WHERE Salary >= 50000
  88. ORDER BY Salary DESC
  89.  
  90. -- 17. Write a SQL query to find the top 5 best paid employees.
  91. SELECT TOP 5 FirstName, LastName, Salary
  92. FROM Employees
  93. ORDER BY Salary DESC
  94.  
  95. -- 18. Write a SQL query to find all employees along with their address.
  96. -- Use inner join with ON clause.
  97. SELECT e.FirstName+' '+e.LastName AS FullName, t.Name +', '+a.AddressText AS FullAddress
  98. FROM Employees e
  99.     JOIN Addresses a
  100.     ON e.AddressID = a.AddressID
  101.     JOIN Towns t
  102.     ON a.TownID = t.TownID
  103.  
  104. -- 19. Write a SQL query to find all employees and their address.
  105. -- Use equijoins (conditions in the WHERE clause).
  106. SELECT e.FirstName+' '+e.LastName AS FullName, t.Name +', '+a.AddressText AS FullAddress
  107. FROM Employees e, Addresses a, Towns t
  108. WHERE e.AddressID = a.AddressID AND a.TownID = t.TownID
  109.  
  110. -- 20. Write a SQL query to find all employees along with their manager.
  111. SELECT e.FirstName, e.LastName, m.FirstName+ ' ' + m.LastName AS ManagerName
  112. FROM Employees e
  113. JOIN Employees m
  114. ON e.ManagerID = m.EmployeeID;
  115.  
  116. -- 21. Write a SQL query to find all employees, along with their manager and
  117. -- their address. Join the 3 tables: Employees e, Employees m and Addresses a.
  118. SELECT e.FirstName, e.LastName, a.AddressText, m.FirstName+' '+m.LastName AS ManagerFullName
  119. FROM Employees e
  120.     JOIN Employees m
  121.     ON e.ManagerID = m.EmployeeID
  122.     JOIN Addresses a
  123.     ON e.AddressID = a.AddressID
  124. ORDER BY m.FirstName+' '+m.LastName
  125.  
  126. -- 22. Write a SQL query to find all departments and all town names as
  127. -- a single list. Use UNION.
  128. SELECT Name
  129. FROM Departments
  130. UNION
  131. SELECT Name
  132. FROM Towns
  133.  
  134. -- 23. Write a SQL query to find all the employees and the manager for each of
  135. -- them along with the employees that do not have manager. Use right outer join.
  136. -- Rewrite the query to use left outer join.
  137. SELECT e.FirstName, e.LastName,  m.FirstName+' '+m.LastName AS ManagerFullName
  138. FROM Employees m
  139.     RIGHT JOIN Employees e
  140.     ON e.ManagerID = m.EmployeeID
  141.  
  142. SELECT e.FirstName, e.LastName,  m.FirstName+' '+m.LastName AS ManagerFullName
  143. FROM Employees e
  144.     LEFT JOIN Employees m
  145.     ON e.ManagerID = m.EmployeeID
  146.  
  147. -- 24. Write a SQL query to find the names of all employees from the departments "Sales"
  148. -- and "Finance" whose hire year is between 1995 and 2005
  149. SELECT e.FirstName, e.LastName, d.Name, e.HireDate
  150. FROM Employees e
  151.     JOIN Departments d
  152.     ON e.DepartmentID = d.DepartmentID
  153. WHERE (d.Name = 'Sales' OR d.Name = 'Finance')
  154.     AND (YEAR(e.HireDate) >= 1995 AND YEAR(e.HireDate) < 2005)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement