1/21
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What does ORDER BY do?
Sorts the final query results by one or more fields.
What types of fields can ORDER BY sort?
Numbers, dates, and text/string fields.
What happens if a query has no ORDER BY clause?
Results are unsorted and usually display in the order recorded in the database.
What is the default ORDER BY sort direction?
Ascending (ASC).
What does ASC mean?
Ascending order: lowest to highest, earliest to latest, or A to Z.
What does DESC mean?
Descending order: highest to lowest, latest to earliest, or Z to A.
Example: sort names A–Z
SELECT Name FROM Customer ORDER BY Name ASC;
Alternative query for names A–Z
SELECT Name FROM Customer ORDER BY Name;
Why do SELECT Name FROM Customer ORDER BY Name; and ORDER BY Name ASC give the same result?
ASC is the default sort order.
Example: sort credit scores highest to lowest
SELECT Name, CreditScore FROM Customer ORDER BY CreditScore DESC;
Example: sort dates oldest to newest
SELECT Name, FirstPurchase FROM Customer ORDER BY FirstPurchase ASC;
Example: sort dates newest to oldest
SELECT Name, FirstPurchase FROM Customer ORDER BY FirstPurchase DESC;
How do you sort by more than one field?
List fields in ORDER BY, separated by commas.
Example: sort first by State, then by Name
SELECT Name, State FROM Customer ORDER BY State ASC, Name ASC;
What does ORDER BY State ASC, Name ASC do?
Sorts states A–Z; within each state, it sorts names A–Z.
What is the most important rule for multiple-field sorting?
The first field is sorted first; the next field breaks ties.
Example: sort by highest credit score, then name A–Z
SELECT Name, CreditScore FROM Customer ORDER BY CreditScore DESC, Name ASC;
Can each ORDER BY field have a different direction?
Yes. Example: ORDER BY State ASC, CreditScore DESC.
Where is ORDER BY written in a basic SELECT query?
After FROM and WHERE. Example: SELECT Name FROM Customer WHERE State = 'Utah' ORDER BY Name ASC;
When does ORDER BY execute?
Near the end—after SELECT and DISTINCT.
Exam tip: What must match the question exactly?
The ORDER BY field order and each field’s sort direction (ASC or DESC).
Exam tip: ASC vs. DESC
ASC = A–Z/low–high/old–new; DESC = Z–A/high–low/new–old.