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 November SQL Puzzle: SQL Advance Query – Count the total number of missing Numbers

SQL Puzzle: SQL Advance Query – Count the total number of missing Numbers

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

Check the below input data and expected output to find the total number of missing numbers from the table.

Input Data:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Number
-----------
1
2
3
5
6
8
9
15
19
21
22
24
29

Expected Data:

1
2
3
TotalMissingNumbers
--------------------
16

Create a table with data:

1
2
3
4
5
6
CREATE TABLE tbl_Ranges (Number INTEGER PRIMARY KEY)
GO
 
INSERT INTO tbl_Ranges VALUES
(1),(2),(3),(5),(6),(8),(9),(15),(19),(21),(22),(24),(29)
GO

Solution:

1
2
3
4
5
6
7
8
9
10
;WITH CTE
AS
(
SELECT
Number
,Number - row_number() OVER (ORDER BY Number) rnk
FROM tbl_Ranges
)
SELECT MAX(rnk) TotalMissingNumbers
FROM CTE

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

Nov 25, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Find the most repeated value of a TableGreenplum: Script to find information for Long running Queries with occupied resource
Comments: 8
  1. Harikrushna V. Adiecha
    November 27, 2017 at 8:38 am

    I solve this in mysql database. please check, i tried, and it’s working fine.

    Solution #1:

    SELECT SUM(p.diff) as TotalMissingNumbers from
    (
    SELECT t.Number,
    t.Number – @last – 1 AS diff,
    @last := t.Number AS last
    FROM tbl_ranges t,
    (SELECT @last := 0) r
    ) as p

    I found one more solution, which is even simpler and straight forward

    Solution #2:

    SELECT MAX(Number) – COUNT(Number) as TotalMissingNumbers FROM `tbl_ranges`

    ———————————————————————————-

    Apart from this, I wish to know how can we calculate complexity of the query we wrote, if you know any tool or any technique to get such calculations then please let me know.

    I’m very curious to know such things.

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      November 27, 2017 at 11:58 am

      Not sure about any tool, but checking query execution plan we can estimate the complexity of any query.

      ReplyCancel
  2. Dinesh IS
    November 30, 2017 at 7:05 am

    Declare @MaxNumber As Int
    Select @MaxNumber=Max(Number)
    From TBL_Ranges
    ;
    WITH CTE
    As
    (Select 1 As SLNo
    Union All
    Select SLNo+1
    From CTE
    Where SLNo<@MaxNumber)
    Select Count(SLNo) As NoOfNumber From CTE
    Where SLNo Not In(Select Number From TBL_Ranges)

    ReplyCancel
  3. aprkturk
    January 15, 2018 at 11:27 am

    If it starts with 1 it is ok otherwise no.

    ReplyCancel
  4. VINUTNA
    May 22, 2018 at 12:01 pm

    with cte as
    (
    select number, lead(number)over (order by number) as lead_values from tbl_Ranges
    ),
    cte1 as
    (
    select (lead_values – number)-1 as required from cte
    )
    select sum(required) from cte1
    where required >= 1

    ReplyCancel
  5. Ankush
    September 14, 2018 at 8:27 am

    Select max(diff)
    from
    (Select num, rownum, num-rownum as diff from table)

    ReplyCancel
  6. vinay
    December 25, 2018 at 5:24 pm

    SELECT ((SELECT max(number) from tbl_ranges)- (SELECT count(number) from tbl_ranges))

    ReplyCancel
  7. Suraj MU
    April 25, 2020 at 5:56 am

    SELECT (MAX(number)-MIN(number)+1) – COUNT(number)
    FROM tbl_ranges

    ReplyCancel

Leave a Reply to Dinesh IS Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

November 25, 2017 8 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....