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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T02:42:53+00:00 2026-06-11T02:42:53+00:00

I have the following query I am trying to optimize. EXPLAIN select clb.f_name, clb.l_name,

  • 0

I have the following query I am trying to optimize.

EXPLAIN
select clb.f_name, clb.l_name, noofbooks 
from (
    select f_name, l_name, count(*) as noofbooks from 
    customer natural join loaned_book
    group by f_name, l_name
) as clb
where 3 > (
    select count(*) from (
        select f_name, l_name, count(*) as noofbooks
        from customer natural join loaned_book
        group by f_name, l_name
    ) as clb1
    where clb.noofbooks<clb1.noofbooks
)
order by noofbooks desc;

Essentially this query is trying to find the “top three” counts (including ties i.e. not limited to 3) of the no. of books loaned by a customer.
The problem is related to the amount of counts that must be made in the query.
Is it possible to use the count values from the first query to reduce selected rows in the second query without recounting all of the rows?

This is a homework task so I am not expecting a direct answer. Any pointers would be appreciated.

  • 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-11T02:42:54+00:00Added an answer on June 11, 2026 at 2:42 am

    Have a look at the dense_rank window function. You want all the rows that have a dense rank of 3 or less.

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

Sidebar

Related Questions

I have to perform the following SQL query: select answer_nbr, count(distinct user_nbr) from tpoll_answer
I have following query: $query=SELECT language_value, votes, user_id FROM labels WHERE approved=1 AND label_value=.
I have following query , select * from process where name like 'abc'; now
I have following SQL query SELECT TOP 10000 AVG(DailyNodeAvailability.Availability) AS AVERAGE_of_Availability FROM Nodes INNER
I have the following query to count all data every minute. $sql= SELECT COUNT(*)
I have the following query: SELECT item.`ID`, item.`Name`, item.`UnitCost`, item.`Price`, discount.`rate` FROM item INNER
I have following query, SELECT UNIX_TIMESTAMP() - UNIX_TIMESTAMP(rc_timestamp) AS LAG_TIME FROM recentchanges ORDER BY
I have the following query: $result = mysql_query("SELECT option_value FROM wp_10_options WHERE option_name='homepage'"); $row
I have the following query: SELECT col1, col2, col3 FROM tb1 WHERE col4=ANY( SELECT
I have the following query: select id from table1 where some_func(?) = 1; where

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.