Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7903375
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:44:04+00:00 2026-06-03T09:44:04+00:00

I am new to SQL, so I need your help on a query. Basically

  • 0

I am new to SQL, so I need your help on a query. Basically I have a Database of ZipCodes and I want to get 3 items before the selected ZipCode and 3 items after. The Query that I came up with is pretty bad…

WITH numberedlogtable AS
(
SELECT *
 FROM dbo.US
)

SELECT *
FROM numberedlogtable
WHERE ZipCode IN (SELECT ZipCode+i
             FROM numberedlogtable
             CROSS JOIN (SELECT -1 AS i UNION ALL SELECT 0 UNION ALL SELECT 1) n
             WHERE ZipCode='91803')

I picked up a sample Query from somewhere and successfully converted it for my use. The only problem is that this Query returns current item and the next item. Instead, it should returns previous 3 items, current item, and next three items.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:44:06+00:00Added an answer on June 3, 2026 at 9:44 am

    Using a common table expression (the WITH part) producing a numbered sequence:

    WITH NumberedZipCodes AS
    (SELECT SELECT ROW_NUMBER() OVER (ORDER BY ZipCode) AS RowNumber, *
    FROM ZipCodes)
    
    SELECT * From NumberedZipCodes
    WHERE RowNumber BETWEEN
    (SELECT RowNumber FROM NumberedZipCodes WHERE ZipCode=91803) - 3
    AND (SELECT RowNumber FROM NumberedPerson WHERE ZipCode=91803) + 3
    

    Normally in SQL there is no such concept as the previous or next items in a match. Actually, unless an order by clause is specified the rows are returned in any order that the sql engine find suitable. To make a query like this, an order has to be applied and index numbers generated. That’s done in NumberedZipCodes. The second part is just a query to get the data out of it.

    To have the query run efficiently, make sure that there is an index on the ZipCode column.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm somewhat new to SQL and need help with query syntax. My issue involves
I need to create an entirely new Sql Server 2008 database and want to
I am new to SQL Server 2008, and I need some help in query
I need to add a new column to a MS SQL 2005 database with
I am writing some new SQL queries and want to check the query plans
We have a security problem here and we really need your help and inputs.
I'm completely new to sql and can't do that myself. So I need your
I am very new to SQL and i need to retreive a number of
I am very new to sql.The actual problem is much bigger. I need information
I need to restrict the development users from creating new schedules for SQL Agent

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.