SQL task from job interview [closed] - mysql

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I was recently on one job interview and one question was about databases which makes me problem for a bit.
This was task:
You are creating database scheme for bloging service where:
Every user has one blog
Every blog can have many different categories of blog posts
Every blog can have blog posts
Every post can be multiple categories
And questiones where:
Write select query containing 2 users with the most posts
Get a post which includes most categories
I have created database with datatables - Users, Blogs, BlogCategory, Categories, Postcategory and Posts
BlogCategory and Postcategory are relation tables mapping relation between blog/post and its category and I think this is correct database scheme.
And about these questions, I think that both will be some complicated queries with inner joins but so far I was not able to solve them. Any idea please?
I am trying in now in firebird db, because MS SQL server has problem installing ms sql management studio. Thing is that you have come up with your own tables with their columns and keys. For sure you will need User, blog, post, category table, next tables are up to you

Pretty vague to say the least but pretty sure something like this should work.
select top 2 UserName
from Posts
group by UserName
order by count(*) desc
select top 1 PostID
from Posts p
join PostCategories pc on pc.PostID = p.PostID
group by p.PostID
order by count(*) desc

Related

Database table structure for Exam test portal [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed yesterday.
This post was edited and submitted for review yesterday and failed to reopen the post:
Original close reason(s) were not resolved
Improve this question
I am developing a website for Exams / Test for Students.
We will have student table (student id) , question table (question id, 4 options, correct option)
However for recording the results of students taking an exam, if we create table that has
student id
question id
selected answer
But the challenge is volume. Expecting 1000 students and 200 questions.. If each student does exam once, it will create 200000 records. Assuming they will be practicing exams multiple times, the rows can go to millions very quickly. This may make data retrieval very slow (for showing in each user profile).
Any suggestion how to manage it ? or should we use different type database (nosql rather than mysql).
thanks
the details are provided above
Look into indexing for your tables. This will improve retrieval speed.
Good reference: http://www.tutorialspoint.com/sql/sql-indexes.htm
Two important factors are:
Speed of your server
SELECT statement has minimal conditions and joins. I would spend some time figuring out some good, clean, and optimized SELECT queries your server could use to retrieve the data.

How to design "like on posts" in relational databases? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 11 months ago.
Improve this question
I'm developing a kind of social app, where an user can write a post, and give like on other posts.
And on the frontend users can see how many likes a post has.
In this case, how can I handle the count of likes for each post?
What I've been thinking so far is...
(1) maintain the post_likes table whose primary keys are user_id and post_id. Everytime users give like on a post, a new row is appended on the table. The count of likes are calculated from counting the corresponding rows of post_likes table.
(2) posts table has a like_count column. Everytime users give like on a post, the value of the column increases by 1. The count of likes are calculated from selecting like_count value.
I think that the implementation (1) would be better in that which user gave like to which post. However, it seems that it could be inefficient since select COUNT(*) should be executed in order to get the like count whenever users requests a page.
what is the best approach in this case?
Both approaches have their pros and cons, but I think approach (1) is a better option for several reasons:
You can prevent users from liking their own posts (if you want to implement that).
You can allow users to remove their like.
You can prevent one user liking a post multiple times.
If performance is an issue you can opt for a new table post_likes_count that will periodically be updated from post_likes with the new like count for a post. Again, keep in mind it depends on what you specifically want to accomplish.

Best Practice for Budgeting MySQL Database Design [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I'm trying to find the best solution for my needs. I have a budgeting application that each user will have their own budget which will include potentially hundreds or thousands of budget entries. Initially I thought I would have a table for the users and basic info, and then each user would have their own sql table with all of their budget items held within. However, if the user list grows to hundreds or thousands then I would have a large amount of tables since each user would have their own. I also considered a single table to hold everyone's budget entries but I'm not sure that's the correct solution either.
Any advice? Thank you in advance
I think a single table that holds all the budget entries, with a primary key that's referenced by a foreign key in the "users" table, is a good way to go. You can always use something like select * from users u join budgets b on u.userID = b.userID where userID = xxx to get the budget items for a particular user.

Mysql, retrieve students who haven't received a request from me [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I am currently logged in as the first student. I want to retrieve a list of the students I did not send a request to yet. I also want to retrieve a list of students I sent a request to.
Both sender_id and Rec_id are foreign keys referencing to Students.id.
DB schema
Hi Souleiman and welcome to stackoverflow! Your question is not quite clear, you say that sender_id and Rec_id in requestts are both referencing to students, but you can't see to which field exactly. Your requestts table contains different entries in these fields.
Next time you should post the output of SHOW CREATE TABLE students and SHOW CREATE TABLE requestts and a little INSERT statement for the data. Furthermore it would be nice if you would explain the relations and show what you have already tried (see Barmars comment).
I suppose that the relevant foreign key in requestts is Rec_id, so you could try this query:
SELECT s.email FROM students s WHERE s.id NOT IN
(SELECT Rec_id FROM requestts WHERE Rec_id = s.id);
Hope this helps. Have fun learning SQL!

How to tie (connect) to values in mysql? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am newbie in programming and I need some help.
I have table (clubs) in DB with teams names and their id.
Next I want to create match in table "matches" in new table named "matches".
How can I tie team's id from "clubs" with teams (home&away)in table matches. I need that id in next actions.
I thought about foreing key, but I cant us it.
Thanks in advance for ur help
Regardless of if the two tables are linked via. a foreign key relationship (although it is good practice to do so) when selecting you can still use a JOIN to connect the two tables. Since you have both a home and away team you need to join the clubs table twice, once for each team.
SELECT m.*, ch.name AS homeClub
FROM matches m
INNER JOIN clubs ch
ON ch.id = matches.home_id
This query will get the match and the home club name, I leave it to you to figure out how to get the away club's name.
Read http://dev.mysql.com/doc/refman/5.0/en/join.html for more information on using joins.