Advertisement
reathh

SQL Introduction

Feb 15th, 2015
377
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 3.80 KB | None | 0 0
  1. --4. Write a SQL query to find all information about all departments (use "SoftUni" database).
  2. SELECT * FROM Departments
  3.  
  4. --5. Write a SQL query to find all department names.
  5. SELECT Name FROM Departments
  6.  
  7. --6. Write a SQL query to find the salary of each employee.
  8. SELECT Salary FROM Employees
  9.  
  10. --7. Write a SQL to find the full name of each employee.
  11. SELECT FirstName + ' ' + LastName AS [FULL Name] FROM Employees
  12.  
  13. --8. Write a SQL query to find the email addresses of each employee.
  14. SELECT FirstName + '.' + LastName + '@softuni.bg' AS [FULL Email Adresses] FROM Employees
  15.  
  16. --9. Write a SQL query to find all different employee salaries.
  17. SELECT DISTINCT Salary FROM Employees
  18.  
  19. --10. Write a SQL query to find all information about the employees whose job title is “Sales Representative“.
  20. SELECT * FROM Employees
  21. WHERE JobTitle = 'Sales Representative'
  22.    
  23. --11. Write a SQL query to find the names of all employees whose first name starts with "SA".
  24. SELECT FirstName FROM Employees
  25. WHERE FirstName LIKE 'sa%'
  26.  
  27. --12. Write a SQL query to find the names of all employees whose last name contains "ei".
  28. SELECT FirstName, LastName FROM Employees
  29. WHERE LastName LIKE '%ei%'
  30.  
  31. --13. Write a SQL query to find the salary of all employees whose salary is in the range [20000…30000].
  32. SELECT Salary FROM Employees
  33. WHERE Salary BETWEEN 20000 AND 30000
  34.  
  35. --14. Write a SQL query to find the names of all employees whose salary is 25000, 14000, 12500 or 23600.
  36. SELECT FirstName, LastName, Salary FROM Employees
  37. WHERE Salary IN(25000, 14000, 12500, 23600)
  38.  
  39. --15. Write a SQL query to find all employees that do not have manager.
  40. SELECT * FROM Employees
  41. WHERE ManagerID IS NULL
  42.  
  43. --16. Write a SQL query to find all employees that have salary more than 50000.
  44. --Order them in decreasing order by salary.
  45. SELECT * FROM Employees
  46. WHERE Salary > 50000
  47. ORDER BY Salary DESC
  48.  
  49. --17. Write a SQL query to find the top 5 best paid employees.
  50. SELECT TOP 5 * FROM EMPLOYEES
  51. ORDER BY Salary DESC
  52.  
  53. --18. Write a SQL query to find all employees along with their address.
  54. SELECT FirstName, LastName, a.AddressText AS [Address], t.Name FROM Employees e
  55.     JOIN Addresses a ON e.AddressID = a.AddressID
  56.     JOIN Towns t ON a.TownID = t.TownID
  57.  
  58. --19. Write a SQL query to find all employees and their address.
  59. SELECT FirstName, LastName, a.AddressText, t.Name
  60. FROM Employees e, Addresses a, Towns t
  61. WHERE e.AddressID = a.AddressID AND a.TownID = t.TownID
  62.  
  63. --20. Write a SQL query to find all employees along with their manager.
  64. SELECT e.FirstName, e.LastName,
  65.     m.FirstName + ' ' + m.LastName AS Manager
  66. FROM Employees e
  67.     JOIN Employees m ON e.ManagerID = m.EmployeeID
  68.  
  69. --21. Write a SQL query to find all employees, along with their manager and their address.
  70. SELECT e.FirstName, e.LastName,
  71.     m.FirstName + ' ' + m.LastName AS Manager,
  72.     a.AddressText AS [Address],
  73.     t.Name AS Town
  74. FROM Employees e
  75.     JOIN Employees m ON e.ManagerID = m.EmployeeID
  76.     JOIN Addresses a ON e.AddressID = a.AddressID
  77.     JOIN Towns t ON a.TownID = t.TownID
  78.  
  79. --22. Write a SQL query to find all departments and all town names as a single list.
  80. SELECT Name FROM Departments
  81. UNION
  82. SELECT Name FROM Towns
  83.  
  84. --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.
  85. SELECT e.FirstName, e.LastName,
  86.     m.FirstName + ' ' + m.LastName AS Manager
  87. FROM Employees e
  88.     LEFT OUTER JOIN Employees m ON e.ManagerID = m.EmployeeID
  89.  
  90. --24. Write a SQL query to find the names of all employees from the departments "Sales" and "Finance"
  91. --whose hire year is between 1995 and 2005.
  92. SELECT FirstName, LastName, d.Name, HireDate FROM Employees e
  93.     JOIN Departments d ON e.DepartmentID = d.DepartmentID
  94. WHERE d.Name = 'Sales' OR d.Name = 'Finance' AND
  95. e.HireDate BETWEEN '1995/01/01' AND '2005/12/31'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement