Sobes.tech
Middle

What are ORDER BY and GROUP BY used for in SQL? Which aggregate functions have you used in your work?

sobes.tech AI

Answer from AI

ORDER BY is used to sort the query results by one or more columns, for example, in ascending or descending order. This helps organize the data for easier reading or further processing.

GROUP BY is used to group rows that have the same values in specified columns, allowing aggregate functions to be performed on each group.

Commonly used aggregate functions:

  • COUNT() — counts the number of rows
  • SUM() — sums the values
  • AVG() — calculates the average
  • MAX() — finds the maximum value
  • MIN() — finds the minimum value

Example:

SELECT department, COUNT(*) AS employee_count, AVG(salary) AS avg_salary
FROM employees
GROUP BY department
ORDER BY avg_salary DESC;
What are ORDER BY and GROUP BY used for in SQL? Which… - sobes.tech