Ive got table with columns:
PlayerId
Points
I would like to get 100 best players (the more points player have, the better he is). What would be the quesry for that ?
Im using sql server 2008
SELECT TOP 100 PlayerId
FROM TableName
ORDER BY Points DESC
To break it down:
TOP 100 - Selects the top 100 records to return.
ORDER BY Points DESC - Orders the results by the Points field, and DESC sets them in numerical reverse (assuming Points is an integer data type).
You'll need to select the top X from the query and then order by the points in descending order.
select top 100 * from players order by Points DESC
What happens when two or more players have the same points?, or even worse, what happens if you have 120 players with the maximum points. You should use a query that returns every one of those players. I recommend that you use RANK if this is the case.
;WITH CTE AS
(
SELECT *, RANK() OVER(ORDER BY Points DESC) RN
FROM Players
)
SELECT *
FROM CTE
WHERE RN <= 100
SELECT TOP 100 PlayerID
FROM <<Table>>
ORDER BY Points DESC
Assuming your table name is Players:
SELECT TOP 100 PlayerId, Points
FROM Players
ORDER BY Points DESC
SELECT TOP 100 *
FROM tblName ORDER BY Points DESC
SELECT TOP 100 PlayerId FROM name_of_your_table ORDER BY Points DESC
Related
i am a newbie to SQL.
I wanna find out what which player is oldest by age.
So here is my table..
Somehow my Query give error.
Can you please tell me where i am doing it wrong.
Thanks.
select * from players
where age = (select max(age) as Oldest_Player from players);
limit 1
SQL has a SELECT TOP command, which allows you to retrieve a set number of rows. You can do SELECT TOP 1 name AS 'Oldest Person' FROM players ORDER BY age DESC
What this will do is: first retrieve all the players, sort them by age descending (oldest first), then take the first one.
You can use row_number as below:
Select * from (
Select *, RowN = Row_Number() over(order by age desc) from Players
) a Where a.RowN = 1
How can I find the sum of the lowest five points in the Point column
and group by ID
Table
The desired results should be;
Results
No idea where to start
Thanks
select a.ID, SUM(a.points) from(select ID , points,row_number() over
(partition by ID order by POINTS) as rownum_returned from your_table) a where
a.rownum_returned<6 group by a.ID;
Read about row_number() function here
If I've to do that I would solve it with a subquery.
In SQL server I will do subquery that retrieve the 5 lower point
Select Top 5 id, point from table
Order by point asc
Note: the keyword TOP that limit the result to the first 5
Note 2: order by point asc will order the result putting in top the lowest value
Now I use the query as subquery to complete the activity
Select id, sum (point) from
(Select top 5 id,point from table order by point asc) group by id
This should work
Let's say I have an app that determine the winners in a prize drawing. All entries are entered into a table indicating their employeeID. Each employee can enter the drawing multiple times. I select from the table, order by newid to get a random sort. I assume the more entries (database records) an employee has the better chance he will end up in the top 5 of my query each time I run it. So far so good. However, because each employee has multiple records, there is a good chance he will come up multiple times in the top 5. I need the ability to return 5 unique records from the randomly sorted results.
How do I get 5 unique rows while still ensuring those with multiple drawing entries get a heavier weighting in the selection?
My base query:
SELECT TOP 5 employeeID
FROM events
TABLESAMPLE(1000 ROWS)
ORDER BY CHECKSUM(NEWID());
Kinda what I am trying to do:
SELECT TOP 5 *
FROM events
WHERE employeeID IN (SELECT employeeID
FROM events
TABLESAMPLE(1000 ROWS)
ORDER BY CHECKSUM(NEWID())
)
ORDER BY CHECKSUM(NEWID())
But of course I cannot do an order by in the subquery.
Any solution must take into account 2 things:
If an employee enter multiple tickets, his chance of winning increases relative to other.
Everyone can only win once
Here's my approach:
;WITH
tmp1 AS
(
SELECT EmployeeID,
ROW_NUMBER() OVER (ORDER BY NEWID()) AS SortOrder
FROM Events
),
tmp2 AS
(
SELECT EmployeeID,
MIN(SortOrder) AS WinOrder
FROM tmp1
GROUP BY EmployeeID
)
SELECT TOP 5 *
FROM tmp2
ORDER BY WinOrder
The SQL Fiddle gives employees 1 & 5 higher chances to win, but they will only win once each, regardless of how many times they enter.
Here's a fairly simple way to get what you're after:
select top 5 EmployeeID
from
(
select EmployeeID, row_number() over (order by newid()) DrawOrder
from Events
) wins
group by EmployeeID
order by min(DrawOrder)
This is a pretty complex query so i am having difficulties here. If you help me i appreciate it.
select AvgLevel,TotalCount,tblPokedex.PokemonId,Name,Type1,Type2,Hp,tblPokedex.Attack,tblPokedex.Defense,tblPokedex.SpAttack,tblPokedex.SpDefense,tblPokedex.Speed,(Hp+tblPokedex.Attack+tblPokedex.Defense+tblPokedex.SpAttack+tblPokedex.SpDefense+tblPokedex.Speed) as TotalStats
from tblPokemonStats,tblAvailablePokemons,tblPokedex
left join tblUsersPokemons on tblPokedex.PokemonId=tblUsersPokemons.PokemonId
where tblPokemonStats.PokemonId=tblPokedex.PokemonId
group by tblPokedex.PokemonId,tblPokedex.Name,tblPokedex.Type1,tblPokedex.Type2,tblPokedex.Hp,tblPokedex.Attack,tblPokedex.Defense,tblPokedex.SpAttack,tblPokedex.SpDefense,tblPokedex.Speed,tblPokemonStats.AvgLevel,tblPokemonStats.TotalCount
order by PokemonId asc
Alright so what i want to do is for example select between top 100 and 150
How can i do that ?
Here's one way - tack on a row_number column and filter on that:
select AvgLevel /*...*/
from (
select AvgLevel /*...*/, row_number() over (order by PokemonId) rownum
from tblPokemonStats,tblAvailablePokemons,tblPokedex
left join tblUsersPokemons on tblPokedex.PokemonId=tblUsersPokemons.PokemonId
where tblPokemonStats.PokemonId=tblPokedex.PokemonId
group by /* etc ... */
) A
where A.rownum > 100 and A.rownum <= 150
row_number does what it sounds like - it numbers your rows.
Here's another possibility - say you want rows X through Y. You can select the TOP Y rows according to your order. Then use that a subquery and select the TOP Y-X according to the opposite order.
Assuming X = 100 and Y = 150:
select top 50 *
from (
select top 150 PokemonId
from tblPokemonStats
order by PokemonId
) A
order by PokemonId desc
As far as performance, I'm not sure which would be better. My gut says that the ORDER BY approach is better for pages near the 'front' so to speak, but row_number() would be better for later pages (high X and Y). That's just a guess though - run some tests and see what works for you.
I have an SQL Query that i'm running but I only want to select a specific row. For example lets say my query was:
Select * from Comments
Lets say this returns 10 rows, I only want to select the 8th record returned by this query. I know I can do:
Select Top 5 * from Comments
To get the top 5 records of that query but I only want to select a certain record, is there anything I can put into this query to do that (similar to top).
Thanks
jack
This is a classic interview question.
In Ms SQL 2005+ you can use the ROW_NUMBER() keyword and have the Predicate ROW_NUMBER = n
USE AdventureWorks;
GO
WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
ROW_NUMBER() OVER (ORDER BY OrderDate) AS 'RowNumber'
FROM Sales.SalesOrderHeader
)
SELECT *
FROM OrderedOrders
WHERE RowNumber = 5;
In SQL2000 you could do something like
SELECT Top 1 *FROM
[tblApplications]
where [ApplicationID] In
(
SELECT TOP 5 [ApplicationID]
FROM [dbo].[tblApplications]
order by applicationId Desc
)
How about
SELECT TOP 1 * FROM
(SELECT TOP 8 * FROM Comments ORDER BY foo ASC)
ORDER BY foo DESC
First, you should say which RDBMS you're using.
Second, you should give careful thought to what it is you're trying to accomplish. Relational Databases are set-based. In general, the order of elements in a set does not matter. You'll want to ask why it matters in this case, then see if there's a better way to embed the concept of order into the query itself.
For instance, in SQL Server 2005 (and other RDBMS), you can use the ROW_NUMBER function to assign a sequential number to each row returned, based on the criteria you specify. You could then select rows based on the row number. Example from Books Online:
USE AdventureWorks;
GO
WITH OrderedOrders AS
(
SELECT SalesOrderID, OrderDate,
ROW_NUMBER() OVER (ORDER BY OrderDate) AS 'RowNumber'
FROM Sales.SalesOrderHeader
)
SELECT *
FROM OrderedOrders
WHERE RowNumber BETWEEN 50 AND 60;
SELECT * FROM comments WHERE ...conditions... LIMIT 1 OFFSET 8
OFFSET is a good thing for MySQL
For SQL Server 2005:
select rank() OVER (ORDER BY c.subject, c.date) as rank, c.subject, c.date
from comments c
where rank = 8
Well, in T-SQL (the dialect for SQL Server) you can do the following:
SELECT TOP 1 *
FROM (SELECT TOP 8 *
FROM Table
ORDER
BY SortField)
ORDER
BY SortField DESC
This way you get the 8th record.
I have read the question & your comments on you would want next 3 blog comments etc.
How is your tables structured?
Assume that you have blog post Id & comment Id is generated in ascending order for each blog post, you could do a SELECT based on the current Id.
e.g. if the blogpostId = 101, you get the top 3 comments order by posted Id. Now lets say, you want to get the next 3 comments - you could do a SELECT WHERE commentId between the last comment id shown TO the comment id - 3
But all that depends on how your tables are defined.
In SQL 2000 where you do not have ROW_NUMBER() function you could use a work-around like this:
SELECT CommentsTableFieldList, IDENTITY(INT, 1,1) as seqNo
INTO #SeqComments
FROM Comments
SELECT * FROM #SeqComments
WHERE seqNo = 8
select top 1 *
from TableName
where ColumnName1 in
(
select top nth ColumnName1
from TableName
order by ColumnName1 desc
)
order by ColumnName1 desc
From the SELECT reference, use the LIMIT keyword:
SELECT * FROM tbl LIMIT 5,10; # Retrieve rows 6-15
SELECT * FROM tbl LIMIT 5; # Retrieve first 5 rows
Note: this is for MySQL, other SQL engines may have a different keyword.
Select from tablename limit nthrow,1;
try This
Let us assume , We want select 5th row of WC_Video Table
And
Select * from (Select Row_Number() over (Order by Uploadedon) as 'rownumber',* from Wc_Video )as Temp where rownumber=5