Objectives:
- Learn how to sort query results using
ORDER BY - Understand how to limit the number of results using
LIMIT - Use
ASCandDESCto specify sort order
1. Introduction to ORDER BY (5 minutes)
Syntax Overview:
- The
ORDER BYclause is used to sort the result set in ascending or descending order.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1 [ASC|DESC], column2 [ASC|DESC];
Example:
SELECT * FROM TestTable ORDER BY Name ASC;
2. Sorting in Ascending and Descending Order (10 minutes)
Ascending Order (ASC):
- Default order, sorts values from A to Z, or 0 to 9.
Descending Order (DESC):
- Sorts values from Z to A, or 9 to 0.
Examples:
- Sort by
Namein ascending order:
SELECT * FROM TestTable ORDER BY Name ASC;
- Sort by
DateOfBirthin descending order:
SELECT * FROM TestTable ORDER BY DateOfBirth DESC;
Exercise:
- Write SQL queries to:
- Sort
StudentsbyFirstNamein ascending order. - Sort
StudentsbyEnrollmentDatein descending order.
3. Limiting Results with LIMIT (10 minutes)
Syntax Overview:
- The
LIMITclause is used to specify the number of records to return.
SELECT column1, column2, ...
FROM table_name
ORDER BY column1
LIMIT number;
Example:
SELECT * FROM TestTable ORDER BY Name ASC LIMIT 5;
Exercise:
- Write SQL queries to:
- Retrieve the first 3 rows from
Studentstable, sorted byEnrollmentDate. - Select the top 5 students by
StudentIDin ascending order.
4. Combining ORDER BY and LIMIT (10 minutes)
Combining Syntax:
- Use
ORDER BYandLIMITtogether to sort and restrict results.
SELECT * FROM Students ORDER BY EnrollmentDate DESC LIMIT 5;
Example:
SELECT * FROM TestTable ORDER BY DateOfBirth ASC LIMIT 10;
Exercise:
- Write SQL queries to:
- Select the first 5 students ordered by
LastNamein descending order. - Retrieve the last 3 students by
StudentIDin ascending order.
5. Practical Exercise: Sorting and Limiting Results (5 minutes)
- Task:
- Continue using the
Studentstable. - Insert additional sample data:
INSERT INTO Students (StudentID, FirstName, LastName, EnrollmentDate)
VALUES
(6, 'Charlie', 'Davis', '2024-01-20'),
(7, 'Dana', 'Evans', '2024-01-21');
- Exercises:
- Retrieve all columns from
Studentsordered byFirstNamein ascending order. - Select
FirstNameandLastNamefromStudentswhereStudentIDis between 4 and 6, ordered byEnrollmentDatein descending order. - Limit the number of rows returned to 3, sorted by
LastName.
Exercises
- Sorting with ORDER BY:
- Exercise: Write SQL queries to:
- Sort
TestTablebyNamein ascending order. - Sort
TestTablebyDateOfBirthin descending order.
- Sort
- Using LIMIT:
- Exercise: Write SQL queries to:
- Retrieve the first 3 rows from
Studentstable, sorted byEnrollmentDate. - Select the top 5 students by
StudentIDin ascending order.
- Retrieve the first 3 rows from
- Combining ORDER BY and LIMIT:
- Exercise: Write SQL queries to:
- Select the first 5 students ordered by
LastNamein descending order. - Retrieve the last 3 students by
StudentIDin ascending order.
- Select the first 5 students ordered by
- Sample Queries for Students Table:
- Exercise: Execute the following queries:
- Retrieve all columns from
Studentsordered byFirstNamein ascending order. - Select
FirstNameandLastNamefromStudentswhereStudentIDis between 4 and 6, ordered byEnrollmentDatein descending order. - Limit the results to 3 rows, sorted by
LastName.
- Retrieve all columns from
Homework:
- Practice Questions:
- Write SQL queries to:
- Select the first 10 rows from
Studentsordered byEnrollmentDatein descending order. - Retrieve the last 4 students by
StudentID, sorted byLastNamein ascending order.
- Select the first 10 rows from
