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 June SQL Puzzle: SQL Advance Query – Find first 100 Prime numbers in SQL Server

SQL Puzzle: SQL Advance Query – Find first 100 Prime numbers in SQL Server

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

Question:
Write a SQL query to find first 100 Prime numbers in SQL Server.

Require Output:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
PrimeNumbers
------------
2
3
5
7
11
13
17
19
23
29
31
37
41
43
47
53
59
61
67
71
73
79
83
89
97

Solution:

1
2
3
4
5
6
7
8
9
10
SELECT DISTINCT Number AS PrimeNumbers
FROM MASTER..SPT_VALUES AS a
WHERE
Number >= 2 AND Number <= 100 AND
NOT EXISTS
(
SELECT 1 FROM MASTER..SPT_VALUES AS b WHERE b.Number > 1
AND b.Number < a.Number
AND a.Number % b.Number = 0
)

Jun 18, 2017Anvesh Patel
SQL Puzzle: SQL Advance Query - Draw a Triangle without using any LoopPostgreSQL: Use PSQL command line variable to make your SQL Queries Dynamic
Comments: 5
  1. Pasi
    September 22, 2017 at 6:49 pm

    Incomplete solution, that’s only 25 first prime numbers… šŸ˜‰

    ReplyCancel
    • Anvesh Patel
      Anvesh Patel
      September 22, 2017 at 7:11 pm

      Total prime numbers for top 100 numbers…

      ReplyCancel
  2. Ankush
    September 17, 2018 at 11:26 am

    Select * from (
    Select rw, case when (mod(rw,2) 0 OR rw= 2) AND
    (mod(rw,3) 0 OR rw = 3) AND
    (mod(rw,5) 0 OR rw = 5) AND
    mod(rw,9) 0 AND
    mod(rw,11) 0 THEN ‘Prime’ ELSE ‘Not Prime’ END as Number_Status FROM
    (Select Rownum as rw
    From dual
    Connect By Rownum <= 1000)foo)foo2
    where Number_Status = 'Prime'
    and rownum <= 100

    ReplyCancel
  3. Carter
    June 29, 2019 at 10:00 pm

    You only showing the prime number from 1 to 100
    Wh as version of SQL are you using because Mod is build in function in all SQL

    For me would use a CTE table and then select from the CTE table and cross join it to it self

    ;with CTEPrime(
    Select 1 n
    Union all
    Select n+1 from CTEPrime
    Where nc2.n

    ReplyCancel
  4. Ashish Rawat
    October 4, 2019 at 2:37 pm

    Easiest and fastest way to find primes numbers for first 100 numbers in SQL server

    DECLARE @range int = 100 –(Select your range )
    ,@x INT = 1, @y INT = 1

    While (@y <= @range)
    BEGIN
    while (@x <= @y)
    begin
    IF ((@y%@x) =0)
    BEGIN
    IF (@x = @y)
    PRINT @y
    break
    END
    IF ((@y%@x)0)
    set @x = @x+1
    end
    set @x = 2
    set @y = @y+1
    end

    ReplyCancel

Leave a Reply to Anvesh Patel Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

June 18, 2017 5 Comments SQL PuzzleAnvesh Patel, database, database research and development, dbrnd, SQL Puzzle, SQL Query, SQL Server, SQL Server Administrator, SQL Server Error, SQL Server Interview, 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....