Advertisement
XfreeBG

Untitled

Jan 14th, 2023
8
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.33 KB | None | 0 0
  1. create table cities(
  2. city_id int auto_increment,
  3. name varchar(30) not null,
  4. constraint PK_city primary key(city_id)
  5. );
  6.  
  7. CREATE TABLE users(
  8. user_id INT PRIMARY KEY AUTO_INCREMENT,
  9. username VARCHAR(50) NOT NULL UNIQUE,
  10. password VARCHAR(255) NOT NULL,
  11. first_name VARCHAR(255) NOT NULL,
  12. last_name VARCHAR(255),
  13. balance DECIMAL(15, 2) NOT NULL,
  14. city_id INT NOT NULL,
  15. register_date DATETIME NOT NULL,
  16. CONSTRAINT FK_users_cities FOREIGN KEY(city_id) REFERENCES cities(city_id)
  17. );
  18.  
  19. CREATE TABLE scooters(
  20. scooter_id INT PRIMARY KEY AUTO_INCREMENT,
  21. model VARCHAR(255) NOT NULL,
  22. gps_position VARCHAR(255) NOT NULL,
  23. is_taken BOOL DEFAULT FALSE
  24. );
  25.  
  26. CREATE TABLE rents(
  27. rent_id INT PRIMARY KEY AUTO_INCREMENT,
  28. user_id INT NOT NULL,
  29. scooter_id INT,
  30. start_date DATETIME NOT NULL,
  31. end_date DATETIME,
  32. is_completed BOOL DEFAULT FALSE,
  33. CONSTRAINT FK_rents_users FOREIGN KEY(user_id) REFERENCES users(user_id),
  34. CONSTRAINT FK_rents_scooters FOREIGN KEY(scooter_id) REFERENCES scooters(scooter_id)
  35. );
  36. -- Query 2
  37. SELECT name FROM cities
  38. ORDER BY name
  39. LIMIT 5;
  40.  
  41. -- Query 3
  42. SELECT username, register_date FROM users
  43. WHERE balance = 0
  44. ORDER BY register_date DESC, username ASC;
  45.  
  46. -- Query 4
  47. SELECT username, balance FROM users AS u
  48. JOIN cities AS c
  49. ON c.city_id = u.city_id
  50. WHERE c.name = 'Varna'
  51. ORDER BY balance DESC
  52. LIMIT 1;
  53.  
  54. -- Query 5
  55. SELECT username, balance FROM users AS u
  56. LEFT JOIN rents AS r
  57. ON u.user_id = r.user_id
  58. WHERE r.rent_id IS NULL
  59. ORDER BY balance DESC, username
  60. LIMIT 5;
  61.  
  62.  
  63. SELECT username, balance FROM users
  64. WHERE user_id NOT IN (SELECT DISTINCT user_id FROM rents)
  65. ORDER BY balance DESC, username
  66. LIMIT 5;
  67.  
  68. -- Query 6
  69. SELECT
  70. COUNT(*) AS total_rents,
  71. c.name
  72. FROM rents AS r
  73. JOIN users AS u
  74. ON u.user_id = r.user_id
  75. JOIN cities AS c
  76. ON c.city_id = u.city_id
  77. GROUP BY c.name
  78. ORDER BY total_rents DESC, r.rent_id
  79. LIMIT 10;
  80.  
  81. -- Query 7
  82. SELECT
  83. COUNT(*) AS 'total_rents',
  84. u.username
  85. FROM users AS u
  86. JOIN rents AS r
  87. ON r.user_id = u.user_id
  88. GROUP BY u.username
  89. ORDER BY total_rents DESC, u.username
  90. LIMIT 5;
  91.  
  92. -- Query 8
  93. SELECT COUNT(*) AS 'not_returned' FROM rents AS r
  94. JOIN users AS u
  95. ON u.user_id = r.user_id
  96. JOIN cities AS c
  97. ON c.city_id = u.city_id
  98. WHERE c.name = 'Plovdiv' AND r.is_completed = FALSE;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement