So, I have tasks, and there are some that I still canβt solve even with the help of Google. Maybe someone can suggest some solutions (should this be done with JOINS or not?) Etc.).
Here is the database:
CREATE TABLE Foto ( ID int PRIMARY KEY, FileName varchar(100), FileSize int, Created int ) DEFAULT CHARSET=utf8; CREATE TABLE Users ( LoginName varchar(10) PRIMARY KEY, Surname varchar(20), Name varchar(20), Created date, LastLoginDate date ) DEFAULT CHARSET=utf8; CREATE TABLE Scoring ( ID int, LoginName varchar(10), Score int, ScoreDate date, FOREIGN KEY (LoginName) REFERENCES Users(LoginName), FOREIGN KEY (ID) REFERENCES Foto(ID), PRIMARY KEY(ID, LoginName) ) DEFAULT CHARSET=utf8;
The table "Photo" (Photo) and "Users" should be quite understandable. The Scoring table is for users who rate photos. Each user can rate each photo only once. The task was as follows:
Write a query that will return the photo ID, file_name and AVG (rating) of all photos whose AVG rating exceeds 9, and create an index that will increase the performance of this request.
Write a query that will return "LoginName", "Name", "Surname" and the AVG rating that the user gave to the photos that he rated for a specific user (for example, a user named "John"). Create an index that will increase the performance of this query.
I am using MySQL 5.5.16, and my idea for the first query was something like this:
SELECT F.ID, F.FileName, AVG(SC.Score) FROM Foto F, scoring SC WHERE F.ID = SC.ID AND AVG(SC.Score) > 9 GROUP BY SC.ID;
And it returns "# 1111 - Invalid use of a group function", I never liked aggregate functions: D
In general, I realized that sucking SQL, and I would like to train to write more queries, but most of the tutorials that I find on the Internet have fairly simple examples that do not help in solving these problems. Perhaps some of you can offer me a resource that will have a more complex, ready-made database and tasks (which also have answers if I'm stuck) for writing queries?
Javatar
source share