Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Help   Contact   Merch   Join   Order   Logon   Forums   
 
Back to Access Forum    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
A Quiz on Query Condition
Kevin Yip 
     
10 hours ago
Hi Richard, here is a quiz of my own that demonstrates some key concepts in database.  Has any student asked you about this before, or has a TechHelp video been made?  This is a somewhat tricky problem that we may come across once in a while, or perhaps quite often.  

In the list of names below, sorted by last name and then first name, how do you create a query that only displays names from "Mitchell, Sophia" onwards?  In other words, the query should find the proper "cut off point" and return only the last 10 names in this list below, starting from "Mitchell, Sophia".

ID     LastName     FirstName
972    Anderson     Alexander
956    Bennett      James
341    Bennett      Henry
36     Brooks       Noah
576    Carter       Olivia
108    Coleman      Mia
166    Cooper       Benjamin
571    Edwards      Samuel
576    Edwards      Liam
729    Hayes        Isabella
740    Mitchell     Sophia
806    Mitchell     Ava
179    Parker       Daniel
459    Peterson     Grace
627    Phillips     Chloe
159    Reynolds     Ethan
239    Richardson   Charlotte
379    Sullivan     Emma
507    Turner       Lucas
33     Wallace      Amelia

You can't use the condition:

   LastName >= "Mitchell" And FirstName >= "Sophia"

because that will miss "Mitchell, Ava".  The FirstName field is *not* necessarily sorted ascendingly from record to record, so you can't use the ">=" operator, nor any operator -- because there is no discernible order in the FirstName field, since it is a secondary sort field.  

The ID field doesn't help us either.  It is an autonumber field that increments by one every time a new record is entered.  The names were not entered alphabetically -- think of them as employees of a company, whose names were entered in the order of when they were hired.

Anyone interested in solving this, please post your answer below.  There may be more than one answer.  Ideally, the solution should cause no change to the table's structure.  But any solution is fine if the problem can be solved, which is the bottom line.
Richard Rost  @Reply  
          
10 hours ago
Kevin, one clarification before I take a crack at this. I notice that within duplicate last names, the first names in your sample appear to be sorted descending. For example, Bennett has James before Henry, Edwards has Samuel before Liam, and Mitchell has Sophia before Ava.

Can we assume that FirstName DESC is actually the secondary sort order, or is that just a coincidence in this particular sample? In other words, if there were many more records, could the first names within the same last name appear in any arbitrary order?

That makes a big difference in how I'd approach the solution.
Kevin Yip OP  @Reply  
     
9 hours ago
Thanks for catching that mistake.  Both fields should be ascending, last name first, and first name second.  Maybe you could just edit the post for me to avoid cluttering up the thread.  Thanks again.
A Toykan  @Reply  
      
4 hours ago
Hi Kevin Try this:

SELECT ID, LastName, FirstName
FROM yourtable
WHERE LastName > Mitchell OR (LastName = Mitchell AND FirstName >= Sophia)
ORDER BY LastName ASC, FirstName ASC

1.Anderson, Alexander
2.Bennett, Henry
3.Bennett, James
4.Brooks, Noah
5.Carter, Olivia
6.Coleman, Mia
7.Cooper, Benjamin
8.Edwards, Liam
9.Edwards, Samuel
10.Hayes, Isabella
11.Mitchell, Ava  (Sorted ASC, Ava is before Sophia)
12.Mitchell, Sophia  (Juntion Point)
13.Parker, Daniel
14.Peterson, Grace
15.Phillips, Chloe
16.Reynolds, Ethan
17.Richardson, Charlotte
18.Sullivan, Emma
19.Turner, Lucas
20.Wallace, Amelia

Records 1–10: Filtered out because their last names come alphabetically before Mitchell.
11th Record (Mitchell, Ava): Has the last name Mitchell, but fails the condition FirstName >= Sophia (Ava<Sophia). Therefore, it is excluded.
12th Record (Mitchell, Sophia): Has the last name Mitchell and satisfies the condition FirstName >= Sophia (Sophia >= Sophia). Included (1st returned record).
Records 13–20 (Parker – Wallace): Included directly regardless of their first names because the first condition (LastName > Mitchell) is true (e.g., Parker > Mitchell). (Returned records 2–9).

Therefore, based on the example in the problem, the number of records meeting the condition is actually only 9. However, if you want to structure this for a larger dataset and list only the first 10 consecutive records from that point forward, you must use
SELECT TOP 10 ID, LastName, FirstName
FROM yourtable
WHERE LastName > "Mitchell" OR (LastName = "Mitchell" AND FirstName >= "Sophia")
ORDER BY LastName ASC, FirstName ASC
Add a Reply Upload an Image
Next Unseen

 
 
What's This?

 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 9/11/2026 6:30:08 AM. PLT: 2s