Objectives:
- Learn how to sort query results using
ORDER BY
- Understand how to limit the number of results using
LIMIT
- Use
ASC
andDESC
to specify sort order
1. Introduction to ORDER BY (5 minutes)
Syntax Overview:
- The
ORDER BY
clause 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
Name
in ascending order:
SELECT * FROM TestTable ORDER BY Name ASC;
- Sort by
DateOfBirth
in descending order:
SELECT * FROM TestTable ORDER BY DateOfBirth DESC;
Exercise:
- Write SQL queries to:
- Sort
Students
byFirstName
in ascending order. - Sort
Students
byEnrollmentDate
in descending order.
3. Limiting Results with LIMIT (10 minutes)
Syntax Overview:
- The
LIMIT
clause 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
Students
table, sorted byEnrollmentDate
. - Select the top 5 students by
StudentID
in ascending order.
4. Combining ORDER BY and LIMIT (10 minutes)
Combining Syntax:
- Use
ORDER BY
andLIMIT
together 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
LastName
in descending order. - Retrieve the last 3 students by
StudentID
in ascending order.
5. Practical Exercise: Sorting and Limiting Results (5 minutes)
- Task:
- Continue using the
Students
table. - 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
Students
ordered byFirstName
in ascending order. - Select
FirstName
andLastName
fromStudents
whereStudentID
is between 4 and 6, ordered byEnrollmentDate
in descending order. - Limit the number of rows returned to 3, sorted by
LastName
.
Exercises
- Sorting with ORDER BY:
- Exercise: Write SQL queries to:
- Sort
TestTable
byName
in ascending order. - Sort
TestTable
byDateOfBirth
in descending order.
- Sort
- Using LIMIT:
- Exercise: Write SQL queries to:
- Retrieve the first 3 rows from
Students
table, sorted byEnrollmentDate
. - Select the top 5 students by
StudentID
in 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
LastName
in descending order. - Retrieve the last 3 students by
StudentID
in ascending order.
- Select the first 5 students ordered by
- Sample Queries for Students Table:
- Exercise: Execute the following queries:
- Retrieve all columns from
Students
ordered byFirstName
in ascending order. - Select
FirstName
andLastName
fromStudents
whereStudentID
is between 4 and 6, ordered byEnrollmentDate
in 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
Students
ordered byEnrollmentDate
in descending order. - Retrieve the last 4 students by
StudentID
, sorted byLastName
in ascending order.
- Select the first 10 rows from