Dilyana_Dobreva

Untitled

Aug 19th, 2019
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.21 KB | None | 0 0
  1. using Microsoft.EntityFrameworkCore;
  2. using System;
  3. using System.Linq;
  4.  
  5. namespace SQL_Homework_Entity_Framework
  6. {
  7. class Program
  8. {
  9. static void Main(string[] args)
  10. {
  11. var context = new TelerikAcademyContext();
  12.  
  13. // 1.Write a SQL query to find all information about all departments(use "TelerikAcademy" database).
  14. //var departmets = context.Departments.ToList();
  15. //departmets.ForEach(d => Console.WriteLine($"Department: {d.Name}, Manager ID: {d.ManagerId}"));
  16.  
  17. //2.Write a SQL query to find all department names.
  18. //var departments = context.Departments
  19. // .Select(d => d.Name)
  20. // .ToList();
  21.  
  22. //3.Write a SQL query to find the salary of each employee.
  23. //var employees = context.Employees
  24. // .Select(e => new
  25. // {
  26. // e.FirstName,
  27. // e.LastName,
  28. // e.Salary
  29. // });
  30. //foreach (var e in employees)
  31. //{
  32. // Console.WriteLine($"Name: {e.FirstName} {e.LastName} Salary: {e.Salary}");
  33. //}
  34.  
  35. //4.Write a SQL to find the full name of each employee.
  36. //var employees = context.Employees
  37. // .Select(e => new
  38. // {
  39. // e.FirstName,
  40. // e.LastName,
  41. // });
  42. //foreach (var e in employees)
  43. //{
  44. // Console.WriteLine($"Name: {e.FirstName} {e.LastName}");
  45. //}
  46.  
  47. //5.Write a SQL query to find the email addresses of each employee(by his first and last name).
  48. //Consider that the mail domain is telerik.com.Emails should look like “John.Doe @telerik.com".
  49. //The produced column should be named "Full Email Addresses".
  50. //var fullEmailAddress = context.Employees
  51. // .Select(e => new
  52. // {
  53. // e.FirstName,
  54. // e.LastName,
  55. // })
  56. // .Select(e => e.FirstName + "." + e.LastName + "@telerik.com")
  57. // .ToList();
  58.  
  59. //6.Write a SQL query to find all different employee salaries.
  60. //var salaries = context.Employees
  61. // .Select(e => new
  62. // {
  63. // e.Salary
  64. // })
  65. // .Distinct()
  66. // .ToList();
  67.  
  68.  
  69. //7.Write a SQL query to find all information about the employees whose job title is “Sales Representative“.
  70. //var employees = context.Employees
  71. // .Where(e => e.JobTitle == "Sales Representative")
  72. // .ToList();
  73.  
  74. //foreach (var e in employees)
  75. //{
  76. // Console.WriteLine($"{ e.FirstName} {e.LastName}");
  77. //}
  78.  
  79. //8.Write a SQL query to find the names of all employees whose first name starts with "SA".
  80. //var employees = context.Employees
  81. // .Select(e => new
  82. // {
  83. // e.FirstName,
  84. // e.LastName
  85. // })
  86. // .Where(e => e.FirstName.StartsWith("Sa"))
  87. // .ToList();
  88. //foreach(var e in employees)
  89. //{
  90. // Console.WriteLine($"{ e.FirstName} {e.LastName}");
  91. //}
  92.  
  93. //9.Write a SQL query to find the names of all employees whose last name contains "ei".
  94. //var employees = context.Employees
  95. // .Select(e => new
  96. // {
  97. // e.FirstName,
  98. // e.LastName
  99. // })
  100. // .Where(e => e.LastName.ToLower().Contains("ei"))
  101. // .ToList();
  102. //foreach (var e in employees)
  103. //{
  104. // Console.WriteLine($"{ e.FirstName} {e.LastName}");
  105. //}
  106.  
  107. //10.Write a SQL query to find the salary of all employees whose salary is in the range[20000…30000].
  108. //var salaries = context.Employees
  109. // .Select(e => new
  110. // {
  111. // e.Salary,
  112. // e.FirstName,
  113. // e.LastName
  114. // })
  115. // .Where(e => e.Salary >= 20000 && e.Salary <= 30000)
  116. // .ToList();
  117. //foreach (var e in salaries)
  118. //{
  119. // Console.WriteLine($"{ e.FirstName} {e.LastName} - {e.Salary}");
  120. //}
  121.  
  122.  
  123.  
  124. //11.Write a SQL query to find the names of all employees whose salary is 25000, 14000, 12500 or 23600.
  125. //var salaries = context.Employees
  126. // .Select(e => new
  127. // {
  128. // e.Salary,
  129. // e.FirstName,
  130. // e.LastName
  131. // })
  132. // .Where(e => e.Salary == 25000 || e.Salary == 14000 || e.Salary ==12500 || e.Salary ==23600)
  133. // .ToList();
  134. //foreach (var e in salaries)
  135. //{
  136. // Console.WriteLine($"{ e.FirstName} {e.LastName} - {e.Salary}");
  137. //}
  138.  
  139. //12.Write a SQL query to find all employees that do not have manager.
  140. //var employee = context.Employees
  141. // .Select(e => new
  142. // {
  143. // e.FirstName,
  144. // e.LastName,
  145. // e.ManagerId
  146. // })
  147. // .Where(e => e.ManagerId == null)
  148. // .ToList();
  149.  
  150. //foreach (var e in employee)
  151. //{
  152. // Console.WriteLine($"{ e.FirstName} {e.LastName}");
  153. //}
  154.  
  155.  
  156. //13.Write a SQL query to find all employees that have salary more than 50000.
  157. //Order them in decreasing order by salary.
  158. //var salaries = context.Employees
  159. // .Select(e => new
  160. // {
  161. // e.Salary,
  162. // e.FirstName,
  163. // e.LastName
  164. // })
  165. // .Where(e => e.Salary >= 50000)
  166. // .OrderByDescending(e => e.Salary)
  167. // .ToList();
  168. //foreach (var e in salaries)
  169. //{
  170. // Console.WriteLine($"{ e.FirstName} {e.LastName} - {e.Salary}");
  171. //}
  172.  
  173.  
  174. //14.Write a SQL query to find the top 5 best paid employees.
  175. //var salaries = context.Employees
  176. // .Select(e => new
  177. // {
  178. // e.Salary,
  179. // e.FirstName,
  180. // e.LastName
  181. // })
  182. // .OrderByDescending(e =>e.Salary)
  183. // .Take(5)
  184. // .ToList();
  185. //foreach (var e in salaries)
  186. //{
  187. // Console.WriteLine($"{ e.FirstName} {e.LastName} - {e.Salary}");
  188. //}
  189.  
  190.  
  191. //15.Write a SQL query to find all employees along with their address.Use inner join with ON clause.
  192. //var employees = context.Employees
  193. // .Include(e => e.Address)
  194. // .Select(e => new
  195. // {
  196. // e.FirstName,
  197. // e.LastName,
  198. // e.Address.AddressText
  199. // })
  200. // .ToList();
  201. //foreach (var e in employees)
  202. //{
  203. // Console.WriteLine($"{ e.FirstName} {e.LastName} - {e.AddressText}");
  204. //}
  205.  
  206. //16.Write a SQL query to find all employees and their address.
  207. //Use equijoins(conditions in the WHERE clause).
  208.  
  209.  
  210. //17.Write a SQL query to find all employees along with their manager.
  211. //var employees = context.Employees
  212. // .Join(context.Employees,
  213. // p => p.ManagerId,
  214. // e => e.EmployeeId,
  215. // (p, e) => new
  216. // {
  217. // ManFirstName = e.FirstName,
  218. // ManLastName = e.LastName,
  219. // EmpFirstName = p.FirstName,
  220. // EmpLastName = p.LastName
  221. // })
  222. // .ToList();
  223.  
  224. //foreach (var e in employees)
  225. //{
  226. // Console.WriteLine($"Employee: { e.EmpFirstName} {e.EmpLastName} - Manager: {e.ManFirstName} {e.ManLastName}");
  227. //}
  228.  
  229.  
  230. //18.Write a SQL query to find all employees, along with their manager and their address.
  231. //Join the 3 tables: Employees e, Employees m and Addresses a.
  232. //var employees = context.Employees
  233. // .Join(context.Employees,
  234. // e => e.ManagerId,
  235. // p => p.EmployeeId,
  236. // (e, p) => new { e, p })
  237. // .Join(context.Addresses,
  238. // e1 => e1.e.AddressId,
  239. // a => a.AddressId,
  240. // (e1, a) => new { e1, a })
  241. // .Select(empl => new
  242. // {
  243. // EmplFirstName = empl.e1.e.FirstName,
  244. // EmplLastName = empl.e1.e.LastName,
  245. // ManFirstName = empl.e1.p.FirstName,
  246. // ManLastName = empl.e1.p.LastName,
  247. // EmplAddress = empl.a.AddressText
  248. // })
  249. // .ToList();
  250.  
  251. // foreach (var e in employees)
  252. //{
  253. // Console.WriteLine($"Employee: {e.EmplFirstName} {e.EmplLastName}, Manager: {e.ManFirstName} {e.ManLastName}, Address: {e.EmplAddress}");
  254. //}
  255.  
  256.  
  257. //19.Write a SQL query to find all departments and all town names as a single list. Use UNION.
  258.  
  259. //NOTDONE //20.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.
  260. //var employees = context.Employees
  261. // .Join(context.Employees,
  262. // e => e.ManagerId,
  263. // p => p.EmployeeId,
  264. // (e, p) => new
  265. // {
  266. // EmplID = e.EmployeeId,
  267. // ManFirstName = p.FirstName,
  268. // ManLastName = p.LastName,
  269. // EmpFirstName = e.FirstName,
  270. // EmpLastName = e.LastName
  271. // })
  272. // .OrderBy(e => e.EmplID)
  273. // .ToList();
  274.  
  275. //foreach (var e in employees)
  276. //{
  277. // Console.WriteLine($"Employee:{e.EmplID} { e.EmpFirstName} {e.EmpLastName} - Manager: {e.ManFirstName} {e.ManLastName}");
  278. //}
  279.  
  280. //21.Write a SQL query to find the names of all employees from the departments "Sales" and "Finance"
  281. //whose hire year is between 1995 and 2005.
  282. //var employees = context.Employees
  283. // .Join(context.Departments,
  284. // e => e.DepartmentId,
  285. // d => d.DepartmentId,
  286. // (e, d) => new { e, d }
  287. // )
  288. //.Select(empl => new
  289. //{
  290. // EmplFulltName = empl.e.FirstName + empl.e.LastName,
  291. // Department = empl.d.Name
  292. //})
  293. //.Where(e => e.Department == "Sales" || e.Department == "Finance")
  294. //.ToList();
  295.  
  296. //foreach (var e in employees)
  297. //{
  298. // Console.WriteLine($"{e.Department} - {e.EmplFulltName}");
  299. //}
  300.  
  301.  
  302.  
  303. //NOTDONE //22.Write a SQL query to find the names and salaries of the employees that take the minimal salary in the company.
  304. // -Use a nested SELECT statement.
  305.  
  306. // Throws an Exception for Min()
  307.  
  308. //var employees = context.Employees
  309. // .Select(e => new
  310. // {
  311. // FullName = e.FirstName + e.LastName,
  312. // Salary = e.Salary
  313. // })
  314. // .Where(e => e.Salary == (context.Employees.Select(e1 => new { e1.Salary }).Min(e1 => e1.Salary)))
  315. // .ToList();
  316.  
  317.  
  318. //foreach (var e in employees)
  319. //{
  320. // Console.WriteLine($"{e.Salary} - {e.FullName}");
  321. //}
  322.  
  323.  
  324. //23.Write a SQL query to find the names and salaries of the employees that have a salary that is up to 10 % higher than the minimal salary for the company.
  325. //24.Write a SQL query to find the full name, salary and department of the employees that take the minimal salary in their department.
  326. // - Use a nested SELECT statement.
  327. //25.Write a SQL query to find the average salary in the department #1.
  328. //var avgSalary = context.Employees
  329. // .Select(e => new
  330. // {
  331. // e.Salary
  332. // })
  333. // .Average(e => e.Salary);
  334.  
  335. //Console.WriteLine($"Average salary: {avgSalary}");
  336.  
  337. //26.Write a SQL query to find the average salary in the "Sales" department.
  338.  
  339. //var avgSalary = context.Employees
  340. // .Join(context.Departments,
  341. // e => e.DepartmentId,
  342. // d => d.DepartmentId,
  343. // (e, d) => new { e, d })
  344. // .Select(empl => new
  345. // {
  346. // Salary = empl.e.Salary,
  347. // Department = empl.d.Name,
  348. // })
  349. // .Where(d => d.Department == "Sales")
  350. // .Average(e => e.Salary);
  351.  
  352. //Console.WriteLine(avgSalary);
  353.  
  354.  
  355. //27.Write a SQL query to find the number of employees in the "Sales" department.
  356. //var numberEmpl = context.Employees
  357. // .Join(context.Departments,
  358. // e => e.DepartmentId,
  359. // d => d.DepartmentId,
  360. // (e, d) => new { e, d })
  361. // .Select(empl => new
  362. //{
  363. // Salary = empl.e.Salary,
  364. // Department = empl.d.Name,
  365. //})
  366. // .Where(d => d.Department == "Sales")
  367. // .Count();
  368.  
  369. //Console.WriteLine(numberEmpl);
  370.  
  371. //28.Write a SQL query to find the number of all employees that have manager.
  372. //var employeeCount = context.Employees
  373. // .Where(e => e.ManagerId != null)
  374. // .Count();
  375. //Console.WriteLine(employeeCount);
  376.  
  377. //29.Write a SQL query to find the number of all employees that have no manager.
  378. //var employeeCount = context.Employees
  379. // .Where(e => e.ManagerId == null)
  380. // .Count();
  381. //Console.WriteLine(employeeCount);
  382.  
  383. //30.Write a SQL query to find all departments and the average salary for each of them.
  384. //var query = context.People
  385. // .GroupBy(p => p.name)
  386. // .Select(g => new { name = g.Key, count = g.Count() });
  387.  
  388. //var dptAvgSalary = context.Employees
  389. // .Join(context.Departments,
  390. // e => e.DepartmentId,
  391. // d => d.DepartmentId,
  392. // (e, d) => new { e, d })
  393. // .Select(empl => new
  394. // {
  395. // Salary = empl.e.Salary,
  396. // Department = empl.d.Name
  397. // })
  398. // .GroupBy(e => e.Department)
  399. // .Select(e => new { name = e.Key, avg = e.Average(s => s.Salary) })
  400. // .ToList();
  401.  
  402. //foreach (var s in dptAvgSalary)
  403. //{
  404. // Console.WriteLine($"{s.name} - {s.avg}");
  405. //}
  406.  
  407. //NOTDONE //31.Write a SQL query to find the count of all employees in each department and for each town.
  408. //var employeesPerTown = context.Employees
  409. // .Join(context.Departments,
  410. // e => e.DepartmentId,
  411. // p => p.DepartmentId,
  412. // (e, p) => new { e, p })
  413. // .Join(context.Addresses,
  414. // e1 => e1.e.AddressId,
  415. // a => a.AddressId,
  416. // (e1, a) => new { e1, a })
  417. // .Join(context.Towns,
  418. // a1 => a1.a.TownId,
  419. // t => t.TownId,
  420. // (a1, t) => new { a1, t })
  421. // .GroupBy(e => new
  422. // {
  423. // DepartmentName = e.a1.e1.p.Name,
  424. // TownName = e.t.Name
  425. // })
  426. // .Select(e => new
  427. // {
  428. // departmentName = e.Key.DepartmentName,
  429. // townName = e.Key.TownName,
  430. // count = e.Count()
  431. // })
  432. // .ToList();
  433.  
  434. //foreach (var e in employeesPerTown)
  435. //{
  436. // Console.WriteLine($"{e.departmentName} - {e.count}");
  437. //}
  438.  
  439.  
  440. //32.Write a SQL query to find all managers that have exactly 5 employees.Display their first name and last name.
  441. //33.Write a SQL query to find all employees along with their managers.For employees that do not have manager display the value "(no manager)".
  442. //34.Write a SQL query to find the names of all employees whose last name is exactly 5 characters long.Use the built-in LEN(str) function.
  443. //35.Write a SQL query to display the current date and time in the following format "day.month.year hour:minutes:seconds:milliseconds".
  444.  
  445. // - Search in Google to find how to format dates in SQL Server.
  446. //36.Write a SQL statement to create a table Users. Users should have username, password, full name and last login time.
  447.  
  448. // - Choose appropriate data types for the table fields.Define a primary key column with a primary key constraint.
  449.  
  450. // - Define the primary key column as identity to facilitate inserting records.
  451.  
  452. // - Define unique constraint to avoid repeating usernames.
  453.  
  454. // - Define a check constraint to ensure the password is at least 5 characters long.
  455. //37.Write SQL statements to insert in the Users table the names of all employees from the Employees table.
  456.  
  457. // - Combine the first and last names as a full name.
  458.  
  459. // - For username use the first letter of the first name + the last name(in lowercase).
  460.  
  461. // - Use the same for the password, and NULL for last login time.
  462. //38.Write a SQL statement that changes the password to NULL for all users that have not been in the system since 10.03.2010.
  463. //39.Write a SQL statement that deletes all users without passwords(NULL password).
  464. //40.Write a SQL query to display the average employee salary by department and job title.
  465. //41.Write a SQL query to display the minimal employee salary by department and job title along with the name of some of the employees that take it.
  466. //42.Write a SQL query to display the town where maximal number of employees work.
  467. //43.Write a SQL query to display the number of managers from each town.
  468. }
  469. }
  470. }
Add Comment
Please, Sign In to add comment