Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2017 August SQL Puzzle: SQL Advance Query – Find most awarded cricket match type

SQL Puzzle: SQL Advance Query – Find most awarded cricket match type

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

Check the below input data and require output data to find most awarded cricket match type which also won 10th worldcup.
Input Data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
match_id match_name year Worldcup_10th
----------- -------------------------------- ----------- -------------
1 ICC_MATCH 2005 1
2 ICL_MATCH 2006 1
3 IPL_MATCH 2001 1
4 ICL_MATCH 2006 0
5 ICL_MATCH 1999 0
6 IPL_MATCH 2002 0
7 IPL_MATCH 2000 0
8 IPL_MATCH 1999 0
9 IPL_MATCH 1998 0
10 World_cup 1999 0
11 ICC_MATCH 1999 0
12 ICC_MATCH 2006 0

Expected Output:

1
2
3
match_name
--------------------------------
IPL_MATCH

Create a table with sample data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
create table tbl_cricketMatches
(
match_id INT
,match_name Varchar(32)
,[year] INT
,Worldcup_10th INT
)
insert into tbl_cricketMatches
values (1, 'ICC_MATCH', 2005, 1)
,(2, 'ICL_MATCH', 2006, 1)
,(3, 'IPL_MATCH', 2001, 1)
,(4, 'ICL_MATCH', 2006, 0)
,(5, 'ICL_MATCH', 1999, 0)
,(6, 'IPL_MATCH', 2002, 0)
,(7, 'IPL_MATCH', 2000, 0)
,(8, 'IPL_MATCH', 1999, 0)
,(9, 'IPL_MATCH', 1998, 0)
,(10, 'World_cup', 1999, 0)
,(11, 'ICC_MATCH', 1999, 0)
,(12, 'ICC_MATCH', 2006, 0)

Solution 1:

1
2
3
4
5
6
7
8
with ctetest as
(
select match_name, count(1) as ct from tbl_cricketMatches group by match_name
)
select a.match_name from ctetest a
inner join tbl_cricketMatches b
on a.match_name = b.match_name and b.Worldcup_10th=1
where ct = (select max(ct) from ctetest)

Solution 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
WITH CTE AS
(
SELECT match_name, Worldcup_10th,
COUNT(*) over(PARTITION BY match_name) cnt
FROM tbl_cricketMatches
)
,CTE2 AS
(
SELECT a.match_name ,COUNT(a.cnt) Maxy
FROM CTE a
INNER JOIN tbl_cricketMatches b
ON a.match_name = b.match_name
WHERE b.Worldcup_10th = 1
GROUP BY a.match_name
)
SELECT match_name FROM CTE2
WHERE Maxy = (SELECT MAX(Maxy) FROM CTE2)

Please try the different solutions for this puzzle and share it via comment...

Aug 11, 2017Anvesh Patel
SQL Server: How to find Table name from Page ID?PostgreSQL: pgAdmin 4 Error - Unrecognized configuration parameter “bytea_output”
Comments: 2
  1. Prabhu Thambidurai
    July 4, 2018 at 10:02 am

    select * from
    (
    select cm.*,count(*) over (partition by match_name ) as cnt from tbl_cricketMatches cm ORDER BY count(*) over (partition by match_name ) DESC
    )
    where worldcup_10th=1 AND ROWNUM=1

    ReplyCancel
  2. Avinash Reddy
    February 25, 2020 at 2:44 pm

    with cte
    as
    (

    Select Match_name,count(1) as ct from #tbl_cricketMatches group by match_name

    )

    select match_name from cte where ct =( select max(ct) from cte)

    ReplyCancel

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

August 11, 2017 2 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Interview, SQL Problem, SQL Programming, SQL Puzzle, SQL Query, SQL Tips and Tricks
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....