Advertisement
panaewboi

LAB7 complete

Nov 4th, 2019
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 2.72 KB | None | 0 0
  1. -----LAB7-----
  2.  
  3. --1.    List staffs who manage properties with three rooms.
  4. SELECT *
  5. FROM staff s
  6. WHERE s.staffno IN(
  7.     SELECT pf.staffno
  8.     FROM propertyforrent pf
  9.     WHERE pf.rooms = 3);
  10.    
  11.    
  12. --2.    List properties that owner’s telephone number starts with 012.
  13. SELECT *
  14. FROM propertyforrent pf
  15. WHERE pf.ownerno IN(
  16.     SELECT po.ownerno
  17.     FROM privateowner po
  18.     WHERE pf.ownerno = po.ownerno
  19.     AND po.telno LIKE '012%');
  20.    
  21. --3.    List branches that do not have any staff.
  22.  
  23. --type1
  24. SELECT *
  25. FROM branch b
  26. WHERE b.branchno NOT IN (
  27.     SELECT branchno
  28.     FROM staff);
  29. -- --- ---- -----
  30. --type2
  31. SELECT *
  32. FROM branch b
  33. WHERE NOT EXISTS(
  34.     SELECT s.branchno
  35.     FROM staff s
  36.     WHERE s.branchno = b.branchno);
  37.    
  38.    
  39. --4.    List staffs whose salary is less than the average salary of all staffs and staffs who work in the branch located in Glasgow city.
  40. SELECT *
  41. FROM staff s
  42. WHERE s.salary <(
  43.     SELECT AVG(s.salary)
  44.     FROM staff s
  45. )AND EXISTS(
  46.     SELECT b.branchno
  47.     FROM branch b
  48.     WHERE s.branchno = b.branchno
  49.     AND b.city = 'Glasgow');
  50.    
  51. ---------- 4 NOVEMBER 2019 ----------
  52. --5.    List staffs whose salary is greater than the salaries of all staffs who work at branch number B003.
  53. SELECT *
  54. FROM staff s
  55. WHERE s.salary > (
  56.     SELECT MAX(s.salary)
  57.     FROM staff s
  58.     JOIN branch b
  59.     ON s.branchno = b.branchno
  60.     WHERE b.branchno = 'B003'
  61. );
  62.  
  63. SELECT *
  64. FROM staff s
  65. WHERE s.salary > ALL(
  66.     SELECT s.salary
  67.     FROM staff s
  68.     JOIN branch b
  69.     ON s.branchno = b.branchno
  70.     WHERE b.branchno = 'B003'
  71. );
  72.  
  73. --6.    List properties that are viewed by clients more than two times.
  74. --type1
  75. SELECT *
  76. FROM propertyforrent pf --EXISTS ถ้าเป็นtrueจะแสดงผลลัพธ์ออกมา
  77. WHERE EXISTS(
  78.     SELECT v.propertyno , COUNT(v.clientno)
  79.     FROM viewing v
  80.     WHERE pf.propertyno = v.propertyno
  81.     GROUP BY v.propertyno
  82.     HAVING COUNT(v.clientno) > 2
  83. );
  84.  
  85. --type2
  86. SELECT *
  87. FROM propertyforrent
  88. WHERE propertyno IN(
  89.     SELECT propertyno FROM viewing
  90.     GROUP BY propertyno
  91.     HAVING COUNT(clientno)>2
  92. );
  93.                
  94. -- 7.   List clients who registered in the branch at 163 Main Street.
  95. SELECT *
  96. FROM client c
  97. WHERE c.clientno IN(
  98.     SELECT r.clientno
  99.     FROM registration r
  100.     JOIN branch b
  101.     ON b.branchno = r.branchno
  102.     WHERE c.clientno = r.clientno
  103.     AND b.street = '163 Main Street'
  104. );
  105.  
  106. -- 8.   In each branch, list staffs whose salary is greater than the average salary in their branch.
  107. --only type2
  108. SELECT *
  109. FROM staff s
  110. WHERE  s.salary > (
  111.             SELECT AVG(salary)
  112.             FROM staff
  113.             WHERE branchno = s.branchno
  114.             GROUP BY branchno);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement