-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathIntroToSQL.sql
More file actions
137 lines (113 loc) · 5.1 KB
/
Copy pathIntroToSQL.sql
File metadata and controls
137 lines (113 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
--4. Write a SQL query to find all information about all departments (use "TelerikAcademy" database).
SELECT d.Name, e.FirstName + ' ' + e.LastName as Manager
FROM Departments d, Employees e
WHERE d.ManagerID = e.EmployeeID
--5. Write a SQL query to find all department names.
SELECT Name
FROM Departments
--6. Write a SQL query to find the salary of each employee.
SELECT FirstName + ' ' + LastName AS FullName, Salary
FROM Employees
--7. Write a SQL to find the full name of each employee.
SELECT FirstName + ' ' + LastName AS FullName
FROM Employees
--8. Write a SQL query to find the email addresses of each employee (by his first and last name).
--Consider that the mail domain is telerik.com. Emails should look like "John.Doe@telerik.com".
--The produced column should be named "Full Email Addresses".
SELECT FirstName + '.' + LastName + '@telerik.com' AS [Full Email Addresses]
FROM Employees
--9. Write a SQL query to find all different employee salaries.
SELECT DISTINCT Salary
FROM Employees
ORDER BY Salary DESC
--10. Write a SQL query to find all information about the employees whose job title is "Sales Representative"
SELECT e1.FirstName + ' ' + e1.LastName AS FullName, e1.JobTitle, d.Name,
e2.FirstName + ' ' + e2.LastName AS Manager,
e1.HireDate, e1.Salary,
a.AddressText
FROM Employees e1, Employees e2, Addresses a, Departments d
WHERE e1.JobTitle = 'Sales Representative'
AND a.AddressID = e1.AddressID
AND d.DepartmentID = e1.DepartmentID
AND e1.ManagerID = e2.EmployeeID
--11. Write a SQL query to find the names of all employees whose first name starts with "SA".
SELECT FirstName + ' ' + LastName AS FullName
FROM Employees
WHERE FirstName LIKE 'SA%'
--12. Write a SQL query to find the names of all employees whose last name contains "ei".
SELECT FirstName + ' ' + LastName AS FullName
FROM Employees
WHERE LastName LIKE '%ei%'
--13. Write a SQL query to find the salary of all employees whose salary is in the range [20000…30000].
SELECT FirstName + ' ' + LastName AS FullName, Salary
FROM Employees
WHERE Salary BETWEEN 20000 AND 30000
ORDER BY Salary DESC
--14. Write a SQL query to find the names of all employees whose salary is 25000, 14000, 12500 or 23600.
SELECT FirstName + ' ' + LastName AS FullName, Salary
FROM Employees
WHERE Salary IN (25000, 14000, 12500, 23600)
ORDER BY Salary DESC
--15. Write a SQL query to find all employees that do not have manager.
SELECT FirstName + ' ' + LastName AS FullName
FROM Employees
WHERE ManagerID IS NULL
--16. Write a SQL query to find all employees that have salary more than 50000. Order them in decreasing order by salary.
SELECT FirstName + ' ' + LastName AS FullName, Salary
FROM Employees
WHERE Salary > 50000
ORDER BY Salary DESC
--17. Write a SQL query to find the top 5 best paid employees.
SELECT TOP 5 FirstName + ' ' + LastName AS FullName, Salary
FROM Employees
ORDER BY Salary DESC
--18. Write a SQL query to find all employees along with their address. Use inner join with ON clause.
SELECT e.FirstName + ' ' + e.LastName AS FullName, a.AddressText
FROM Employees e
INNER JOIN Addresses a
ON e.AddressID = a.AddressID
--19. Write a SQL query to find all employees and their address. Use equijoins (conditions in the WHERE clause).
SELECT e.FirstName + ' ' + e.LastName AS FullName, a.AddressText
FROM Employees e, Addresses a
WHERE e.AddressID = a.AddressID
--20. Write a SQL query to find all employees along with their manager.
SELECT e1.FirstName + ' ' + e1.LastName AS Employee,
e2.FirstName + ' ' + e2.LastName AS Manager
FROM Employees e1, Employees e2
WHERE e1.ManagerID = e2.EmployeeID
--21. Write a SQL query to find all employees, along with their manager and their address.
--Join the 3 tables: Employees e, Employees m and Addresses a.
SELECT e.FirstName + ' ' + e.LastName AS Employee,
m.FirstName + ' ' + m.LastName AS Manager,
a.AddressText
FROM Employees e, Employees m, Addresses a
WHERE e.ManagerID = m.EmployeeID
AND e.AddressID = a.AddressID
--22. Write a SQL query to find all departments and all town names as a single list. Use UNION
SELECT Name AS [Town/Department]
FROM Departments
UNION
SELECT Name AS [Town/Department]
FROM Towns
--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.
--Use right outer join. Rewrite the query to use left outer join.
--left outer join
SELECT ISNULL(e.FirstName + ' ' + e.LastName, 'not responsible enought') AS Employee,
m.FirstName + ' ' + m.LastName AS Manager
FROM Employees e
RIGHT JOIN Employees m
ON e.ManagerID = m.EmployeeID
--right outer join
SELECT e.FirstName + ' ' + e.LastName AS Employee,
ISNULL(m.FirstName + ' ' + m.LastName, 'Unmanageable') AS Manager
FROM Employees e
LEFT JOIN Employees m
ON e.ManagerID = m.EmployeeID
--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.
SELECT e.FirstName + ' ' + e.LastName AS Employee, e.HireDate,
d.Name
FROM Employees e, Departments d
WHERE e.DepartmentID = d.DepartmentID AND d.Name IN ('Sales', 'Finance')
AND e.HireDate BETWEEN '1995-01-01 00:00:00' AND '2005-12-31 00:00:00'