Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hello, I was wondering if you can help me with a SQL code. I am trying to write a SQL query on Microsoft SQL server

Hello, I was wondering if you can help me with a SQL code. I am trying to write a SQL query on Microsoft SQL server that generates the name of the most popular story for each user (according to total reading counts)? Do you know why my code is not working? Please help!
/* Creating table. */
CREATE TABLE HOM(story_name varchar (255), [user] char(50), age int, reading_counts int NOT NULL, CONSTRAINT Story_User PRIMARY KEY (story_name,[user]));
INSERT INTO HOM VALUES ('Humpty Dumpty', 'Andrea', 5, 10);
INSERT INTO HOM VALUES ('Wheels on the Bus', 'Simon', 3, 15);
INSERT INTO HOM VALUES ('Baa Baa Black Sheep', 'Leo', 7, 4);
INSERT INTO HOM VALUES ('Jack and Jill', 'James', 4, 11);
INSERT INTO HOM VALUES ('Three Blind Mice', 'Leslie', 6, 12);
INSERT INTO HOM VALUES ('Humpty Dumpty', 'Simon', 3, 8);
INSERT INTO HOM VALUES ('Three Blind Mice', 'Andrea', 5, 7);
INSERT INTO HOM VALUES ('Wheels on the Bus', 'Andrea', 5, 11);
/* View table.*/
SELECT * FROM HOM;
/* Generates most popular book*/
SELECT * FROM HOM
INNER JOIN
(SELECT DISTINCT [user], MAX(reading_counts) AS Max_reads FROM HOM GROUP BY [user]) b
ON HOM.[user] = b.[user]
AND HOM.reading_counts = b.reading_counts;

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Time Series Databases New Ways To Store And Access Data

Authors: Ted Dunning, Ellen Friedman

1st Edition

1491914726, 978-1491914724

Students also viewed these Databases questions

Question

107 MA ammeter 56 resistor ? V voltmeter

Answered: 1 week ago

Question

Generally If Drug A is an inducer of Drug B , Drug B levels will

Answered: 1 week ago

Question

3. Would you say that effective teamwork saved their lives?

Answered: 1 week ago