SQL Exercises: Count how many salesmen each day register orders
SQL Aggregate Functions: Exercise-20 with Solution
From the following table, write a SQL query to count the number of orders based on the combination of each order date and salesperson. Return order date, salesperson id.
Sample table: orders
SELECT ord_date,salesman_id,COUNT(*) FROM orders GROUP BY ord_date,salesman_id;
Output of the Query:
ord_date salesman_id count 2012-07-27 5001 1 2012-08-17 5007 1 2012-04-25 5001 1 2012-09-10 5002 1 2012-10-05 5002 1 2012-10-10 5003 1 2012-09-10 5005 1 2012-08-17 5003 1 2012-06-27 5002 1 2012-09-10 5001 1 2012-10-05 5001 1 2012-10-10 5006 1
The said SQL query that count the number of orders placed on each date by each salesman. The "GROUP BY ord_date, salesman_id" clause groups the rows in the 'orders' table by the "ord_date" and "salesman_id" columns, and the "COUNT()" function counts the number of rows in each group.
Relational Algebra Expression:
Relational Algebra Tree:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
SQL: Tips of the Day
How to drop all tables from a database with one SQL query?
USE Databasename SELECT 'DROP TABLE [' + name + '];' FROM sys.tables
- Weekly Trends
- Python Interview Questions and Answers: Comprehensive Guide
- Scala Exercises, Practice, Solution
- Kotlin Exercises practice with solution
- MongoDB Exercises, Practice, Solution
- SQL Exercises, Practice, Solution - JOINS
- Java Basic Programming Exercises
- SQL Subqueries
- Adventureworks Database Exercises
- C# Sharp Basic Exercises
- SQL COUNT() with distinct
- Java Collection Exercises
- SQL COUNT() function
- SQL Inner Join
We are closing our Disqus commenting system for some maintenanace issues. You may write to us at reach[at]yahoo[dot]com or visit us at Facebook