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

  • SEARCH
  • Home
  • 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 8265377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T04:46:45+00:00 2026-06-08T04:46:45+00:00

The following table will store exchange rates between various currencies over time: CREATE TABLE

  • 0

The following table will store exchange rates between various currencies over time:

CREATE TABLE `currency_exchange` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `currency_from` int(11) DEFAULT NULL,
 `currency_to` int(11) DEFAULT NULL,
 `rate_in` decimal(12,4) DEFAULT NULL,
 `rate_out` decimal(12,4) DEFAULT NULL,
 `exchange_date` datetime DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

How would I query it to fetch a list the most recent exchange rates?

It’d essentially be identifying the combined currency_from and currency_to columns as a distinct exchange rate, then finding the one with the most recent date.

For example, let’s say I’ve got the data as:

INSERT INTO currency_exchange (id, currency_from, currency_to, rate_in, rate_out, exchange_date) VALUES
(1, 1, 2, 0.234, 1.789, '2012-07-23 09:24:34'),
(2, 2, 1, 0.234, 1.789, '2012-07-24 09:24:34'),
(3, 2, 1, 0.234, 1.789, '2012-07-24 09:24:35'),
(4, 1, 3, 0.234, 1.789, '2012-07-24 09:24:34');

I’d want it to select row ID’s:

  • 1 – as the most recent rate between currencies 1 and 2
  • 3 – as the most recent rate between currencies 2 and 1
  • 4 – as the most recent rate between currencies 1 and 3
  • 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-06-08T04:46:48+00:00Added an answer on June 8, 2026 at 4:46 am

    The following query should work:

    SELECT ce.*
    FROM currency_exhcnage ce
    LEFT JOIN currency_exchange newer
        ON (newer.currency_from = ce.currency_from
        AND newer.currency_to = ce.currency_to
        AND newer.exchange_date > ce.exchange_date)
    WHERE newer.id IS NULL
    

    The trick of doing a self-LEFT JOIN is to avoid resorting to a subquery that may be very expensive if you have large datasets. It’s essentially looking for records where no “newer” record exists.

    Alternatively, you could go for a simpler (although it may (or may not, as noted in comments) be slower):

    SELECT *
    FROM currency_exchange ce
    NATURAL JOIN (
        SELECT currency_from, currency_to, MAX(exchange_date) AS exchange_date
        FROM currency_exchange
        GROUP BY currency_from, currency_to
    ) AS most_recent
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following table...: CREATE TABLE v ( height int, width int, depth int, volume
For example, I have the following table: CREATE TABLE `test` ( `total_results` int(10) unsigned
I have a table which will contain various categories. The following is a minified
QTableWidget::setItemPrototype says following. The table widget will use the item prototype clone function when
Assuming an InnoDB table, will the following command ... CHECK TABLE table_name; ... cause
I have the following code, which will retrieve a filename from a table and
I have the following table drop table names; create table names( name varchar(200), surname
I have to create an application in PHP which will store the phone contacts
I have an SQL table with the following structure: Code1 - int Code2 -
I have the following tables: FACULTY table CREATE TABLE FACULTY ( FACULTY_ID NUMBER(3,0), FACULTY_NAME

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.