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 Order By Examples

##### 1-) List the students according to their names

Solution 1)

``````Select * from students
order by name``````

Solution 2)

``````Select * from students
order by 2``````

ETİKETLER

- -
##### 2-) List male students according to their names

Solution 1)

``````Select * from students
where gender = 'M'
order by name``````

Solution 2)

``````Select * from students
where gender = 'M'
order by 2``````

ETİKETLER

- - -
##### 3-) List all students according to their names reverse

Solution 1)

``````Select * from students
order by name desc``````

ETİKETLER

- -
##### 4-) List all students according to their class then by name

Solution 1)

``````Select * from students
order by sinif,gender``````

ETİKETLER

- -
##### 5-) List all students randomly ordered

Solution 1)

``````Select * from students
order by newid()``````

ETİKETLER

- -
##### 6-) List male students randomly ordered

Solution 1)

``````Select * from students
where gender = 'M'
order by newid()``````

ETİKETLER

- - -
##### 7-) List the male students whose class "10A" randomly ordered

Solution 1)

``````select * from students
where sinif='10A' and gender='M'
order by newid()``````

ETİKETLER

- - -
##### 8-) List 3 students, randomly selected

Solution 1)

``````Select top 3 * from students
order by newid()``````

ETİKETLER

- - -
##### 9-) List a student from 10A class, randomly selected

Solution 1)

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

ETİKETLER

- - - -
##### 10-) 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

- - - - -
##### 11-) 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

- - - - - -
##### 12-) List the student whose age is the biggest

Solution 1)

``````Select * from students
order by birthdate``````

ETİKETLER

- -
##### 13-) 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

- - - -
##### 14-)

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

- - - - - - -
##### 15-) List the name and surname of the students and the number of books they read sorted by BookCount.

Solution 1)

``````Select name,surname,count(*) BookCount
from students
join borrows on students.studentId = borrows.studentId
group by students.studentId,name,surname
order by BookCount desc``````

Solution 2)

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

ETİKETLER

- - - - - - - -
Library Database