Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6963941
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:51:36+00:00 2026-05-27T15:51:36+00:00

What are the orders of the following SQL statements from most to least efficient?

  • 0

What are the orders of the following SQL statements from most to least efficient?

NOTE: Let the syntaxes be language agnostic (i.e. use TOP 1 in the proper place instead of LIMIT 1). Assume that table_name is the name of the table, column_name is the name of a column, id is the name of a column with a primary key, and the table has tens of thousands of records.

  1. SELECT FIRST(column_name) FROM table_name
  2. SELECT column_name FROM table_name LIMIT 1
  3. SELECT column_name FROM table_name WHERE id=1 --assumes the first id is 1
  4. SELECT column_name FROM table_name WHERE id=(SELECT MIN(id) FROM table_name)

AND

  1. SELECT LAST(column_name) FROM table_name
  2. SELECT column_name FROM table_name ORDER BY id DESC LIMIT 1
  3. SELECT column_name FROM table_name WHERE id=(SELECT COUNT(id) FROM table_name) --assumes no values have been skipped
  4. SELECT column_name FROM table_name WHERE id=(SELECT MAX(id) FROM table_name)

I don’t know how to benchmark these statements but my guess is 2, 3, 1, 4 for getting the first record and 3, 2, 1, 4 for getting the last record.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:51:37+00:00Added an answer on May 27, 2026 at 3:51 pm

    I am guessing but; FIRST and LAST are grouping operations. For the query engine to get their results it would need to collect the data first.

    The others just need to return the first row it finds.
    If there’s an index on ‘id’ ordering it descending is not very expensive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following snippet from a SQL statements ROW_NUMBER() OVER (ORDER BY CASE
How can I make the following SQL a Linq query: SELECT * FROM ORDERS
I have the following SQL statement: USE ws_results_db_2011_09_11_09_06_24;SELECT table_name FROM INFORMATION_SCHEMA.Tables WHERE table_name like
I have the following SQL statement: Select Choose(1,Orders.Employee, Orders.Customer) as Name1, Choose(2,Orders.Employee, Orders.Customer) as
I have the following query: WITH Orders(Id) AS ( SELECT DISTINCT anfrageid FROM MPHotlineAnfrageAnhang
With the following query I get the list of top 10 most expensive queries
Again I'm having troubles while migrating from Access to MySQL . The following SQL
w3schools.com has the following SQL statement example: SELECT Product_Orders.OrderID, Persons.LastName, Persons.FirstName FROM Persons, Product_Orders
I'm using the following SQL statement to copy data from TableA to TableB. INSERT
I have the following SQL statement SELECT be.*, it.order_number FROM songs AS be INNER

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.