Informatics Practices
A relation Vehicles is given below:
| V_no | Type | Company | Price | Qty |
|---|---|---|---|---|
| AW125 | WagonR | Maruti | 250000 | 25 |
| J0083 | Jeep | Mahindra | 4000000 | 15 |
| S9090 | SUV | Mitsubishi | 2500000 | 18 |
| M0892 | Mini van | Datsun | 1500000 | 26 |
| W9760 | SUV | Maruti | 2500000 | 18 |
| R2409 | Mini van | Mahindra | 350000 | 15 |
Write SQL commands to:
(i) Display the average price of each type of vehicle having a quantity of more than 20.
(ii) Count the type of vehicles manufactured by each company.
(iii) Display the total price of all types of vehicles.
SQL Queries
1 Like
Answer
(i)
SELECT Type, AVG(Price) AS Average_Price
FROM Vehicles
WHERE Qty > 20
GROUP BY Type;
Output
+----------+---------------+
| Type | Average_Price |
+----------+---------------+
| WagonR | 250000.0000 |
| Mini van | 1500000.0000 |
+----------+---------------+
(ii)
SELECT Company, COUNT(DISTINCT Type) AS Num_Vehicle_Types
FROM Vehicles
GROUP BY Company;
Output
+------------+-------------------+
| Company | Num_Vehicle_Types |
+------------+-------------------+
| Datsun | 1 |
| Mahindra | 2 |
| Maruti | 2 |
| Mitsubishi | 1 |
+------------+-------------------+
(iii)
SELECT SUM(Price * Qty) AS Total_Price
FROM Vehicles;
Output
+-------------+
| Total_Price |
+-------------+
| 200500000 |
+-------------+
Answered By
1 Like
Related Questions
Mr. Sombuddha, an HR Manager in a multinational company "Star-X World", has created the following table to store the records of employees:
Table: Emp
Eid EName Department DOB DOJ Star1 Dev Sales 1994-08-28 2020-02-14 Star2 Melinda IT 1997-10-15 2021-11-19 Star3 Raj Accounts 1998-10-02 2019-04-02 Star4 Michael Sales 2000-02-17 2020-05-01 Star5 Sajal IT 2001-12-05 2018-06-13 Star6 John Accounts 1995-01-03 2019-07-15 Star7 Julia Sales 1985-11-13 2020-08-19 Predict the output of the following queries that he has written:
(i) Select max(year(DOB)) from emp;
(ii) Select ENAME from emp where month(DOJ) = 11;
(iii) Select length(EName) from emp where Department = "IT";
Write a Python code to create a dataframe with appropriate headings from the list given below:
['S101', 'Amy', 70], ['S102', 'Bandhi', 69], ['S104', 'Cathy', 75], ['S105', 'Gundaho', 82]
What is the difference between the order by and group by clauses when used along with a select statement? Explain with an example.
Prerna received an email from her bank stating that there is a problem with her account. The email provides instructions and a link, by clicking on which she can log in to her account and fix the problem. Help Prerna by telling her the precautions she should take when she receives these types of emails.