SQL join tables with group by and order by

In this page, we are going to discuss the usage of GROUP BY and ORDER BY clause within a join.


Sample table: agents

Sample table: orders

To get 'agent_code' and 'agent_name' columns from the table 'agents' and sum of 'advance_amount' column from the table 'orders' after a joining, with following conditions -

1. 'agent_code' of 'agents' and 'orders' must be same,

2. the same combination of 'agent_code' and 'agent_name' of 'agents' table must be within a group,

3. 'agent_code' of 'agents' table should arrange in an order, default is ascending order,

the following SQL statement can be used:

-- Selecting specific columns: 'agent_code' and 'agent_name' from the 'agents' table, and the sum of 'advance_amount' from the 'orders' table
SELECT agents.agent_code, agents.agent_name, SUM(orders.advance_amount)
-- Performing a Cartesian product (cross join) between the 'agents' and 'orders' tables (implicit join)
FROM agents, orders
-- Defining the join condition in the WHERE clause where 'agents.agent_code' equals 'orders.agent_code'
WHERE agents.agent_code = orders.agent_code
-- Grouping the result set by 'agent_code' and 'agent_name'
GROUP BY agents.agent_code, agents.agent_name
-- Sorting the result set by 'agent_code' in ascending order
ORDER BY agents.agent_code;


  • This SQL query retrieves data from two tables: 'agents' and 'orders'.

  • It selects specific columns from the 'agents' table: 'agent_code' and 'agent_name', along with the sum of 'advance_amount' from the 'orders' table.

  • The query performs a Cartesian product (cross join) between the 'agents' and 'orders' tables, implicitly joining every row from the 'agents' table with every row from the 'orders' table.

  • The join condition is specified in the WHERE clause, where 'agents.agent_code' must equal 'orders.agent_code', linking the two tables based on the agent code.

  • The result set is then grouped by 'agent_code' and 'agent_name' using the GROUP BY clause. This ensures that the sum of advance amounts is calculated for each unique combination of agent code and name.

  • Finally, the result set is sorted by 'agent_code' in ascending order using the ORDER BY clause.

  • This query is useful for calculating the total advance amount for each agent, providing insight into the overall performance of each agent in terms of advance payments received.

Sample Output:

---------- ---------------------------------------- --------------------------
A001       Subbarao                                                        200
A002       Mukesh                                                         3500
A003       Alex                                                           1000
A004       Ivan                                                           2100
A005       Anderson                                                       3100
A006       McDen                                                           600
A007       Ramasundar                                                      500
A008       Alford                                                         3300
A009       Benjamin                                                        100
A010       Santakumar                                                     3700
A011       Ravi Kumar                                                      900
A012       Lucida                                                          450

See our Model Database

Here is a new document which is a collection of questions with short and simple answers, useful for learning SQL as well as for interviews.

Check out our 1000+ SQL Exercises with solution and explanation to improve your skills.

Previous: Joining tables through referential integrity
Next: Join two tables related by a single column primary key or foriegn key pair

Follow us on Facebook and Twitter for latest update.