The Queries in this page solved at the library database. It is a sample database generated by randomly records. You can download it from the link. You will see more than 500 examples in the future. You can filter from the search panel above by selecting sql statements. I hope you will like it

SQL Top Examples

1-) List 3 students, randomly selected

Solution 1)

Select top 3 * from students 
       order by newid()


ETİKETLER

Select - Top - Order By -
2-) List a student from 10A class, randomly selected

Solution 1)

Select top 1 * from students 
	where sinif= '10A' 
	order by newid()


ETİKETLER

Select - Top - Where - Order By -
3-) List a female student from 10A class, randomly selected

Solution 1)

Select top 1 * from students 
	where sinif= '10A' and gender= 'F' 
	order by newid()


ETİKETLER

Select - Top - Where - Order By - Multi Condition -
4-) List the book with the most page number

Solution 1)

Select top 1 * from books 
       order by sayfaSayisi desc

Solution 2)

Select * from books 
     where sayafaSayisi=(Select max(sayfaSayisi) from books)


ETİKETLER

Select - Top - Where - Order By - Sub Query - Aggragate -
5-) Insert into the authors table, Random selected five students

Solution 1)

Insert into authors(name,surname) 
	Select top 5 name,surname from students order by newid()


ETİKETLER

Select - Insert - Top - Order By -
6-)

Solution 1)

Select top 1 students.*,takendate 
from students,borrows 
where students.studentId = borrows.studentId 
order by borrows.takenDate desc

Solution 2)

Select top 1 students.*,takendate 
from students 
join borrows on students.studentId = borrows.studentId 
order by borrows.takenDate desc

Solution 3)

Select students.*,takendate 
from students 
join borrows on students.studentId = borrows.studentId 
where takenDate = (Select max(takenDate) from borrows)


ETİKETLER

Select - Top - Join - Where - Order By - Sub Query - Çoklu Tablo -
7-) List the name and surname of the students and the number of books they read sorted by BookCount. Also list the students who have never read a book.

Solution 1)

Select name,surname,count(borrowsno) BookCount 
from students
left join borrows on students.studentId = borrows.studentId 
group by students.studentId,name,surname
order by BookCount

Solution 2)

Select name, surname, 
	(Select count(*) from borrows 
		where students.studentId = borrows.studentId) as BookCount
from students 
order by BookCount


ETİKETLER

Select - Top - Where - Having - Sub Query -
8-) Öğrenci tablosundan sadece 5 kayıt listeletiniz.

Solution 1)

Select top 5 * from students


ETİKETLER

Select - Top -
9-) 10 tane yazar listeletiniz.

Solution 1)

Select top 10 * from authors


ETİKETLER

Select - Top -
10-) 5 tane farklı öğrenci adı listeletiniz.

Solution 1)

Select distinct top 5  name from students


ETİKETLER

Select - Top - Distinct -
11-) 3 tane farklı kitap adı listeleyiniz.

Solution 1)

Select distinct top 3  name from books


ETİKETLER

Select - Top - Distinct -
Library Database
Database