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 July SQL Puzzle: SQL Advance Query – Find a Book name which is printed in 50% of Languages

SQL Puzzle: SQL Advance Query – Find a Book name which is printed in 50% of Languages

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

Please check the below input & output data, find a book name which is printed on more than 50% of languages.

Input data:

1
2
3
4
5
6
7
bookname lang
---------- ----------
ABC EN
XYZ FRE
ABC EN
ANV EN
ABC ITA

Require output:

1
2
3
bookname
----------
ABC

Create a table with sample data:

1
2
3
4
5
6
7
8
9
create table books
(
bookname varchar(10)
,lang varchar(10)
)
 
insert into books values
('ABC','EN'),('XYZ','FRE'),('ABC','EN')
,('ANV','EN'),('ABC','ITA')

Solution 1 (Using CTE):

1
2
3
4
5
6
7
8
9
10
with ctetest as
(
select
bookname,COUNT(1) as bookcount
,(select COUNT(distinct lang) from books) as langcount
from books
group by bookname
)
select bookname from ctetest
where (bookcount / langcount) > 0.5

Solution 2 (Using Subquery):

1
2
3
4
5
6
7
8
9
10
select bookname
from
(
select
bookname, count(1) bookcount
,(select count(distinct lang) from books) langcount
from books
group by bookname
)as t
where (bookcount/langcount)>0.5

Jul 20, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Find third highest Employee SalarySQL Server: Practical example of Exceeding the maximum row size of 8060
Comments: 1
  1. Sql
    February 14, 2018 at 8:12 am

    Hi Anvesh,

    I tried to write using by EXISTS but I could not.. Could you rewrite for me please? Please use following data because 2 book has the same number(ABC, XYZ) and I want to know that how the code run if 2 record return if use the subquery. Thank you

    create table books
    (
    bookname varchar(10)
    ,lang varchar(10)
    )

    insert into books values
    (‘ABC’,’EN’),(‘XYZ’,’FRE’),(‘ABC’,’EN’)
    ,(‘ANV’,’EN’),(‘ABC’,’ITA’),(‘XYZ’,’FRE’),(‘XYZ’,’FRE’)

    ReplyCancel

Leave a Reply to Sql Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

July 20, 2017 1 Comment SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Advance Query, SQL Puzzle, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Monitoring, SQL Server Performance Tuning, SQL Server Programming, SQL Server Tips and Tricks, TSQL
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....