We will write some example about oldest and youngest records in sql query. To find oldest record we must sort list by a date column and list the top record. Or we can use sub query.
TO DOWNLOAD THE SAMPLE LİBRARY DATABASE CLICK
Example1: List the oldest student
1 |
Select top 1 * from students order by birthdate |
or with Subquery
1 2 |
Select * from students where birthdate = (Select min(birthdate) from students) |
Result:
Example2:List the most recently taken book.
1 2 3 |
Select top 1 books.*,takenDate from books join borrows on books.bookId = borrows.bookId order by takenDate desc |
or with Subquery
1 2 3 4 |
Select top 1 books.*,takenDate from books join borrows on books.bookId = borrows.bookId where takenDate = (Select max(takenDate) from borrows) |
Result: