@@ -81,24 +81,68 @@ VALUES (1, 1, 1, '2021-01-01'),
81
81
## Ad-Hoc Questions and Solutions
82
82
### 1. What are the details of all cars purchased in the year 2022?
83
83
84
- ``` SQL code ```
84
+ ``` SQL
85
+ SELECT *
86
+ FROM cars
87
+ WHERE car_id IN (
88
+ SELECT car_id
89
+ FROM sales2
90
+ WHERE YEAR(purchase_date) = 2022
91
+ );
92
+ ```
85
93
86
94
### 2. What is the total number of cars sold by each salesperson?
87
- ``` SQL code ```
95
+ ``` SQL
96
+ SELECT s .name AS Salesperson,
97
+ COUNT (* ) AS Total_Cars_Sold
98
+ FROM salespersons s
99
+ JOIN sales2 sa ON s .salesman_id = sa .salesman_id
100
+ GROUP BY s .name ;
101
+ ```
88
102
89
103
### 3.What is the total revenue generated by each salesperson?
90
104
91
- ``` SQL code ```
105
+ ``` SQL
106
+ SELECT s .name AS Salesperson,
107
+ SUM (c .cost_ $) AS Total_Revenue_Generated
108
+ FROM salespersons s
109
+ JOIN sales2 sa ON s .salesman_id = sa .salesman_id
110
+ JOIN cars c ON sa .car_id = c .car_id
111
+ GROUP BY s .name ;
112
+ ```
92
113
93
114
### 4. What are the details of the cars sold by each salesperson?
94
115
95
- ``` SQL code ```
116
+ ``` SQL
117
+ SELECT s .name AS Salesperson,
118
+ c.*
119
+ FROM salespersons s
120
+ JOIN sales2 sa ON s .salesman_id = sa .salesman_id
121
+ JOIN cars c ON sa .car_id = c .car_id ;
122
+ ```
96
123
### 5. What is the total revenue generated by each car type?
97
124
98
- ``` SQL code ```
125
+ ``` SQL
126
+ SELECT c .type ,
127
+ SUM (c .cost_ $) AS Total_Revenue_Generated
128
+ FROM cars c
129
+ JOIN sales2 sa ON c .car_id = sa .car_id
130
+ GROUP BY c .type ;
131
+ ```
99
132
### 6. What are the details of the cars sold in the year 2021 by salesperson 'Emily Wong'?
100
133
101
- ``` SQL code ```
134
+ ``` SQL
135
+ SELECT c.*
136
+ FROM cars c
137
+ JOIN sales2 sa ON c .car_id = sa .car_id
138
+ JOIN salespersons s ON sa .salesman_id = s .salesman_id
139
+ WHERE YEAR(sa .purchase_date ) = 2021 AND s .name = ' Emily Wong' ;
140
+ ```
141
+ # car_id, make, type, style, cost_ $
142
+ 2, Toyota, Corolla, Hatchback, 25000
143
+ 4, Chevrolet, Camaro, Coupe, 36000```
144
+
145
+ ```
102
146
### 7. What is the total revenue generated by the sales of hatchback cars?
103
147
104
148
``` SQL code ```
0 commit comments