SQL Challenges-1: Salesman who makes Largest Number of transactions
SQL Challenges-1: Exercise-51 with Solution
From the following table write a SQL query to find the ID for the salesman who makes largest number of transactions.
Input:
Table: sales
Structure:
Field | Type | Null | Key | Default | Extra |
---|---|---|---|---|---|
TRANSACTION_ID | int(5) | NO | PRI | ||
SALESMAN_ID | int(4) | NO | |||
SALE_AMOUNT | decimal(8,2) | YES |
Data:
TRANSACTION_ID | SALESMAN_ID | SALE_AMOUNT |
---|---|---|
501 | 18 | 5200.00 |
502 | 50 | 5566.00 |
503 | 38 | 8400.00 |
504 | 43 | 8400.00 |
505 | 11 | 9000.00 |
506 | 42 | 5900.00 |
507 | 13 | 7000.00 |
508 | 33 | 6000.00 |
509 | 41 | 8200.00 |
510 | 11 | 4500.00 |
511 | 51 | 10000.00 |
512 | 29 | 9500.00 |
513 | 59 | 6500.00 |
514 | 38 | 7800.00 |
515 | 58 | 9800.00 |
516 | 60 | 12000.00 |
517 | 58 | 13900.00 |
518 | 23 | 12200.00 |
519 | 34 | 5480.00 |
520 | 35 | 8129.00 |
521 | 49 | 9323.00 |
522 | 46 | 8200.00 |
523 | 47 | 9990.00 |
524 | 42 | 14000.00 |
525 | 44 | 7890.00 |
526 | 47 | 5990.00 |
527 | 21 | 7770.00 |
528 | 57 | 6645.00 |
529 | 56 | 5125.00 |
530 | 25 | 10990.00 |
531 | 21 | 12660.00 |
532 | 41 | 5514.00 |
533 | 17 | 15600.00 |
534 | 44 | 15000.00 |
535 | 12 | 17550.00 |
536 | 55 | 13000.00 |
537 | 58 | 16800.00 |
538 | 25 | 19900.00 |
539 | 57 | 9990.00 |
540 | 28 | 8900.00 |
541 | 44 | 10200.00 |
542 | 57 | 18000.00 |
543 | 34 | 16200.00 |
544 | 36 | 19998.00 |
545 | 30 | 13500.00 |
546 | 37 | 15520.00 |
547 | 36 | 20000.00 |
548 | 20 | 19800.00 |
549 | 22 | 18530.00 |
550 | 19 | 12523.00 |
551 | 46 | 9885.00 |
552 | 22 | 7100.00 |
553 | 54 | 17500.00 |
554 | 19 | 19600.00 |
555 | 24 | 17500.00 |
556 | 38 | 7926.00 |
557 | 49 | 7548.00 |
558 | 15 | 9778.00 |
559 | 56 | 19330.00 |
560 | 24 | 14400.00 |
561 | 18 | 16700.00 |
562 | 54 | 6420.00 |
563 | 31 | 18720.00 |
564 | 21 | 17220.00 |
565 | 48 | 18880.00 |
566 | 33 | 8882.00 |
567 | 44 | 19550.00 |
568 | 22 | 14440.00 |
569 | 53 | 19500.00 |
570 | 30 | 5300.00 |
571 | 30 | 10823.00 |
572 | 35 | 13300.00 |
573 | 35 | 19100.00 |
574 | 18 | 17525.00 |
575 | 60 | 8995.00 |
576 | 53 | 9990.00 |
577 | 21 | 7660.00 |
578 | 27 | 18990.00 |
579 | 11 | 18200.00 |
580 | 30 | 12338.00 |
581 | 37 | 11000.00 |
582 | 27 | 11980.00 |
583 | 18 | 12628.00 |
584 | 52 | 11265.00 |
585 | 53 | 19990.00 |
586 | 27 | 8125.00 |
587 | 25 | 7128.00 |
588 | 57 | 6760.00 |
589 | 19 | 5985.00 |
590 | 52 | 17641.00 |
591 | 53 | 11225.00 |
592 | 22 | 12200.00 |
593 | 59 | 16520.00 |
594 | 35 | 19990.00 |
595 | 42 | 19741.00 |
596 | 19 | 15000.00 |
597 | 57 | 19625.00 |
598 | 53 | 9825.00 |
599 | 24 | 16745.00 |
600 | 12 | 14900.00 |
Sample Solution:
SQL Code(MySQL):
CREATE TABLE sales (
TRANSACTION_ID INTEGER(5) NOT NULL,
SALESMAN_ID INTEGER(4) NOT NULL,
SALE_AMOUNT decimal(8,2),
PRIMARY KEY (TRANSACTION_ID)
);
INSERT INTO sales VALUES(501,18,5200.00),(502,50,5566.00),(503,38,8400.00),(504,43,8400.00),(505,11,9000.00);
INSERT INTO sales VALUES (506,42,5900.00),(507,13,7000.00),(508,33,6000.00),(509,41,8200.00),(510,11,4500.00);
INSERT INTO sales VALUES (511,51,10000.00),(512,29,9500.00),(513,59,6500.00),(514,38,7800.00),(515,58,9800.00);
INSERT INTO sales VALUES (516,60,12000.00),(517,58,13900.00),(518,23,12200.00),(519,34,5480.00),(520,35,8129.00);
INSERT INTO sales VALUES (521,49,9323.00),(522,46,8200.00),(523,47,9990.00),(524,42,14000.00),(525,44,7890.00);
INSERT INTO sales VALUES (526,47,5990.00),(527,21,7770.00),(528,57,6645.00),(529,56,5125.00),(530,25,10990.00);
INSERT INTO sales VALUES (531,21,12600.00),(532,41,5514.00),(533,17,15600.00),(534,44,15000.00),(535,12,17550.00);
INSERT INTO sales VALUES (536,55,13000.00),(537,58,16800.00),(538,25,19900.00),(539,57,9990.00),(540,28,8900.00);
INSERT INTO sales VALUES (541,44,10200.00),(542,57,18000.00),(543,34,16200.00),(544,36,19998.00),(545,30,13500.00);
INSERT INTO sales VALUES (546,37,15520.00),(547,36,20000.00),(548,20,19800.00),(549,22,18530.00),(550,19,12523.00);
INSERT INTO sales VALUES (551,46,9885.00),(552,22,7100.00),(553,54,17500.00),(554,19,19600.00),(555,24,17500.00);
INSERT INTO sales VALUES (556,38,7926.00),(557,49,7548.00),(558,15,9778.00),(559,56,19330.00),(560,24,14400.00);
INSERT INTO sales VALUES (561,18,16700.00),(562,54,6420.00),(563,31,18720.00),(564,21,17220.00),(565,48,18880.00);
INSERT INTO sales VALUES (566,33,8882.00),(567,44,19550.00),(568,22,14440.00),(569,53,19500.00),(570,30,5300.00);
INSERT INTO sales VALUES (571,30,10823.00),(572,35,13300.00),(573,35,19100.00),(574,18,17525.00),(575,60,8995.00);
INSERT INTO sales VALUES (576,53,9990.00),(577,21,7660.00),(578,27,18990.00),(579,11,18200.00),(580,30,12338.00);
INSERT INTO sales VALUES (581,37,11000.00),(582,27,11980.00),(583,18,12628.00),(584,52,11265.00),(585,53,19990.00);
INSERT INTO sales VALUES (586,27,8125.00),(587,25,7128.00),(588,57,6760.00),(589,19,5985.00),(590,52,17641.00);
INSERT INTO sales VALUES (591,53,11225.00),(592,22,12200.00),(593,59,16520.00),(594,35,19990.00),(595,42,19741.00);
INSERT INTO sales VALUES (596,19,15000.00),(597,57,19625.00),(598,53,9825.00),(599,24,16745.00),(600,12,14900.00);
SELECT salesman_id,COUNT(salesman_id) make_transaction
FROM sales
GROUP BY salesman_id
HAVING COUNT(salesman_id) =(
SELECT MAX(make_transaction)
FROM (SELECT COUNT(salesman_id) make_transaction
FROM sales
GROUP BY salesman_id) aa);
Sample Output:
salesman_id|make_transaction| -----------+----------------+ 57| 5| 53| 5|
SQL Code Editor:
Contribute your code and comments through Disqus.
Previous: Find the order id and the item name for all companies who are not registered with the distributor.
Next: compare the purchasing status of the average purchase quantity of products of a category to the average pruchase quantity of the distributor.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/sql-exercises/challenges-1/sql-challenges-1-exercise-51.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics