Guest User

Untitled

a guest
Jan 15th, 2019
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1. -- Query all columns from the jobs table, and limit to 10 rows
  2.  
  3. SELECT *
  4. FROM jobs
  5. LIMIT 10;
  6.  
  7. -- Query all columns with costs above 50
  8.  
  9. SELECT *
  10. FROM jobs
  11. WHERE costs > 50;
  12.  
  13. -- Query the state and organization Columns from the jobs table
  14.  
  15. SELECT
  16. state_abb
  17. ,organization_id
  18. FROM jobs;
  19.  
  20. -- Query a count of jobs in NY
  21.  
  22. SELECT COUNT(*)
  23. FROM jobs
  24. WHERE state_abb = 'NY';
  25.  
  26. -- Query the count of jobs in NY for last 30 days
  27.  
  28. SELECT COUNT(*)
  29. FROM jobs
  30. WHERE state_abb = 'NY'
  31. AND jobs.created_at > CURRENT_DATE - 30;
  32.  
  33. -- Query the average cost of jobs
  34.  
  35. SELECT AVG(costs)
  36. FROM jobs;
  37.  
  38. -- Query the average cost per account
  39.  
  40. SELECT
  41. account_id
  42. ,AVG(costs)
  43. FROM jobs
  44. GROUP BY 1;
  45.  
  46. -- Find the first and last jobs created
  47.  
  48. SELECT MIN(jobs.created_at), MAX(jobs.created_at)
  49. FROM jobs;
  50.  
  51. -- Find the total cost per organization for jobs that were completed
  52.  
  53. SELECT
  54. organizations.name as organization_name
  55. ,SUM(costs) as Total_cost
  56. FROM Jobs
  57. LEFT JOIN organizations ON jobs.organization_id = organizations.id
  58. LEFT JOIN accounts ON jobs.account_id = accounts.id
  59. WHERE status = 'complete'
  60. GROUP BY 1;
  61.  
  62. -- What are the statuses
  63.  
  64. SELECT
  65. status
  66. ,COUNT(*)
  67. FROM jobs
  68. GROUP BY 1;
  69.  
  70. -- Which organization has the most jobs
  71.  
  72. SELECT
  73. organization_id
  74. ,COUNT(*)
  75. FROM jobs
  76. GROUP BY 1
  77. ORDER BY 2 DESC;
  78.  
  79. -- Which month had the most activity
  80.  
  81. SELECT
  82. date_trunc('month', (created_at)::timestamp)::date
  83. ,COUNT(*)
  84. FROM jobs
  85. GROUP BY 1;
Add Comment
Please, Sign In to add comment