From the course: Quick Start Guide to SQL

Unlock the full course today

Join today to access over 23,200 courses taught by industry experts.

Creating groups and nested groups

Creating groups and nested groups

From the course: Quick Start Guide to SQL

Creating groups and nested groups

- [Instructor] Let's learn how to group rows in a table. Instead of using the entire table, sometimes we need small groups of rows to perform operations individually. In the previous video, we saw how to find the employee with highest salary under one manager, 7,003. But now I need to find the employee with the highest salary under every manager. To do this, we group rows in the table by using GROUP BY clause based on manager. Let's look at an example. If I give SELECT manager, MAX(salary) FROM emp_tab GROUP BY manager ORDER BY MAX(salary), and they want it in descending. If I run this, this is going to give me the manager and the employees with highest salaries who are reporting to them, as well. The syntax for GROUP BY clause is select column names, group functions, and include the columns in the braces. And the group functions could be like min, max, count, any of those functions from table name, where condition,…

Contents