Advertisement
panaewboi

lab5

Sep 17th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
SQL 4.61 KB | None | 0 0
  1. ----- PRACTICE 1 -----
  2. SELECT * FROM propertyforrent;
  3.  
  4. SELECT COUNT(*) FROM  propertyforrent;
  5. -- 1. How many clients are there?
  6.  
  7. SELECT COUNT(*) FROM propertyforrent WHERE TYPE = 'Flat';
  8. -- 2. How many clients whose preferred property type are Flat?
  9.  
  10. SELECT AVG(rent) FROM propertyforrent WHERE TYPE = 'Flat' AND rooms = 3;
  11. -- 3. What is average rent for three-room flat?
  12.  
  13. SELECT POSITION, COUNT(staffno), MIN(salary), MAX(salary), AVG(salary)
  14.     FROM staff
  15.     GROUP BY POSITION;
  16. -- 4. What are the number of staff and the minimum, maximum and average staff salary in each staff position?
  17.  
  18. SELECT * FROM viewing;
  19.  
  20. SELECT clientno, COUNT(clientno)
  21.     FROM viewing
  22.     GROUP BY clientno HAVING COUNT(clientno)>1;
  23. -- 5. Which clients have viewed the properties more than one time?
  24.  
  25. SELECT * FROM propertyforrent;
  26.  
  27. SELECT ownerno, COUNT(*)
  28.     FROM propertyforrent
  29.     GROUP BY ownerno;
  30. -- 6. How many properties are owned by each property owner?
  31.  
  32. SELECT propertyno, COUNT(staffno),TYPE
  33.     FROM propertyforrent
  34.     GROUP BY propertyno, TYPE;
  35. -- 7. Count the number of properties by staff number and type of property
  36.  
  37. SELECT * FROM registration;
  38.  
  39. SELECT branchno, COUNT(clientno) FROM registration
  40. GROUP BY branchno;
  41. -- 8. How many customers register in each branch? Show the branch number and the number of customers.
  42.  
  43. SELECT * FROM propertyforrent;
  44.  
  45. SELECT TYPE, COUNT(TYPE)
  46. FROM propertyforrent
  47. GROUP BY TYPE;
  48. -- 9. Find out the total number of properties rented by type of property.
  49.  
  50. SELECT city, TYPE, AVG(rent)
  51. FROM propertyforrent
  52. GROUP BY city, TYPE;
  53. -- 10.Find out the average rent of property rented by city and by type of property.
  54.  
  55.  
  56.  
  57. ----- PRACTICE 2 -----
  58. SELECT pf.* FROM privateowner po JOIN propertyforrent pf ON po.ownerno = pf.ownerno;
  59. -- 1. List full details of all properties for rent along with owner’s name (first name and last name).
  60.  
  61. SELECT staffno, fname, lname, b.city FROM staff s JOIN branch b ON s.branchno = b.branchno;
  62. -- 2. List the staff number, first and last names and branch city of all staffs.
  63.  
  64. SELECT DISTINCT c.fname, COUNT(v.viewdate)
  65. FROM client c JOIN viewing v ON c.clientno = v.clientno
  66. GROUP BY c.fname HAVING COUNT(v.viewdate)>1;
  67. -- 3. List the name of clients who have viewed the properties more than one time.
  68.  
  69. SELECT s.staffno, s.branchno, po.propertyno
  70. FROM propertyforrent po JOIN staff s ON po.staffno = s.staffno;
  71. -- 4. List the number of properties handled by each staff member, along with the branch number of the member of staff
  72.  
  73. SELECT DISTINCT s.fname, b.city
  74. FROM branch b JOIN staff s ON b.branchno = s.branchno
  75.     JOIN propertyforrent p ON s.staffno = p.staffno
  76.     WHERE TYPE = 'House';
  77. -- 5. List the branch cities that staffs manage the properties that are house type. Remove the duplicate records.
  78.  
  79. SELECT *
  80. FROM client c JOIN viewing v ON c.clientno = v.clientno
  81. JOIN registration r ON c.clientno = r.clientno
  82. JOIN branch b ON r.branchno = b.branchno
  83. JOIN staff s ON r.staffno = s.staffno;
  84.  
  85.  
  86.  
  87. SELECT c.fname || ' ' || c.lname AS "client name",
  88.     v.viewdate
  89. FROM client c JOIN viewing v ON c.clientno = v.clientno
  90. JOIN registration r ON c.clientno = r.clientno
  91. JOIN branch b ON r.branchno = b.branchno
  92. JOIN staff s ON r.staffno = s.staffno
  93. WHERE b.city = 'Glasgow';
  94. -- 6. List the client name and view date of all viewings on properties that are managed by staff's branch office in Glasgow.
  95.  
  96. SELECT po.fname || ' ' || po.lname AS "name" , COUNT(v.viewdate)
  97. FROM propertyforrent pf JOIN privateowner po ON pf.ownerno = po.ownerno
  98.     JOIN viewing v ON pf.propertyno = v.propertyno
  99.     WHERE pf.city = 'Glasgow'
  100.     GROUP BY po.fname || ' ' || po.lname
  101.     HAVING COUNT(pf.propertyno)>2;
  102. -- 7. List the name of owners whose properties locate in Glasgow and clients have viewed the properties of the same owner more than two times.
  103.  
  104.  
  105. SELECT s.branchno,
  106.         po.fname || ' ' || po.lname AS "owner's name",
  107.         pf.propertyno,
  108.         pf.street,
  109.         pf.city,
  110.         pf.postcode,
  111.         pf.TYPE,
  112.         pf.rooms,
  113.         pf.rent,
  114.         pf.staffno,
  115.         pf.ownerno
  116. FROM propertyforrent pf JOIN privateowner po ON pf.ownerno = po.ownerno
  117. JOIN staff s ON pf.staffno = s.staffno
  118. WHERE s.branchno = 'B003';
  119. -- 8. List full details of all properties for rent along with owner’s name (first name and last name).  Show only the properties that are managed by staffs in Branch No B003.
  120.  
  121.  
  122. SELECT b.city,COUNT(pf.propertyno)
  123. FROM propertyforrent pf JOIN staff s ON pf.staffno = s.staffno
  124.     JOIN branch b ON s.branchno = b.branchno
  125. GROUP BY b.city;
  126. -- 9. How many properties are managed in each branch city?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement