Advertisement
makispaiktis

Query 3 - Filters (=, LIKE, IN, %, AND, BETWEEN)

Jun 6th, 2023 (edited)
1,196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 1.96 KB | Source Code | 0 0
  1. USE [AdventureWorks2012]
  2.  
  3. SELECT * FROM [HumanResources].[Department]
  4.  
  5. -- 1. QUERIES
  6.  
  7. -- 1a. Show me all the department names (2nd column)
  8. SELECT Name FROM [HumanResources].[Department]
  9. -- 1b. Show me all the department group names (3rd column)
  10. SELECT GroupName FROM [HumanResources].[Department]
  11. -- 1c. Give me the distinct values of  group names
  12. SELECT DISTINCT GroupName FROM [HumanResources].[Department]
  13.  
  14.  
  15. -- 2. FILTERS
  16.  
  17. -- 2a. Show me all the department names which are part of manufacturing
  18. SELECT Name,GroupName FROM [HumanResources].[Department]
  19. WHERE GroupName LIKE 'MANUFACTURING'
  20. -- Change table (Employee not Department)
  21. -- 2b. Give me a list level of employees of org. level = 2
  22. SELECT  * FROM [HumanResources].[Employee]
  23. WHERE [OrganizationLevel] = 2
  24. -- 2c. Give me a list level of employees of org. level = 2 or 3
  25. SELECT  * FROM [HumanResources].[Employee]
  26. WHERE [OrganizationLevel] IN (2, 3)
  27. -- 2d. Give me a list of employees who have a title as facilities manager (not case-sensitive)
  28. SELECT  * FROM [HumanResources].[Employee]
  29. WHERE [JobTitle] LIKE 'FaCIlities MANager'
  30. -- 2e. Give me a list of employees who have the word 'Manager' in the job title as the last word of the title
  31. -- '%' means searching before 'Manager'. If I had put it after 'Manager', it would search
  32. -- also after 'Manager'
  33. SELECT  * FROM [HumanResources].[Employee]
  34. WHERE [JobTitle] LIKE '%Manager'
  35. -- 2f. Give me a list of employees who have the word 'control' in the job title
  36. SELECT  * FROM [HumanResources].[Employee]
  37. WHERE [JobTitle] LIKE '%control%'
  38. -- 2g. Give me all employees who were born after January 1 1980
  39. SELECT  * FROM [HumanResources].[Employee]
  40. WHERE [BirthDate] > '1/1/1980'
  41. -- 2h. Give me all employees who were born between Jan 1, 1970 and Jan 1, 1980
  42. SELECT  * FROM [HumanResources].[Employee]
  43. WHERE [BirthDate] > '1/1/1970' AND [BirthDate] < '1/1/1980'
  44.  
  45. SELECT  * FROM [HumanResources].[Employee]
  46. WHERE [BirthDate] BETWEEN '1/1/1970' AND '1/1/1980'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement