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 840991
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:43:07+00:00 2026-05-15T05:43:07+00:00

If you know about sakila sample database, then what is the statement to select

  • 0

If you know about sakila sample database, then what is the statement to select items currently rented by a user.
If not here is a code explanation:

CREATE TABLE IF NOT EXISTS `rentals` (
 `item_id` int(10) unsigned NOT NULL,
 `user_id` int(10) unsigned NOT NULL,
 `last_change_date` date NOT NULL,
 PRIMARY KEY  (`item_id`,`user_id`,`last_change_date`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Notice there is no return date, this is because the last renter has indefinite rental time, until somebody requests this item and it’s transferred to the new renter for indefinite time as well. A user can rent more than one item and there is only one piece of each item so an item_id cannot go to two users at the same time.

I would like to display currently rented items per user_id.

  • 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-15T05:43:07+00:00Added an answer on May 15, 2026 at 5:43 am
    SELECT r1.*
    FROM   rentals r1
           LEFT JOIN rentals AS r2
             ON r1.item_id = r2.item_id
                AND r1.last_change_date < r2.last_change_date
    WHERE  r2.last_change_date IS NULL  
    

    This is a classic sql question. The answer is explained here.

    Using

    INSERT INTO rentals (item_id, user_id, last_change_date) VALUES (1, 1, '2009-01-01'), (1, 3, '2009-11-10'), (3, 3, '2009-02-13'), (3, 5, '2010-05-11'), (5, 5, '2010-06-04'), (7, 7, '2010-06-04'), (9, 9, '2010-06-04');
    

    as play-data, to understand the method, look at the output of

    SELECT r1.*,r2.*
    FROM   rentals r1
           LEFT JOIN rentals AS r2
             ON r1.item_id = r2.item_id
                AND r1.last_change_date < r2.last_change_date
    
    +---------+---------+------------------+---------+---------+------------------+
    | item_id | user_id | last_change_date | item_id | user_id | last_change_date |
    +---------+---------+------------------+---------+---------+------------------+
    |       1 |       1 | 2009-01-01       |       1 |       3 | 2009-11-10       | 
    |       1 |       3 | 2009-11-10       |    NULL |    NULL | NULL             | 
    |       3 |       3 | 2009-02-13       |       3 |       5 | 2010-05-11       | 
    |       3 |       5 | 2010-05-11       |    NULL |    NULL | NULL             | 
    |       5 |       5 | 2010-06-04       |    NULL |    NULL | NULL             | 
    |       7 |       7 | 2010-06-04       |    NULL |    NULL | NULL             | 
    |       9 |       9 | 2010-06-04       |    NULL |    NULL | NULL             | 
    +---------+---------+------------------+---------+---------+------------------+
    

    The first 3 columns refer to r1‘s columns, the last 3 refer to r2‘s.

    As you can see, whenever there is no r2.last_change_date which is greater than r1.last_change_date, the value is NULL. Those are the rows where r1.last_change_date is greatest. So to find the rows you want, you use
    the condition

    WHERE  r2.last_change_date IS NULL  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.