Write an SQL write on a List property - sql

I have the following tables Situation:
A Blog contains many Posts. A Post has an Author.
I want to write an SQL query that: returns all the unique blogs that have at least 3 posts by the author "Janet";
I am no sure how to do this as I usually work with EntityFramework. Any help would be great.
**EDIT
I tried this:
SELECT auth.name
FROM Blog blo
LEFT JOIN Auth auth
WHERE blo.authname = "janet"

Select distinct Blogs from tblblogs,tblposts,tblauthor where tblblogs.id=tblposts.blogid and tblposts.id=tblauthor.postid and tblautor.name='Janet'
group by blogs
having count(tblposts.id)>=3
This is a rough query because you didn't mention the table structure. If you want more elaboration, kindly mention table structure

Related

What am I / Was I missing on this SQL request?

I apologize for any vagueness, this is about a question I had on a entry level SQL position test last week. I couldn't figure out how to do this at the time, and can't seem to figure anything out now.
Basically I was provided with 3 tables. One was Recipes (had recipe name, ID, instructions and notes), one was RecipeIngredients (had recipe ID, ingredient ID, ingredients, and quantity of ingredients), and the third was Ingredients (ingredient ID and ingredient). Something like that.
I had a few queries with JOIN statements that showed how to make certain recipes and so on. But I couldn't figure out quite how to manage the final question. The final question was something like:
"Provide 2 sets of queries at once. 1st query - Return Ingredients, quantities, and notes for a specific ID. 2nd Query - Return the instructions for the same Recipe ID. Write the queries so that the user can easily alter the recipe ID in one place only for both queries in order to query for different recipe IDs."
I know we can't alias a WHERE clause, but that is the only thing I can remotely think of for doing 2 queries at once with only specifying the WHERE once. I tried to see if I could do it with a subquery but had no luck. I considered UNION... but there were different columns and different values in each query so that's a no go.
Is there something I'm missing? Or did I just completely fail when trying to set this up as a subquery? I apologize for vagueness, it's been a few days and I've been too busy to remember to post this on here. I've found a lot of help anonymously browsing this site in the past so I figured it was worth posting this as I've not seen anything similar so far.
Using SQL Server, you could have declared a variable, like #recipeID, then used it in two queries. That would allow you to change the value in one place, and have it used in 2 queries.
DECLARE #recipeID INT = 123
SELECT *
FROM recipes
WHERE recipeID = #recipeID
SELECT *
FROM recipe_ingredients
WHERE recipeID = #recipeID

Where can I find a list of all OrientDB SQL projections?

I'm looking for documentation about projects in OrientDB SQL syntax. I've seen various examples, but I'd like a comprehensive list.
Examples of projections:
SELECT out('my_edge') FROM Person
SELECT expand(in('my_edge')) FROM Person
SELECT max('my_field') FROM Person
SELECT count(*) FROM Person
There are obviously lots of other ones out there, but I'd love it if there were some complete syntax documentation for this somewhere.
There are a lots of different methods for the projections but this is list of the different functions that you can use (with examples) included your posted above:
http://orientdb.com/docs/last/SQL-Functions.html
While, here, you can find a couple of examples of queries:
http://orientdb.com/docs/2.1/Query-Examples.html
Hope it helps

Rails double match from has_and_belongs_to_many

Say that I have a has_and_belongs_to_many relationship where I have posts and categories. It is simple to find all the posts in a category, or all the categories that a particular post is a member of. However, what if I want to find a list of posts that belong to multiple categories? For example, a list of posts that are on the topic of security in Rails, I might want the posts that belong to the categories "Security" and "Rails".
Is it possible to do this with the finder methods build into ActiveRecord, or will I need to use SQL? Can someone please explain how?
You can use includes or joins, like:
#result = Post.includes(:categories).where("categories.name = 'Security' OR categories.name = 'Rails'")
or
#result = Post.joins(:categories).where("categories.name = 'Security' OR categories.name = 'Rails'")
I also suggest to check this railscast to understand the difference between joins and includes, so you can decide what is better in your case.
i don't know anything about rails, but i'm attempting a similar thing with some sql. this may or may not work for either of us....
i have a table of articles, and a look-up table of applied categories. to get an article that has the 'security' category and the 'rails' category, i'm joining the article table to the category table, of course, but also re-joining it a second time. each join of the category table uses a hint in the table alias name (ie language or topic)
pseudo code:
SELECT article.*,
category_language.category_id,
category_topic.category_id
FROM category category_language
INNER JOIN article ON category_language.articleID = article.articleID
INNER JOIN category category_topic ON article.articleID = category_topic.articleID
WHERE category_language.category_id in (420) /* rails */
and category_topic.category_id in (421) /* security */
this isn't completely ironed out, and i hope that if i am showing my ignorance here, someone will speak up.

Master-detail Filtering in SQL

I have an interesting query that I would like to get done:
1. I have an Article table [ArticleId, ArticleName]
2. An Article has an ArticleCheckin (1:1) [ArticleId, CheckinName]
3. An Article can have multiple ArticleResources [ArticleResourceId, ArticleId, ResourceName]
I would like to write a query that gets me a list of Articles with Checkin (if exists) and a list of the Article Resources for each Article in the same query.
Currently, I obtain the Article and am querying the Resources again for each article and that doesn't seem like the right way to do this.
Any help would be great!
AFAIK a query will always give you a flattened out result. This means that in order to get a list of Articles and lists of corresponding ArticleResources, you'll need to loop through each Article like you are doing now.
But you are not saying anything about which technology you are using, so it is hard to suggest how you can improve on your query/code.
This query will give you all articles with or without checkins but only if there are article resources (LEFT JOIN otherwise).
SELECT A.*, AC.*, AR.*
FROM Article A
LEFT JOIN ArticelCheckin AC ON AC.ArticleId = A.ArticleId
INNER JOIN ArticleResources AR ON AR.ArticleId = A.ArticleId

MySQL: Limit output according to associated ID

So here's my situation. I have a books table and authors table. An author can have many books... In my authors page view, the user (logged in) can click an author in a tabled row and be directed to a page displaying the author's books (collected like this URI format: viewauthorbooks.php?author_id=23), very straight forward... However, in my query, I need to display the books for the author only, and not all books stored in the books table (as i currently have!) As I am a complete novice, I used the most simple query of:
SELECT * FROM books
This returns the books for me, but returns every single value (book) in the database, and not ones associated with the selected author. And when I click a different author the same books are displayed for them...I think everyone gets what I'm trying to achieve, I just don't know how to perform the query. I'm guessing that I need to start using more advanced query clauses like INNER JOIN etc. Anyone care to help me out :)
Enters the WHERE clause:
The WHERE clause is used to extract only those records that fulfill a specific criteria. In your case, all you need to do is:
SELECT * FROM tasks_tb WHERE author_id = '23';
You will obviously need to change the '23' with the value passed in the URL querystring, so that each page lists the books of each relevant author.
Since it is never too early to start reading about best practices, note that for public websites it is really dangerous to include any un-sanitized input into an SQL query. You may want to read further on this topic from the following Stack Overflow posts:
XKCD sql injection - please explain (with pictures!)
What is SQL injection?
Is SQL injection a risk today?
SQL Injection Topics on Stack Overflow
The basic syntax is
SELECT * FROM table WHERE column=value;
But you really need to study more SQL, I'd suggest going through sqlzoo tutorial. http://sqlzoo.net/