Answered step by step
Verified Expert Solution
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
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started