Informatics Practices
What is the difference between the order by and group by clauses when used along with a select statement? Explain with an example.
SQL Queries
3 Likes
Answer
ORDER BY:
The ORDER BY clause is used to sort the result set in ascending or descending order based on one or more columns.
Example:
SELECT *
FROM Vehicles
ORDER BY Price ASC;
This query selects all columns (*) from the Vehicles table and sorts the result set in ascending order (ASC) based on the Price column. The output will display the vehicles with the lowest price first.
GROUP BY:
The GROUP BY clause is used to group the result set based on one or more columns. It divides the result set into groups, and each group contains rows with the same values in the specified columns.
Example:
SELECT Type, AVG(Price) AS Average_Price
FROM Vehicles
GROUP BY Type;
This query selects the Type column and calculates the average price (AVG(Price)) for each group of vehicles with the same Type. The result set will display the average price for each type of vehicle.
Answered By
1 Like
Related Questions
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]
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.
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.
Write the names of any two common types of Intellectual Property Rights which are protected by law.