Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. Database
  4. Using TOP operator within a WHERE clause

Using TOP operator within a WHERE clause

Scheduled Pinned Locked Moved Database
tutorialquestion
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    OBRon
    wrote on last edited by
    #1

    I know that the TOP operator can't be within the where clause, but is there a way to achieve this behavior in a single select call? For example, if I want to select all books where publisher = 3 or 5; but I want to limit the results to only getting the top 5 for publisher 3 and the top 10 for publisher 5. Is this achievable on a single select call? Something along the lines of: select * from books where (publisher = 3 and top 5) or (publisher = 5 and top 10) Thanks in advance if anyone knows. Ron Ward

    D 1 Reply Last reply
    0
    • O OBRon

      I know that the TOP operator can't be within the where clause, but is there a way to achieve this behavior in a single select call? For example, if I want to select all books where publisher = 3 or 5; but I want to limit the results to only getting the top 5 for publisher 3 and the top 10 for publisher 5. Is this achievable on a single select call? Something along the lines of: select * from books where (publisher = 3 and top 5) or (publisher = 5 and top 10) Thanks in advance if anyone knows. Ron Ward

      D Offline
      D Offline
      David Llamas
      wrote on last edited by
      #2

      You could use a union...

      SELECT TOP 5 FROM books WHERE publisher = 3
      UNION
      SELECT TOP 5 FROM books WHERE publisher = 5

      O 1 Reply Last reply
      0
      • D David Llamas

        You could use a union...

        SELECT TOP 5 FROM books WHERE publisher = 3
        UNION
        SELECT TOP 5 FROM books WHERE publisher = 5

        O Offline
        O Offline
        OBRon
        wrote on last edited by
        #3

        That's a really good idea. Thanks for the quick response.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups