Advertisement
favmatteo

Untitled

Jun 21st, 2023
1,840
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 4.92 KB | None | 0 0
  1. CREATE TABLE Cars (
  2.     car_id INT PRIMARY KEY,
  3.     brand VARCHAR(50),
  4.     model VARCHAR(50),
  5.     year INT,
  6.     rental_price DECIMAL(10, 2),
  7.     available BOOLEAN,
  8.     location_id INT,
  9.     FOREIGN KEY (location_id) REFERENCES Locations (location_id)
  10. );
  11.  
  12. INSERT INTO Cars (car_id, brand, model, year, rental_price, available, location_id)
  13. VALUES
  14.     (1, 'Ford', 'Focus', 2019, 50.00, true, 1),
  15.     (2, 'Toyota', 'Corolla', 2020, 60.00, true, 2),
  16.     (3, 'Honda', 'Civic', 2021, 55.00, true, 3),
  17.     (4, 'Volkswagen', 'Golf', 2018, 45.00, false, 2);
  18.  
  19.    CREATE TABLE Rentals (
  20.     rental_id INT PRIMARY KEY,
  21.     rental_date DATE,
  22.     customer_id INT,
  23.     car_id INT,
  24.     rental_days INT,
  25.     total_cost DECIMAL(10, 2),
  26.     location_id INT,
  27.     FOREIGN KEY (customer_id) REFERENCES Customers (customer_id),
  28.     FOREIGN KEY (car_id) REFERENCES Cars (car_id),
  29.     FOREIGN KEY (location_id) REFERENCES Locations (location_id)
  30. );
  31.  
  32. INSERT INTO Rentals (rental_id, rental_date, customer_id, car_id, rental_days, total_cost, location_id)
  33. VALUES
  34.     (1, '2023-06-01', 1, 2, 5, 300.00, 2),
  35.     (2, '2023-06-03', 2, 1, 3, 180.00, 1),
  36.     (3, '2023-06-04', 3, 3, 2, 110.00, 3),
  37.     (4, '2023-06-05', 4, 2, 7, 420.00, 2);
  38.  
  39.    CREATE TABLE Locations (
  40.     location_id INT PRIMARY KEY,
  41.     city VARCHAR(50),
  42.     address VARCHAR(100)
  43. );
  44.  
  45. insert into Rentals (rental_id, rental_date, customer_id, car_id, rental_days, total_cost, location_id)
  46. values
  47.     (8, '2023-06-12', 3, 2, 5, 320.00, 2);
  48.  
  49. INSERT INTO Locations (location_id, city, address)
  50. VALUES
  51.     (1, 'New York', '123 Main Street'),
  52.     (2, 'London', '456 Park Avenue'),
  53.     (3, 'Paris', '789 Elm Road'),
  54.     (4, 'Berlin', '321 Oak Lane');
  55.  
  56.    
  57.    CREATE TABLE Customers (
  58.     customer_id INT PRIMARY KEY,
  59.     first_name VARCHAR(50),
  60.     last_name VARCHAR(50),
  61.     email VARCHAR(50),
  62.     phone_number VARCHAR(20)
  63. );
  64.  
  65. INSERT INTO Customers (customer_id, first_name, last_name, email, phone_number)
  66. VALUES
  67.     (1, 'John', 'Smith', 'john@example.com', '123-456-7890'),
  68.     (2, 'Alice', 'Johnson', 'alice@example.com', '987-654-3210'),
  69.     (3, 'Michael', 'Brown', 'michael@example.com', '555-123-4567'),
  70.     (4, 'Maria', 'Rodriguez', 'maria@example.com', '111-222-3333');
  71.  
  72. INSERT INTO Customers (customer_id, first_name, last_name, email, phone_number)
  73. VALUES
  74.     (10, 'Mario', 'Catalasso', 'Mario@example.com', '323-321-9999');
  75.    
  76. -- Esercizio 1:
  77. -- Scrivi una query che restituisca i dettagli completi dei noleggi effettuati, inclusi i dettagli del cliente e le informazioni sull'auto noleggiata.
  78. select r.*, cc.*, c.*
  79. from Rentals r
  80. inner join Cars c on c.car_id
  81. inner join Customers cc on cc.customer_id
  82.  
  83. -- Esercizio 2:
  84. -- Scrivi una query che restituisca il conteggio totale dei noleggi effettuati da ciascun cliente.
  85. select c.first_name, c.last_name, count(r.rental_id)
  86. from Rentals r
  87. right join Customers c on c.customer_id = r.customer_id
  88. group by c.customer_id
  89.  
  90.  
  91. -- Esercizio 3:
  92. -- Scrivi una query che restituisca l'auto con il prezzo di noleggio più alto.
  93. select c.*
  94. from Cars c
  95. order by rental_price desc
  96. limit 1
  97.  
  98. -- Esercizio 4:
  99. -- Scrivi una query che restituisca i dettagli dei clienti che hanno effettuato almeno 3 noleggi.
  100. select c.*
  101. from Customers c
  102. left join Rentals r on r.customer_id = c.customer_id
  103. group by c.customer_id
  104. having count(r.rental_id) >= 3
  105.  
  106. -- Esercizio 5:
  107. -- Scrivi una query che restituisca i dettagli delle auto disponibili presso una determinata posizione,
  108. -- ordinati per prezzo di noleggio crescente.
  109. select c.*
  110. from Cars c
  111. inner join Locations l on c.location_id = l.location_id and l.location_id = 2
  112. order by c.rental_price
  113.  
  114.  
  115. -- Esercizio 6:
  116. -- Scrivi una query che restituisca i dettagli delle auto noleggiate dai clienti con almeno 2 noleggi,
  117. -- mostrando solo le auto con il prezzo di noleggio massimo.
  118. select  c.*, c2.customer_id
  119. from Cars c
  120. inner join Rentals r on c.car_id = r.car_id
  121. inner join Customers c2 on c2.customer_id = r.customer_id
  122. where (
  123.         select count(*)
  124.         from Customers c3
  125.         left join Rentals r on r.customer_id  = c3.customer_id
  126.         where c3.customer_id = c2.customer_id) > 2
  127. order by c.rental_price desc
  128. limit 1
  129.  
  130. -- Esercizio 7:
  131. -- Scrivi una query che restituisca il conteggio delle auto disponibili presso ciascuna posizione,
  132. -- ordinando i risultati in modo decrescente per il numero di auto disponibili.
  133. select l.city, count(c.car_id)
  134. from Cars c
  135. right join Locations l on c.location_id = l.location_id
  136. group by l.city
  137. order by count(c.car_id) desc
  138.  
  139. -- Esercizio 8:
  140. -- Scrivi una query che restituisca i dettagli dei noleggi effettuati in una data specifica, inclusi i
  141. -- dettagli del cliente, le informazioni sull'auto noleggiata e la posizione di ritiro.
  142. select r.*, c.*, cc.*, l.*
  143. from Rentals r
  144. inner join Cars c on c.car_id = r.car_id
  145. inner join Customers cc on r.customer_id = cc.customer_id
  146. inner join Locations l on l.location_id = c.location_id
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement