Related Topics

Database Management System
SELECT column1, column2, ...
FROM table
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC], ...
Here’s a breakdown of the syntax:
SELECT
: Specifies the columns you want to retrieve in the query result.FROM
: Specifies the table from which you want to retrieve the data.ORDER BY
: Specifies the columns by which you want to sort the data.column1, column2, ...
: The columns by which you want to sort the data. You can specify multiple columns separated by commas.ASC
(optional): Specifies ascending order (default if not specified). The data will be sorted in ascending order.DESC
(optional): Specifies descending order. The data will be sorted in descending order.
Here’s an example to illustrate how to sort data in a query result:
SELECT name, age, salary
FROM employees
ORDER BY age DESC, salary ASC;
In the above example, the query retrieves the name
, age
, and salary
columns from the employees
table. The data is sorted first by age
in descending order, and then by salary
in ascending order.
By using the ORDER BY clause in your query, you can control the sorting of the data in the query result based on your specific requirements.
SELECT COUNT(*) AS total_rows,
SUM(sales_amount) AS total_sales,
AVG(sales_amount) AS average_sales,
MIN(sales_amount) AS min_sale,
MAX(sales_amount) AS max_sale
FROM sales_data;
In the above example, we have a table called sales_data
. The SELECT statement uses several aggregate functions to calculate different statistics from the sales_amount
column. The COUNT(*)
function counts the total number of rows, while SUM
, AVG
, MIN
, and MAX
calculate the total sales, average sales, minimum sale, and maximum sale, respectively.
Aggregate functions can also be used with the GROUP BY clause to calculate statistics for specific groups of data. For example:
SELECT category, COUNT(*) AS total_products, AVG(price) AS average_price
FROM products
GROUP BY category;
In this example, the query groups the products by category and then calculates the total number of products and the average price for each category.
Aggregate functions are powerful tools in SQL that allow you to summarize and analyze data in various ways, providing valuable insights and summary statistics for decision-making and reporting purposes.




Popular Category
Topics for You
Go through our study material. Your Job is awaiting.