davidjwest
davidjwest

Reputation: 546

Add LIMIT to INNER JOIN (SQL Server)

I have this seemingly working query:

    USE [Development Westy]
GO

SELECT [ToReadType_60]
  FROM [dbo].['SMART Month End'] AS e

INNER JOIN [dbo].['SMART Reads$'] AS p 
ON e.ContractNumber LIKE p.[Contract Number]
GO

How would I limit the number of rows returned? I cannot seem to get it to take the LIMIT command.

I am running the above but it is taking a long time as there are millions of rows, so I want to test it using just a small selection.

Thanks,

Upvotes: 0

Views: 760

Answers (2)

Adam Scharp
Adam Scharp

Reputation: 632

Use the TOP command and specify the amount of rows you want returned.

USE [Development Westy]
GO

SELECT TOP 100 [ToReadType_60]
  FROM [dbo].['SMART Month End'] AS e

INNER JOIN [dbo].['SMART Reads$'] AS p 
ON e.ContractNumber LIKE p.[Contract Number]

Upvotes: 1

Gordon Linoff
Gordon Linoff

Reputation: 1269753

You can just use TOP:

SELECT TOP 100 [ToReadType_60]
FROM [dbo].['SMART Month End'] e INNER JOIN
     [dbo].['SMART Reads$'] p 
     ON e.ContractNumber = p.[Contract Number];

I would also suggest using = instead of like. And indexes on the columns used for the joins can help performance.

Upvotes: 2

Related Questions